Skip to content

Instantly share code, notes, and snippets.

@chenhan1218
Last active March 15, 2022 02:25
Show Gist options
  • Save chenhan1218/51f93321b8e6582d791a1ddade4a7609 to your computer and use it in GitHub Desktop.
Save chenhan1218/51f93321b8e6582d791a1ddade4a7609 to your computer and use it in GitHub Desktop.
import zlib
import diskcache
class ZLIBDISK(diskcache.Disk):
def __init__(self, directory, **kwargs):
super(ZLIBDISK, self).__init__(directory, **kwargs)
def put(self, key):
data = zlib.compress(pickle.dumps(key))
return super(ZLIBDISK, self).put(data)
def get(self, key, raw):
data = super(ZLIBDISK, self).get(key, raw)
return pickle.loads(zlib.decompress(data))
def store(self, value, read, key=diskcache.core.UNKNOWN):
if not read:
value = zlib.compress(pickle.dumps(value))
return super(ZLIBDISK, self).store(value, read, key=key)
def fetch(self, mode, filename, value, read):
data = super(ZLIBDISK, self).fetch(mode, filename, value, read)
if not read:
data = pickle.loads(zlib.decompress(data))
return data
cache = diskcache.Cache('cache', disk=ZLIBDISK)
cache.set('data', data)
cache.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment