Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
[package]
name = "memcache"
version = "0.17.0"
authors = ["An Long <[email protected]>"]
repository = "https://github.com/aisk/rust-memcache"
readme = "README.md"
license = "MIT"
description = "memcached client for rust"
keywords = ["memcache", "memcached", "driver", "cache", "database"]
edition = "2018"

[features]
default = ["tls"]
tls = ["openssl"]

[dependencies]
byteorder = "1"
url = "2.1.1"
rand = "0.8"
enum_dispatch = "0.3"
openssl = { version = "^0.10", optional = true }
r2d2 = "0.8.8"
[workspace]
members = [
"examples/*",
"memcache"
]
default-members = [
"memcache"
]
10 changes: 10 additions & 0 deletions examples/axum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "example-axum"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
axum = { version = "0.6.19", features = ["headers"] }
memcache = { path = "../../memcache" }
tokio = { version = "1.15.0", features = ["full"] }
66 changes: 66 additions & 0 deletions examples/axum/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! An example of how to setup Axum with memcached, using a shared
//! connection pool that is accessible to all requests through Axum's
//! state.
//! Run the example with:
//!
//! ```not_rust
//! cargo run -p example-axum
//! ```

use std::sync::Arc;

use axum::{
extract::{Path, State},
routing::get,
Router, Server,
};
use memcache::{self, Pool, Url};

#[derive(Clone)]
struct AppState {
memcache: Arc<memcache::Client>,
}

async fn get_root(State(app_state): State<AppState>, Path(key): Path<String>) -> String {
match app_state.memcache.get(&key) {
Ok(Some(value)) => value,
Ok(None) => "Not found".to_string(),
Err(err) => format!("ERROR: {}", err),
}
}

async fn post_root(State(app_state): State<AppState>, Path(key): Path<String>, body: String) -> String {
match app_state.memcache.set(&key, body, 300) {
Ok(_) => "OK".to_string(),
Err(e) => format!("ERROR: {}", e),
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("This example requires a memcached server running on 127.0.0.1:11211");

let memcached_url = "memcache://127.0.0.1:11211";
let memcached_url = Url::parse(memcached_url)?;

let pool = Pool::builder()
.max_size(10)
.build(memcache::ConnectionManager::new(memcached_url))?;
let client = memcache::Client::with_pool(pool)?;

let app = Router::new()
.route("/kv/:key", get(get_root).post(post_root))
.with_state(AppState {
memcache: Arc::new(client),
});

println!("Starting server on http://0.0.0.0:3000");
println!("Set keys using [POST] http://0.0.0.0:3000/kv/<key> with a body for the value");
println!("Get keys using [GET] http://0.0.0.0:3000/kv/<key>");

Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
Ok(())
}
8 changes: 8 additions & 0 deletions examples/pooled/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "example-pooled"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
memcache = { path = "../../memcache" }
27 changes: 27 additions & 0 deletions examples/pooled/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! An example of how to setup a connection pool for memcached
//! connections.
//! Run the example with:
//!
//! ```not_rust
//! cargo run -p example-pooled
//! ```

use memcache::{Client, ConnectionManager, Pool, Url};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let localhost = Url::parse("memcache://localhost:11211")?;
let pool = Pool::builder().max_size(10).build(ConnectionManager::new(localhost))?;
let client = Client::with_pool(pool)?;

// sets a key to a value, with a 10 second expiration time
client.set("test", "value", 10)?;

// gets the value of a key
if let Some(value) = client.get::<String>("test")? {
println!("test: {}", value);
}

// deletes a key
client.delete("test")?;
Ok(())
}
8 changes: 8 additions & 0 deletions examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "example-simple"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
memcache = { path = "../../memcache" }
28 changes: 28 additions & 0 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! A simple example of using the memcache crate, which
//! connects to a memcached server and sets, gets, and
//! deletes a key.
//! Run the example with:
//!
//! ```not_rust
//! cargo run -p example-simple
//! ```

use memcache::connect;

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let localhost = "memcache://localhost:11211";
let client = connect(localhost).expect("Couldn't connect to memcached");

// sets a key to a value, with a 10 second expiration time
client.set("test", "value", 10)?;

// gets the value of a key
if let Some(value) = client.get::<String>("test")? {
println!("test: {}", value);
}

// deletes a key
client.delete("test")?;

Ok(())
}
22 changes: 22 additions & 0 deletions memcache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "memcache"
version = "0.17.0"
authors = ["An Long <[email protected]>"]
repository = "https://github.com/aisk/rust-memcache"
readme = "README.md"
license = "MIT"
description = "memcached client for rust"
keywords = ["memcache", "memcached", "driver", "cache", "database"]
edition = "2021"

[features]
default = ["tls"]
tls = ["openssl"]

[dependencies]
byteorder = "1"
url = "2.1.1"
rand = "0.8"
enum_dispatch = "0.3"
openssl = { version = "^0.10", optional = true }
r2d2 = "0.8.8"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.