This library implements the ghetto lock described here. The lock isn't resistant to server failures and should be used only in situations where strong locking guarantees are not required.
A popular use case for this lock is to avoid the stampeding herd problem caused by a cache miss.
## Usage:
Add to your Cargo.toml
:
ghetto-lock = "0.1.0"
## Example:
```rust use ghetto_lock::{LockOptions, LockError}; use memcache::Client; use std::borrow::Cow;
fn expensive_computation() -> u64 { 2 * 2 }
fn main() { let mut client = Client::connect("memcache://localhost:11211").expect("error creating client"); let mut lock = LockOptions::new(Cow::Borrowed("db-lock"), Cow::Borrowed("owner-1")) .withexpiry(1) .build() .expect("failed to build client"); let value = client.get("key").expect("failed to get key"); let v = if value.isnone() { lock.tryacquire() .andthen(|guard| { // compute and update cache for other instances to consume let v = expensivecomputation(); client.set("key", v, 5).expect("failed to set key"); Ok(v) }) .orelse(|| loop { // poll cache key until it is updated. let v = client.get("key").expect("failed to get key"); if v.isnone() { continue; } break Ok::<_, LockError>(v.unwrap()); }).unwrap() } else { value.unwrap() }; asserteq!(4, v); } ```