Skip to content

Commit 19f908b

Browse files
committed
Add docs for JSONDisk.
1 parent 9cfe5df commit 19f908b

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

diskcache/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
'EmptyDirWarning',
2626
'FanoutCache',
2727
'Index',
28-
"JSONDisk",
28+
'JSONDisk',
2929
'Lock',
3030
'RLock',
3131
'Timeout',

diskcache/core.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,26 +363,42 @@ def remove(self, filename):
363363

364364

365365
class JSONDisk(Disk):
366-
"""Cache key and value (de)serialized as JSON."""
366+
"Cache key and value using JSON serialization with zlib compression."
367367
def __init__(self, directory, compress_level=1, **kwargs):
368+
"""Initialize JSON disk instance.
369+
370+
Keys and values are compressed using the zlib library. The
371+
`compress_level` is an integer from 0 to 9 controlling the level of
372+
compression; 1 is fastest and produces the least compression, 9 is
373+
slowest and produces the most compression, and 0 is no compression.
374+
375+
:param str directory: directory path
376+
:param int compress_level: zlib compression level (default 1)
377+
:param kwargs: super class arguments
378+
379+
"""
368380
self.compress_level = compress_level
369381
super(JSONDisk, self).__init__(directory, **kwargs)
370382

383+
371384
def put(self, key):
372385
json_bytes = json.dumps(key).encode('utf-8')
373386
data = zlib.compress(json_bytes, self.compress_level)
374387
return super(JSONDisk, self).put(data)
375388

389+
376390
def get(self, key, raw):
377391
data = super(JSONDisk, self).get(key, raw)
378392
return json.loads(zlib.decompress(data).decode('utf-8'))
379393

394+
380395
def store(self, value, read, key=UNKNOWN):
381396
if not read:
382397
json_bytes = json.dumps(value).encode('utf-8')
383398
value = zlib.compress(json_bytes, self.compress_level)
384399
return super(JSONDisk, self).store(value, read, key=key)
385400

401+
386402
def fetch(self, mode, filename, value, read):
387403
data = super(JSONDisk, self).fetch(mode, filename, value, read)
388404
if not read:

docs/api.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ Read the :ref:`Disk tutorial <tutorial-disk>` for details.
116116
:special-members:
117117
:exclude-members: __weakref__
118118

119+
JSONDisk
120+
--------
121+
122+
Read the :ref:`Disk tutorial <tutorial-disk>` for details.
123+
124+
.. autoclass:: diskcache.JSONDisk
125+
:members:
126+
:special-members:
127+
:exclude-members: __weakref__
128+
119129
Timeout
120130
-------
121131

docs/tutorial.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +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,
817-
available for convenience as :class:`Disk <diskcache.JSONDisk>`.
816+
example below uses compressed JSON, available for convenience as
817+
:class:`JSONDisk <diskcache.JSONDisk>`.
818818

819819
.. code-block:: python
820820

0 commit comments

Comments
 (0)