grantjenks / python-diskcache

Python disk-backed cache (Django-compatible). Faster than Redis and Memcached. Pure-Python.

Home Page:http://www.grantjenks.com/docs/diskcache/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quesion about cache.close() and cull()

santosh-kaladagi opened this issue · comments

Hello,
I am trying to use diskcache in my project, so far i think it's been really good. There are few things I want to clarify

  1. I see the following in the documentation:

Each thread that accesses a cache should also call close on the cache. Cache objects can be used in a with statement to safeguard calling close.

cache.close()

with Cache(cache.directory) as reference:

    reference.set('key', 'value')
True

Closed Cache objects will automatically re-open when accessed. But opening Cache objects is relatively slow, and since all operations are atomic, may be safely left open

This is not very clear since the documentation says we should close the connection but it also says it may be safely left open. What is the good practice and also are there any examples that i can use?

  1. Regarding cull() - I understand cull() function removes keys from cache until the cache volume is less than the size limit. However, this would mean when I add a new key to the cache, the cache volume would go over size limit again. Is there a way to enforce cull or some other function to remove items so that it can fit the next key I am going to add in, say by removing older items in the cache using least-recently-stored policy may be?

Apologies for the long post. Any comments on this would be greatly appreciated.

I looked at the source code, in bit more detail. I think i am clear about cull function now.
But it would be great if someone can explain a bit more about my 1st question.

If you don’t close the cache in each thread the you’ll incur the cost of a file handle. It’s typically a small cost so not worth bothering about.

Cull is called automatically when a new item is inserted in the cache.