rust-memcache
rust-memcache is a memcached client written in pure rust.
Install
The crate is called memcache and you can depend on it via cargo:
[dependencies]
memcache = "*"
Features
- All memcached supported protocols
- Binary protocol
- ASCII protocol
- Meta protocol (experimental, in the
expmodule)
- All memcached supported connections
- TCP connection
- UDP connection
- UNIX Domain socket connection
- TLS connection
- Encodings
- Typed interface
- Automatically compress
- Automatically serialize to JSON / msgpack etc
- Memcached cluster support with custom key hash algorithm
- Authority
- Binary protocol (plain SASL authority plain)
- ASCII protocol
Basic usage
// create connection with to memcached server node:
let client = connect.unwrap;
// flush the database
client.flush.unwrap;
// set a string value
client.set.unwrap;
// retrieve from memcached:
let value: = client.get.unwrap;
assert_eq!;
assert_eq!;
// prepend, append:
client.prepend.unwrap;
client.append.unwrap;
let value: String = client.get.unwrap.unwrap;
assert_eq!;
// cas(check and set):
let : = client.get.unwrap.unwrap;
assert_eq!;
let cas_id = cas_token.unwrap;
client.cas.unwrap;
// delete value:
client.delete.unwrap;
// using counter:
client.set.unwrap;
client.increment.unwrap;
let answer: i32 = client.get.unwrap.unwrap;
assert_eq!;
Custom key hash function
If you have multiple memcached server, you can create the memcache::Client struct with a vector of urls of them. Which server will be used to store and retrive is based on what the key is.
This library have a basic rule to do this with rust's builtin hash function, and also you can use your custom function to do this, for something like you can using a have more data on one server which have more memory quota, or cluster keys with their prefix, or using consitent hash for large memcached cluster.
let mut client = connect.unwrap;
client.hash_function = ;
Experimental meta protocol client
The memcache::exp module contains a new client built on memcached's meta protocol. It is experimental and not feature-complete yet, but the main paths are covered: get / set / delete / counters with their protocol options, CAS, typed values, batches, multiple servers, connection pooling, timeouts, and an async client behind the tokio feature.
The API may change between minor versions. Within a minor version it is guaranteed to stay compatible, so pin the minor version if you use it:
[dependencies]
memcache = "0.20" # >= 0.20.0, < 0.21.0
Typical usage
use MetaClient;
let client = connect?;
// Plain operations; options are chained before send():
client.set.ttl.send?;
let result = client.get.send?;
assert_eq!;
client.delete.send?;
// Store only when the key does not exist:
client.set.ttl.add.send?;
// Typed values; numbers are stored as decimal ASCII and work with counters:
client.set.send?;
client.increment.send?;
let visits: = client.get.send?.decode?;
// Several operations in one round trip:
use ;
let results = client.run_batch?;
Clients are cheap to clone and shareable across threads or tasks; clones share per-server connection pools. Multiple servers, pool size and timeouts:
use Duration;
let client = connect_multiple?
.with_max_idle
.with_connect_timeout
.with_io_timeout;
The async client has the same surface, behind the tokio feature:
use AsyncMetaClient;
let client = connect.await?;
client.set.ttl.send.await?;
let result = client.get.send.await?;
Advanced: cache stampede protection with leases
When a hot key misses or is close to expiring, lease_ttl grants exactly one reader a lease to recompute the value; concurrent readers are told the refresh is pending (or keep serving the current value) instead of all hitting the backing store at once:
let result = client.get.lease_ttl.refresh_before.send?;
if result.won_lease else if result.hit else
Advanced: optimistic concurrency with CAS
use ;
let current = client.get.meta.send?;
let updated = mutate;
let stored = client
.set
.compare_cas
.send?;
if stored.status == CasMismatch
Contributing
Before sending pull request, please ensure:
cargo fmtis being run;- Commit message is using gitmoji, for example:
✨ rust-memcache can print money now.
Contributors
License
MIT