From 091c571227954e80b438c7f78ef5fce98ef33445 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 25 Sep 2018 21:45:04 -0500 Subject: [PATCH] cache --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 13 ++++++------- cache/Cargo.toml | 2 ++ cache/hashmap.rs | 38 ++++++++++++++++++++++++++++++++++++++ cache/lib.rs | 29 ++++++++++++++++++++++++++++- cache/redis.rs | 1 + src/lib.rs | 1 - src/web/guards.rs | 1 - src/web/template.rs | 3 +-- 9 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 cache/hashmap.rs create mode 100644 cache/redis.rs diff --git a/Cargo.lock b/Cargo.lock index 323c65b..4c56fff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cache" version = "0.1.0" +dependencies = [ + "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_cbor 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "cc" @@ -318,6 +322,11 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "half" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "httparse" version = "1.3.2" @@ -526,6 +535,7 @@ dependencies = [ "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.11.16 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -844,6 +854,16 @@ name = "serde" version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_cbor" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "half 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_derive" version = "1.0.79" @@ -1290,6 +1310,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum half 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee6c0438de3ca4d8cac2eec62b228e2f8865cfe9ebefea720406774223fa2d2e" "checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" @@ -1354,6 +1375,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum serde 1.0.78 (registry+https://github.com/rust-lang/crates.io-index)" = "92ec94e2754699adddbbc4f555791bd3acc2a2f5574cba16c93a4a9cf4a04415" +"checksum serde_cbor 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45cd6d95391b16cd57e88b68be41d504183b7faae22030c0cc3b3f73dd57b2fd" "checksum serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "31569d901045afbff7a9479f793177fe9259819aff10ab4f89ef69bbc5f567fe" "checksum serde_json 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "59790990c5115d16027f00913e2e66de23a51f70422e549d2ad68c8c5f268f1c" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" diff --git a/Cargo.toml b/Cargo.toml index 8b6ae24..d31e42a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,15 +7,17 @@ authors = ["Michael "] members = [".", "cache"] [dependencies] -cache = { path = "cache" } -embed = { git = "https://github.com/iptq/embed" } - bcrypt = "0.2" +cache = { path = "cache" } +diesel = { version = "1.3", features = ["mysql"] } +embed = { git = "https://github.com/iptq/embed" } env_logger = "0.5" failure = "0.1.1" lazy_static = "1.0" log = "0.4" mime_guess = "2.0.0-alpha.6" +r2d2 = "0.8" +r2d2-diesel = "1.0" rocket = "0.3" rocket_codegen = "0.3" rocket_contrib = { version = "0.3", default-features = false } @@ -23,7 +25,4 @@ serde = "1.0" serde_derive = "1.0" structopt = "0.2" tera = "0.11" - -r2d2 = "0.8" -r2d2-diesel = "1.0" -diesel = { version = "1.3", features = ["mysql"] } +toml = "0.4" diff --git a/cache/Cargo.toml b/cache/Cargo.toml index 188f41e..097749b 100644 --- a/cache/Cargo.toml +++ b/cache/Cargo.toml @@ -7,3 +7,5 @@ authors = ["Michael Zhang "] path = "lib.rs" [dependencies] +serde = "1.0" +serde_cbor = "0.9" diff --git a/cache/hashmap.rs b/cache/hashmap.rs new file mode 100644 index 0000000..bc86684 --- /dev/null +++ b/cache/hashmap.rs @@ -0,0 +1,38 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +use serde_cbor::Value; +use serde_cbor::{from_value, to_value}; + +use Cache; + +/// A cache implemented with a HashMap. +pub struct HashMapCache { + inner: HashMap, +} + +impl Cache for HashMapCache { + type Error = (); + + fn get(&self, key: K) -> Result + where + K: AsRef, + for<'a> V: Deserialize<'a>, + { + self.inner + .get(key.as_ref()) + .and_then(|value| from_value(value.clone()).ok()) + .ok_or(()) + } + + fn set(&mut self, key: K, value: V) -> Result<(), Self::Error> + where + K: AsRef, + V: Serialize, + { + self.inner + .insert(key.as_ref().to_owned(), to_value(value).map_err(|_| ())?); + Ok(()) + } +} diff --git a/cache/lib.rs b/cache/lib.rs index 638bb7c..6a5c232 100644 --- a/cache/lib.rs +++ b/cache/lib.rs @@ -13,5 +13,32 @@ #![deny(missing_docs)] +extern crate serde; +extern crate serde_cbor; + +#[cfg(feature = "redis")] +mod redis; + +mod hashmap; + +use serde::{Deserialize, Serialize}; + +pub use hashmap::HashMapCache; + /// An abstraction for a key-value cache. -pub trait Cache {} +pub trait Cache { + /// The Error type (should support both `get` and `set`). + type Error; + + /// Get the value associated with the given `key` from the datastore. + fn get(&self, key: K) -> Result + where + K: AsRef, + for<'a> V: Deserialize<'a>; + + /// Set the value associated with `key` to `value`. + fn set(&mut self, key: K, value: V) -> Result<(), Self::Error> + where + K: AsRef, + V: Serialize; +} diff --git a/cache/redis.rs b/cache/redis.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/cache/redis.rs @@ -0,0 +1 @@ + diff --git a/src/lib.rs b/src/lib.rs index 3252b89..e7169ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,4 +38,3 @@ pub mod web; pub use challenge::Challenge; pub use config::Config; -use db::Connection; diff --git a/src/web/guards.rs b/src/web/guards.rs index 3edddc6..a369c20 100644 --- a/src/web/guards.rs +++ b/src/web/guards.rs @@ -2,7 +2,6 @@ use rocket::{ request::{self, FromRequest, Request}, Outcome, }; -use serde::ser::{Serialize, SerializeStruct, Serializer}; use tera::Context; #[derive(Serialize, Default)] diff --git a/src/web/template.rs b/src/web/template.rs index cc3b4cb..76753d6 100644 --- a/src/web/template.rs +++ b/src/web/template.rs @@ -1,6 +1,5 @@ use std::borrow::Cow; -use env_logger; use rocket::{ http::{ContentType, Status}, response::{self, Content}, @@ -55,7 +54,7 @@ impl Template { Err(err) => { error!("Template render error: {}", err); Err(Status::InternalServerError) - }, + } } } }