You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to change JSONDisk's compression level, but it seems that setting disk parameters at cache initialization doesn't work. To test this, I wrote a script that reuses the JSONDisk example and prints the compression level:
fromdiskcacheimportCache, Disk, UNKNOWNimportjsonimportzlibclassJSONDisk(Disk):
"""Cache key and value using JSON serialization with zlib compression."""def__init__(self, directory, compress_level=1, **kwargs):
"""Initialize JSON disk instance. Keys and values are compressed using the zlib library. The `compress_level` is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, 9 is slowest and produces the most compression, and 0 is no compression. :param str directory: directory path :param int compress_level: zlib compression level (default 1) :param kwargs: super class arguments """self.compress_level=compress_levelprint(f'Init: {self.compress_level}')
super().__init__(directory, **kwargs)
defput(self, key):
print(f'Put: {self.compress_level}')
json_bytes=json.dumps(key).encode('utf-8')
data=zlib.compress(json_bytes, self.compress_level)
returnsuper().put(data)
defget(self, key, raw):
print(f'Get: {self.compress_level}')
data=super().get(key, raw)
returnjson.loads(zlib.decompress(data).decode('utf-8'))
defstore(self, value, read, key=UNKNOWN):
print(f'Store: {self.compress_level}')
ifnotread:
json_bytes=json.dumps(value).encode('utf-8')
value=zlib.compress(json_bytes, self.compress_level)
returnsuper().store(value, read, key=key)
deffetch(self, mode, filename, value, read):
print(f'fetch: {self.compress_level}')
data=super().fetch(mode, filename, value, read)
ifnotread:
data=json.loads(zlib.decompress(data).decode('utf-8'))
returndatac1=Cache(disk=JSONDisk)
c9=Cache(disk=JSONDisk, disk_compress_level=9)
print('Storing in c1')
c1['1'] ='one'print('Storing in c9')
c1['9'] ='nine'print('Retrieving from c1')
c1['1']
print('Retrieving from c9')
c1['9']
The expected output is for c1 to print 1 and c9 to print 9. But instead c9 only prints 9 at initialization and prints 1 otherwise:
Init: 1
Init: 9
Storing in c1
Put: 1
Store: 1
Storing in c9
Put: 1
Store: 1
Retrieving from c1
Put: 1
fetch: 1
Retrieving from c9
Put: 1
fetch: 1
I tried this across python 3.11 and 3.12, and diskcache 5.6 all the way back to 4.0, and all pairs of python/diskcache versions failed to produce the expected result.
This would also affect all subclasses of Disk, since any disk_ arguments will be ignored.
The text was updated successfully, but these errors were encountered:
Thanks for the typo notice. I have reread the docs and found that disk_* should be used to set disk parameters. Initializing c9 as c9 = Cache(disk=JSONDisk, disk_compress_level=9) gives the correct result.
I tried to change JSONDisk's compression level, but it seems that setting disk parameters at cache initialization doesn't work. To test this, I wrote a script that reuses the JSONDisk example and prints the compression level:
The expected output is for c1 to print 1 and c9 to print 9. But instead c9 only prints 9 at initialization and prints 1 otherwise:
I tried this across python 3.11 and 3.12, and diskcache 5.6 all the way back to 4.0, and all pairs of python/diskcache versions failed to produce the expected result.
This would also affect all subclasses of Disk, since any
disk_
arguments will be ignored.The text was updated successfully, but these errors were encountered: