Crates.io | arbitrary-lock |
lib.rs | arbitrary-lock |
version | 0.1.1 |
source | src |
created_at | 2023-06-30 10:05:26.341422 |
updated_at | 2023-07-04 04:29:17.011804 |
description | Lock by arbitrary keys |
homepage | https://github.com/wilsonzlin/arbitrary-lock |
repository | https://github.com/wilsonzlin/arbitrary-lock.git |
max_upload_size | |
id | 904322 |
size | 7,256 |
Acquire a lock on an arbitrary key, like a string or integer.
Add the dependency to your project:
cargo add arbitrary-lock
Use the struct:
// This can be cheaply cloned.
// The key must be Hash + Eq + Clone.
// Provide your preferred lock type as the second generic argument. It must implement Default.
let locker = ArbitraryLock::<String, tokio::sync::Mutex<()>>::new();
{
let _l = locker.get("user:2185724".to_string()).lock().await;
do_something_in_database().await;
};