Comments (1)
I think this error was hiding an underlying type error. The correct code which seems to work is this:
pub struct MyCache<V> {
pub dir: String,
my_hash: HashMap<Key, V>,
}
impl<V> MyCache<V> {
pub fn new() -> MyCache<V> {
MyCache {
dir: "./my_cache".to_string(),
my_hash: HashMap::new(),
}
}
fn get_key_hash(&self, key: &Key) -> String {
format!("{}__{}", key.0, key.1)
}
}
// This can be more generalized if we want to
type Key = (String, String);
impl<V: DeserializeOwned + Serialize> Cached<Key, V> for MyCache<V> {
fn cache_get(&mut self, k: &Key) -> Option<&V> {
let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;
let as_str = String::from_utf8(output).unwrap();
let o: V = serde_json::from_str(&as_str).unwrap();
self.my_hash.insert(k.to_owned(), o);
self.my_hash.get(k)
}
fn cache_set(&mut self, k: Key, v: V) -> Option<V> {
let vec = serde_json::to_vec(&v).unwrap();
cacache::write_sync(&self.dir, self.get_key_hash(&k), vec).unwrap();
Some(v)
}
fn cache_get_mut(&mut self, k: &Key) -> Option<&mut V> {
let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;
let as_str = String::from_utf8(output).unwrap();
let o: V = serde_json::from_str(&as_str).unwrap();
self.my_hash.insert(k.to_owned(), o);
self.my_hash.get_mut(k)
}
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: Key, f: F) -> &mut V {
let gotten = self.cache_get(&k);
if gotten.is_none() {
let new = f();
self.cache_set(k.clone(), new).unwrap();
}
self.cache_get_mut(&k).unwrap()
}
fn cache_remove(&mut self, k: &Key) -> Option<V> {
let key = self.get_key_hash(k);
cacache::remove_sync(&self.dir, key).unwrap();
self.my_hash.remove(k)
}
fn cache_clear(&mut self) {
cacache::clear_sync(&self.dir).unwrap();
self.my_hash.clear();
}
fn cache_reset(&mut self) {
cacache::clear_sync(&self.dir).unwrap();
self.my_hash = HashMap::new();
}
fn cache_size(&self) -> usize {
self.my_hash.len()
}
}
from cached.
Related Issues (20)
- Cron-like cache clearing HOT 1
- Mio & Tokio causing wasm build to fail
- Consider only changing patch versions when making non breaking releases
- Feature Request: bool function argument that forces a cache refresh. HOT 1
- Update cached value asynchronously, outside the thread that returns the return value of the function HOT 1
- How to get all cached values? HOT 1
- Feature Request: Floating-Point ttl HOT 4
- Add feature for usage of `redis::aio::ConnectionManager` to help to recover from redis server restarts HOT 1
- `async_once` is unsound HOT 2
- Allow reexport
- sync_writes isn't working correctly when different values for function parameters are used HOT 5
- Tokio included when not using async HOT 1
- Documentation on docs.rs for `stores` module does not exist
- Cannot use `inner` as parameter name HOT 2
- Any plan on Set collection? HOT 1
- Retrieve cache expiration time from cached function result
- Also generate the fresh data function HOT 2
- Cannot call *_no_cache function HOT 3
- `failed to resolve: could not find `async_sync` in `cached`` HOT 2
- Once with sync_writes and async causes deadlocks in tokio.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cached.