31 releases (12 breaking)

0.12.0 Feb 15, 2019
0.10.0 Jan 1, 2019
0.9.2 Dec 17, 2018
0.9.0 Nov 5, 2018
0.0.4 Nov 21, 2014

#37 in Database interfaces

Download history 86/week @ 2018-12-04 100/week @ 2018-12-11 250/week @ 2018-12-18 26/week @ 2018-12-25 79/week @ 2019-01-01 85/week @ 2019-01-08 99/week @ 2019-01-15 102/week @ 2019-01-22 133/week @ 2019-01-29 97/week @ 2019-02-05 76/week @ 2019-02-12 89/week @ 2019-02-19 134/week @ 2019-02-26 75/week @ 2019-03-05 61/week @ 2019-03-12

417 downloads per month
Used in 1 crate

MIT license

73KB
1.5K SLoC

rust-memcache

Build Status Codecov Status Crates.io MIT licensed Docs Gitter chat

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

  • Binary protocol
  • ASCII protocol
  • TCP connection
  • UDP connection
  • UNIX Domain socket connection
  • Automatically compress
  • Automatically serialize to JSON / msgpack etc.
  • Typed interface
  • Mutiple server support with custom key hash algorithm
  • SASL authority (plain)

Basic usage

// create connection with to memcached server node:
let mut client = memcache::Client::connect("memcache://127.0.0.1:12345?timeout=10&tcp_nodelay=true").unwrap();

// flush the database
client.flush().unwrap();

// set a string value
client.set("foo", "bar", 0).unwrap();

// retrieve from memcached:
let value: Option<String> = client.get("foo").unwrap();
assert_eq!(value, Some(String::from("bar")));
assert_eq!(value.unwrap(), "bar");

// prepend, append:
client.prepend("foo", "foo").unwrap();
client.append("foo", "baz").unwrap();
let value: String = client.get("foo").unwrap().unwrap();
assert_eq!(value, "foobarbaz");

// delete value:
client.delete("foo").unwrap();

// using counter:
client.set("counter", 40, 0).unwrap();
client.increment("counter", 2).unwrap();
let answer: i32 = client.get("counter").unwrap().unwrap();
assert_eq!(answer, 42);

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 = memcache::Client::connect(vec!["memcache://127.0.0.1:12345", "memcache:///tmp/memcached.sock"]).unwrap();
client.hash_function = |key: &str| -> u64 {
    // your custom hashing function here
    return 1;
};

License

MIT

Dependencies

~2.5MB
~42K SLoC