Comments (7)
Yeah, allowing the cache to be overweight might have large implications.
If we ignore that part, I could probably add support for get_or_insert
to return a Option<&mut V>
kind of thing w/o extra lookups.
from quick-cache.
Even just insert
returning a Result<&mut V, V>
would work for pretty much everything in my case. Basically either V is in the cache and I can refer to it that way, or it was not inserted for any reason, in which case I can still grab a reference of the value and do something with it without duplicating the effort to regenerate it.
But that would require not calling on_evict
if the item hasn't actually been inserted into the cache.
Thanks for the quick response.
from quick-cache.
I tried to add APIs that would satisfy your use case, but it's elusive to them to co-exists with non-guaranteed admission and the lifecycle hooks. It might take another round of refactors to get it exactly right.
Can you check if
Line 248 in 74aeb9e
from quick-cache.
I think I could implement what I need with get_mut_or_guard
. get_mut_or_insert_with
, as far as I understand would require me to duplicate the logic acting on the value if I wanted to do something with it regardless of whether the value was able to make it into the cache or not. Something like this perhaps:
let already_used = false;
value = cache.get_mut_or_insert_with(key, || {
let mut value = generate();
already_used = true;
use(&mut value);
value
});
if !already_used {
use(&mut value);
}
Not too terrible, though in this particular application it would perhaps be better if get_mut_or_insert_with
either invoked the callback XOR returned a reference, not potentially both. That would allow dropping the already_used
type of flag.
from quick-cache.
Thanks for responding so quickly and spending your time on implementing this! Much appreciated!
from quick-cache.
I merged #23 on master
now, I'll keep the issue open until I make a crates.io release with it.
from quick-cache.
0.5 has now been published
from quick-cache.
Related Issues (12)
- Add atomic `get_or_insert` method to sync `Cache`/`KQCache` HOT 5
- Consider making dependency on `parking_lot` optional HOT 1
- Documentation errors HOT 1
- get_or_* method's trait bound is too strict . HOT 1
- `before_evict` + `weight` API is stateful/awkward (it can't effectively do pining) HOT 5
- Is estimated_items_capacity superfluous in Cache::with_weighter? HOT 6
- Clear all items from `sync::Cache` HOT 3
- Shouldn't Weighter::weight return u64? HOT 2
- Weight capacity per shard and highly varing cache item weights HOT 3
- Equivalent not retrieving correctly HOT 2
- Any wasm/js support? HOT 1
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 quick-cache.