mirror of
https://github.com/deps-rs/deps.rs.git
synced 2024-11-25 11:36:30 +00:00
Revert "use lru_time_cache to improve cache expiry"
This reverts commit 063d418747
.
This commit is contained in:
parent
063d418747
commit
fec6f12a48
4 changed files with 34 additions and 18 deletions
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<S>
|
||||
where S: Service<Error=Error>,
|
||||
S::Request: Ord
|
||||
S::Request: Hash + Eq
|
||||
{
|
||||
inner: S,
|
||||
cache: Mutex<LruCache<S::Request, Shared<FromErr<S::Future, SharedFailure>>>>
|
||||
duration: Duration,
|
||||
cache: Mutex<LruCache<S::Request, (Instant, Shared<FromErr<S::Future, SharedFailure>>)>>
|
||||
}
|
||||
|
||||
impl<S> Debug for Cache<S>
|
||||
where S: Service<Error=Error> + 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<S> Cache<S>
|
||||
where S: Service<Error=Error>,
|
||||
S::Request: Clone + Ord
|
||||
S::Request: Hash + Eq
|
||||
{
|
||||
pub fn new(service: S, duration: Duration, capacity: usize) -> Cache<S> {
|
||||
Cache {
|
||||
inner: service,
|
||||
cache: Mutex::new(LruCache::with_expiry_duration_and_capacity(duration, capacity))
|
||||
duration: duration,
|
||||
cache: Mutex::new(LruCache::new(capacity))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Service for Cache<S>
|
||||
where S: Service<Error=Error>,
|
||||
S::Request: Clone + Ord
|
||||
S::Request: Clone + Hash + Eq
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = CachedItem<S::Response>;
|
||||
|
@ -51,14 +55,17 @@ impl<S> Service for Cache<S>
|
|||
type Future = Cached<S::Future>;
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue