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

diskkache.Cache.get ignoring read=True parameter

inakrin opened this issue · comments

Let's consider this snippet

from diskcache import Cache
import time
import json
import sys
def main():
    cache = Cache(
        directory="cache",
        size_limit=1000000,
        cull_limit=10
    )
    val = {"name":"test",
           "age":25,}
    user_encode_data = json.dumps(val).encode('utf-8')
    for num in range(10):
        _ = cache.set(num, user_encode_data)
    print(1 in cache)
    val = cache.get(1,read=True)
    print(val)
    print(type(val))
    print(cache.get(1,read=True).read().decode('utf-8'))
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main()

In this code cache.get ignores read=True and returns bytes instead of file handle as mentioned in the docs https://grantjenks.com/docs/diskcache/api.html#diskcache.Cache.get

read (bool) – if True, return file handle to value (default False)

That is confusing. For read=True to work reliably, you must use a file-like object and set read=True in the set() call.

There’s a tiny reference related to this in the tutorial regarding DjangoCache: “When values are set using read=True they are guaranteed to be stored in files.” https://grantjenks.com/docs/diskcache/tutorial.html#djangocache

The current value is otherwise so small that it is being stored in the SQLite database itself rather than in a file. See https://www.sqlite.org/fasterthanfs.html for motivation.

You could also set the disk_min_file_size setting to zero. I think that would force all your values to files. https://grantjenks.com/docs/diskcache/tutorial.html#settings

Also, I notice you are serializing using json. There’s a JSONDisk for that https://grantjenks.com/docs/diskcache/tutorial.html#disk

Thank you for the explanation! Would be good to have it mentioned this behavior in the docs to reduce possible confusion