| Crates.io | redlock_batched |
| lib.rs | redlock_batched |
| version | 0.2.0 |
| created_at | 2025-02-25 11:45:27.092053+00 |
| updated_at | 2025-02-25 12:03:39.170213+00 |
| description | Creating, updating and deleting redis locks (Redlock) in batches |
| homepage | |
| repository | https://github.com/raui100/redlock_batched |
| max_upload_size | |
| id | 1569055 |
| size | 91,843 |
This implementation of Redlock
processes creating, updating and deleting RedLocks in batches (redis pipeline).
Due to batching the number of operations per second is reduced and thousands of RedLocks
can be handled concurrently.
RedLock in batchesRedLockredis and kvrocksThe following program expects redis to run with an open port at 6379
use std::time::Duration;
use redis::AsyncCommands;
use redlock_batched::{LockManager, RedisLock};
#[tokio::main]
async fn main() {
let client = redis::Client::open("redis://127.0.0.1:6379").unwrap();
let mut con = redis::aio::ConnectionManager::new(client).await.unwrap();
let manager = LockManager::build(con.clone()).finalize();
// Creating a lock
let lock = RedisLock::new("my_lock".to_string(), Duration::from_secs(5));
let rx = manager.create_lock(&lock);
match rx.await.unwrap() {
redlock_batched::CreationResult::Ready => println!("lock has been created"),
redlock_batched::CreationResult::Canceled => unreachable!(),
};
let value: String = con.get(&lock.name).await.unwrap();
assert_eq!(value, lock.lock_id.to_string());
// Updating the time to live of the lock
let rx = manager.update_lock_ttl_with_result(&lock.clone().with_ttl(Duration::from_secs(1)));
match rx.await.unwrap() {
redlock_batched::UpdateResult::Updated => println!("TTL of lock has been updated"),
redlock_batched::UpdateResult::Expired => unreachable!(),
}
// Deleting the lock
let rx = manager.delete_lock_with_result(lock.name.clone(), lock.lock_id);
match rx.await.unwrap() {
redlock_batched::DeleteResult::Deleted => println!("the lock has been deleted"),
redlock_batched::DeleteResult::Expired => unreachable!(),
}
let value: Option<String> = con.get(&lock.name).await.unwrap();
assert_eq!(value, None);
}
Have a look at tests/integration_test.rs for more code examples