Expand description
§RLock (Redis Lock)
It is an easy-to-use Redis-backed lock library providing both async and sync Mutex, RwLock, and others.
§Examples
use tokio::task::JoinSet;
use rlock::async_lock::RLock;
#[tokio::main]
async fn main() {
static mut COUNTER: u32 = 0;
let rlock = RLock::new("redis://127.0.0.1:6379/0").await.unwrap();
async fn counter_increase(rlock: RLock) {
let lock = rlock.acquire_mutex("lock").await.unwrap();
// ----- critical section -----
unsafe { COUNTER += 1 };
// ----------------------------
drop(lock);
}
let mut tasks = JoinSet::new();
for _ in 0..1000 {
tasks.spawn(counter_increase(rlock.clone()));
}
tasks.join_all().await;
assert_eq!(1000, unsafe { COUNTER });
rlock.shutdown().await;
}
§Roadmap / TODO
- Asynchronous mutex lock
- Synchronous mutex lock
- Asynchronous read-write lock
- Synchronous read-write lock
Re-exports§
pub use redis;asyncorsync
Modules§
- async_
lock async - This module provides an async API based on Tokio.
Structs§
- Acquire
Options asyncorsync - Options used when acquiring a lock.
Enums§
- Acquire
Error asyncorsync - Errors for handling lock acquisition failures.
- Release
Error asyncorsync - Errors for handling lock release failures.
Functions§
- build_
redis_ key_ from_ parts - Builds a Redis key by joining multiple parts with
:as a separator. - build_
redis_ key_ from_ parts_ with_ prefix - Builds a Redis key by combining a prefix with multiple parts, using
:as a separator. - build_
redis_ key_ with_ prefix - Builds a Redis key by concatenating a prefix and a key with
:as a separator.