GithubHelp home page GithubHelp logo

Comments (8)

grantjenks avatar grantjenks commented on July 29, 2024 1

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.

grantjenks avatar grantjenks commented on July 29, 2024

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.

pombredanne avatar pombredanne commented on July 29, 2024

How are you using DiskCache? Is it as a cross-process mapping?

I have two usages in scancode-toolkit:

from python-diskcache.

grantjenks avatar grantjenks commented on July 29, 2024

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.

pombredanne avatar pombredanne commented on July 29, 2024

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.

pombredanne avatar pombredanne commented on July 29, 2024

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.

grantjenks avatar grantjenks commented on July 29, 2024

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.

pombredanne avatar pombredanne commented on July 29, 2024

@grantjenks that would be fine! Thanks!

from python-diskcache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.