Comments (8)
Deployed at 2.6.3
The default Index uses a Cache with the "none" eviction policy. If you want to limit the space the cache uses then you'll need to use Index.fromcache
like:
import diskcache as dc
cache = dc.Cache('/tmp/cache', size_limit=int(1e9))
index = dc.Index.fromcache(cache)
You can add other parameters to Cache
as necessary. Just remember that items may then be evicted based on space needs. (The default never evicts items.)
I haven't added docs to update the tutorial yet but the api docs at http://www.grantjenks.com/docs/diskcache/api.html#index have code samples for every method.
from python-diskcache.
That is intentional. I wasn't sure what to do about expired keys. Some users want a Mapping data type and to skip expiration altogether. Other users want a cache and expiration is critical.
How are you using DiskCache? Is it as a cross-process mapping?
from python-diskcache.
How are you using DiskCache? Is it as a cross-process mapping?
I have two usages in scancode-toolkit:
- a cross-process size-limited fanout cache for license matches used across invocations that you can see here: https://github.com/nexB/scancode-toolkit/blob/5aa84c1b10d6c19244f98847041a2d4b03503df2/src/licensedcode/cache.py#L153
- right I am adding a cross-process mapping that lives (and is deleted once done) for the duration of an invocation. that I am working on ATM for this ticket nexB/scancode-toolkit#267
from python-diskcache.
I designed all methods on Cache to return as quickly as possible and hold locks on the underlying SQLite database as little as possible. Iteration is a corner case because it could take a long time. So I decided to permit iterating expired items. That's a bit surprising but you can call evict
beforehand and explicitly remove cruft if you like.
Your code is not setting the timeout
parameter so keys will expire only based on cache size. Still, just because iteration yields a key, does not mean that value is still in the cache. I'm afraid the best option is to do a get
.
More thought is warranted. Cache
/FanoutCache
are probably more building blocks for a full-featured Mapping
data type.
For now, this seems like an easy case for subclassing:
from diskcache import Cache
class MyCache(Cache):
def iteritems(self):
for key in self:
try:
yield key, self[key]
except KeyError:
pass
from python-diskcache.
Thanks. FWIW, the FanoutCache code is not a use case for iteration but code I have not yet pushed.
And, to your point a subclass with iteritems is about right for now and I am using about the same approach (without the subclass).
from python-diskcache.
And here is the code where I could possibly benefit from an iteritems
like function: https://github.com/nexB/scancode-toolkit/blob/8a106d3f6ce22e381b18ec55ddfbaceb44ce4edb/src/scancode/cache.py#L133
In that use case, the are never any expired items, to I am quite fine with the behaviour of iterating over everything
from python-diskcache.
I recently added diskcache.Index which has a MutableMapping interface. Does that work for you scenario? See http://www.grantjenks.com/docs/diskcache/api.html#index for details. Note that this still is missing iteritems
but I would be more amenable to adding it there.
from python-diskcache.
@grantjenks that would be fine! Thanks!
from python-diskcache.
Related Issues (20)
- Is this project still in active development? HOT 1
- sqlite3.DatabaseError: database disk image is malformed HOT 2
- Quesion about cache.close() and cull() HOT 2
- DiskCache Dropping Items HOT 5
- Native async implementation HOT 1
- Controlling the number of files generated by Cache HOT 3
- in Cache._cull(), cull_limit appears to be prescriptive vs. a limit & in Cache.cull(), 10 files are deleted at a time HOT 2
- Enable custom type serialization in `JSONDisk` HOT 2
- diskkache.Cache.get ignoring read=True parameter HOT 2
- Deque `peekleft` blocks infinitely after corruption(?) HOT 18
- No real-time synchronization between writing data and reading data in different processes. HOT 1
- RFE: is it possible to start making github releases?🤔 HOT 2
- Deque with JSONDisk throws TypeError: a bytes-like object is required, not 'int' HOT 1
- [Feature Request] Allow iterkeys method to yield value or tag along with key?
- High memory usage with multiple threads HOT 3
- [Bug] Fetching an item while writing the item into Cache HOT 4
- "unable to open database file" with Python 3.11 and PySide6 threads HOT 9
- Mark the distributed package as typed HOT 1
- Long-lived cache management HOT 2
- PR for fork deadlock 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 python-diskcache.