Skip to content

Commit 9cfe5df

Browse files
zsimicgrantjenks
authored andcommitted
Provide JSONDisk with diskcache (#124)
1 parent 912f5b6 commit 9cfe5df

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# virutalenv directories
55
/env*/
6+
/.venv*/
67

78
# test files/directories
89
/.cache/

diskcache/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
"""
88

9-
from .core import Cache, Disk, EmptyDirWarning, UnknownFileWarning, Timeout
9+
from .core import Cache, Disk, EmptyDirWarning, JSONDisk, UnknownFileWarning, Timeout
1010
from .core import DEFAULT_SETTINGS, ENOVAL, EVICTION_POLICY, UNKNOWN
1111
from .fanout import FanoutCache
1212
from .persistent import Deque, Index
@@ -25,6 +25,7 @@
2525
'EmptyDirWarning',
2626
'FanoutCache',
2727
'Index',
28+
"JSONDisk",
2829
'Lock',
2930
'RLock',
3031
'Timeout',

diskcache/core.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import errno
88
import functools as ft
99
import io
10+
import json
1011
import os
1112
import os.path as op
1213
import pickletools
@@ -361,6 +362,34 @@ def remove(self, filename):
361362
raise
362363

363364

365+
class JSONDisk(Disk):
366+
"""Cache key and value (de)serialized as JSON."""
367+
def __init__(self, directory, compress_level=1, **kwargs):
368+
self.compress_level = compress_level
369+
super(JSONDisk, self).__init__(directory, **kwargs)
370+
371+
def put(self, key):
372+
json_bytes = json.dumps(key).encode('utf-8')
373+
data = zlib.compress(json_bytes, self.compress_level)
374+
return super(JSONDisk, self).put(data)
375+
376+
def get(self, key, raw):
377+
data = super(JSONDisk, self).get(key, raw)
378+
return json.loads(zlib.decompress(data).decode('utf-8'))
379+
380+
def store(self, value, read, key=UNKNOWN):
381+
if not read:
382+
json_bytes = json.dumps(value).encode('utf-8')
383+
value = zlib.compress(json_bytes, self.compress_level)
384+
return super(JSONDisk, self).store(value, read, key=key)
385+
386+
def fetch(self, mode, filename, value, read):
387+
data = super(JSONDisk, self).fetch(mode, filename, value, read)
388+
if not read:
389+
data = json.loads(zlib.decompress(data).decode('utf-8'))
390+
return data
391+
392+
364393
class Timeout(Exception):
365394
"Database timeout expired."
366395

docs/tutorial.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ database while values are sometimes stored separately in files.
813813
To customize serialization, you may pass in a :class:`Disk <diskcache.Disk>`
814814
subclass to initialize the cache. All clients accessing the cache are expected
815815
to use the same serialization. The default implementation uses Pickle and the
816-
example below uses compressed JSON.
816+
example below uses compressed JSON,
817+
available for convenience as :class:`Disk <diskcache.JSONDisk>`.
817818

818819
.. code-block:: python
819820

tests/test_core.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import functools as ft
88
import hashlib
99
import io
10-
import json
1110
import mock
1211
import os
1312
import os.path as op
@@ -22,7 +21,6 @@
2221
import time
2322
import unittest
2423
import warnings
25-
import zlib
2624

2725
try:
2826
import cPickle as pickle
@@ -94,35 +92,8 @@ def test_disk_valueerror():
9492
pass
9593

9694

97-
class JSONDisk(diskcache.Disk):
98-
def __init__(self, directory, compress_level=1, **kwargs):
99-
self.compress_level = compress_level
100-
super(JSONDisk, self).__init__(directory, **kwargs)
101-
102-
def put(self, key):
103-
json_bytes = json.dumps(key).encode('utf-8')
104-
data = zlib.compress(json_bytes, self.compress_level)
105-
return super(JSONDisk, self).put(data)
106-
107-
def get(self, key, raw):
108-
data = super(JSONDisk, self).get(key, raw)
109-
return json.loads(zlib.decompress(data).decode('utf-8'))
110-
111-
def store(self, value, read, key=dc.UNKNOWN):
112-
if not read:
113-
json_bytes = json.dumps(value).encode('utf-8')
114-
value = zlib.compress(json_bytes, self.compress_level)
115-
return super(JSONDisk, self).store(value, read, key=key)
116-
117-
def fetch(self, mode, filename, value, read):
118-
data = super(JSONDisk, self).fetch(mode, filename, value, read)
119-
if not read:
120-
data = json.loads(zlib.decompress(data).decode('utf-8'))
121-
return data
122-
123-
12495
def test_custom_disk():
125-
with dc.Cache(disk=JSONDisk, disk_compress_level=6) as cache:
96+
with dc.Cache(disk=dc.JSONDisk, disk_compress_level=6) as cache:
12697
values = [None, True, 0, 1.23, {}, [None] * 10000]
12798

12899
for value in values:

0 commit comments

Comments
 (0)