diff --git a/Cargo.lock b/Cargo.lock index b58e10a..8205630 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,6 +273,11 @@ name = "libc" version = "0.2.36" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "linked-hash-map" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "literalext" version = "0.1.1" @@ -295,9 +300,12 @@ dependencies = [ ] [[package]] -name = "lru_time_cache" -version = "0.8.0" +name = "lru-cache" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "maud" @@ -680,7 +688,7 @@ dependencies = [ "hyper-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "indexmap 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lru_time_cache 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "maud 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "route-recognizer 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -966,10 +974,11 @@ dependencies = [ "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum lazycell 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a6f08839bc70ef4a3fe1d566d5350f519c5912ea86be0df1740a7d247c7fc0ef" "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" +"checksum linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7860ec297f7008ff7a1e3382d7f7e1dcd69efc94751a2284bafc3d013c2aa939" "checksum literalext 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f42dd699527975a1e0d722e0707998671188a0125f2051d2d192fc201184a81" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" -"checksum lru_time_cache 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d151f6ddf993d99d1f3fa84530f8a91287497606559e6792ac5453268c17455b" +"checksum lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4d06ff7ff06f729ce5f4e227876cb88d10bc59cd4ae1e09fbb2bde15c850dc21" "checksum maud 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c0023c814f4a545946ab612ad64a4edce8126d4fe4f0abc5f319b80877112048" "checksum maud_htmlescape 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0fb85bccffc42302ad1e1ed8679f6a39d1317f775a37fbc3f79bdfbe054bfb7" "checksum maud_macros 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8377d77c1995044b8ad67a59d15b434c8b7de470ac743de4916ee2bd9fce55" diff --git a/Cargo.toml b/Cargo.toml index d4225a5..bfd8c46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ hyper = "0.11.15" hyper-tls = "0.1.2" indexmap = { version = "0.4.1", features = ["serde-1"] } lazy_static = "1.0.0" -lru_time_cache = "0.8.0" +lru-cache = "0.1.1" maud = "0.17.2" relative-path = { version = "0.3.7", features = ["serde"] } route-recognizer = "0.1.12" diff --git a/src/main.rs b/src/main.rs index 3b0af9c..8ea8e0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ extern crate hyper; extern crate hyper_tls; extern crate indexmap; #[macro_use] extern crate lazy_static; -extern crate lru_time_cache; +extern crate lru_cache; extern crate maud; extern crate relative_path; extern crate route_recognizer; diff --git a/src/utils/cache.rs b/src/utils/cache.rs index 2cc05ef..a002159 100644 --- a/src/utils/cache.rs +++ b/src/utils/cache.rs @@ -1,49 +1,53 @@ use std::fmt::{Debug, Formatter, Result as FmtResult}; -use std::time::Duration; +use std::hash::Hash; +use std::time::{Duration, Instant}; use std::ops::Deref; use std::sync::Mutex; use failure::Error; use futures::{Future, Poll}; use futures::future::{FromErr, Shared, SharedItem}; -use lru_time_cache::LruCache; +use lru_cache::LruCache; use shared_failure::SharedFailure; use tokio_service::Service; pub struct Cache where S: Service, - S::Request: Ord + S::Request: Hash + Eq { inner: S, - cache: Mutex>>> + duration: Duration, + cache: Mutex>)>> } impl Debug for Cache where S: Service + Debug, - S::Request: Ord + S::Request: Hash + Eq { fn fmt(&self, fmt: &mut Formatter) -> FmtResult { fmt.debug_struct("Cache") .field("inner", &self.inner) + .field("duration", &self.duration) .finish() } } impl Cache where S: Service, - S::Request: Clone + Ord + S::Request: Hash + Eq { pub fn new(service: S, duration: Duration, capacity: usize) -> Cache { Cache { inner: service, - cache: Mutex::new(LruCache::with_expiry_duration_and_capacity(duration, capacity)) + duration: duration, + cache: Mutex::new(LruCache::new(capacity)) } } } impl Service for Cache where S: Service, - S::Request: Clone + Ord + S::Request: Clone + Hash + Eq { type Request = S::Request; type Response = CachedItem; @@ -51,14 +55,17 @@ impl Service for Cache type Future = Cached; fn call(&self, req: Self::Request) -> Self::Future { + let now = Instant::now(); let mut cache = self.cache.lock().expect("lock poisoned"); - if let Some(shared_future) = cache.get_mut(&req) { - if let Some(Ok(_)) = shared_future.peek() { - return Cached(shared_future.clone()); + if let Some(&mut (valid_until, ref shared_future)) = cache.get_mut(&req) { + if valid_until > now { + if let Some(Ok(_)) = shared_future.peek() { + return Cached(shared_future.clone()); + } } } let shared_future = self.inner.call(req.clone()).from_err().shared(); - cache.insert(req, shared_future.clone()); + cache.insert(req, (now + self.duration, shared_future.clone())); Cached(shared_future) } }