44 releases

0.17.2 Jan 21, 2024
0.17.1 Nov 9, 2023
0.17.0 Oct 31, 2022
0.16.0 Nov 7, 2021
0.0.4 Nov 21, 2014

#122 in Database interfaces

Download history 978/week @ 2024-01-22 846/week @ 2024-01-29 906/week @ 2024-02-05 825/week @ 2024-02-12 844/week @ 2024-02-19 1557/week @ 2024-02-26 1210/week @ 2024-03-04 1445/week @ 2024-03-11 1901/week @ 2024-03-18 1116/week @ 2024-03-25 1155/week @ 2024-04-01 1480/week @ 2024-04-08 1622/week @ 2024-04-15 1678/week @ 2024-04-22 1165/week @ 2024-04-29 934/week @ 2024-05-06

5,458 downloads per month
Used in 7 crates (6 directly)

MIT license

92KB
2K SLoC

rust-memcache

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

rust-memcache is a memcached client written in pure rust.

logo

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
  • 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 = memcache::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::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;
};

Contributing

Before sending pull request, please ensure:

  • cargo fmt is being run;
  • Commit message is using gitmoji with first character is lower cased, for example: :sparkles: rust-memcache can print money now.

Contributors

License

MIT

Dependencies

~2.6–9MB
~108K SLoC