youknowone / ring

Python cache interface with clean API and built-in memcache & redis + asyncio support.
http://ring-cache.readthedocs.io/en/latest/
Other
480 stars 37 forks source link

Using json file as storage? #165

Closed elyase closed 3 years ago

elyase commented 3 years ago

Is it possible to use a json file as storage? Something like:

@ring.json("path/to/file.json")
def cahed_method(arg):
    return {"key": "value"}
youknowone commented 3 years ago

If you just want to read and write from disks, that's easy. shelve or diskcache storage will fit enough. This is how I tested it.

def test_json_shelve():
    import shelve

    @ring.shelve(shelve.open('/tmp/ring-test/shelve'), coder='json')
    def cached_method(arg):
        return {"key": "value"}

    assert cached_method(None)['key'] == 'value'
def test_json_disk():
    import diskcache
    @ring.disk(diskcache.Cache('/tmp/ring-test/json'), coder='json')
    def cached_method(arg):
        return {"key": "value"}

    assert cached_method(None)['key'] == 'value'

Note that diskcache is backed by sqlite3.

sqlite3 /tmp/ring-test/json/cache.db
SQLite version 3.32.3 2020-06-18 14:16:19
sqlite> select * from Cache;
1|tests.test_func_sync.test_json_disk.<locals>.cached_method:None|1|1622008210.13935||1622008210.13935|0||0|1||{"key": "value"}

If you want to read and write from bare json files which are readable as actual json files, ring doesn't have built-in storage for that. You can implement a custom storage within 50~100 lines of code, but its API is not stable yet. If you are interested in this way, check for https://github.com/youknowone/ring/blob/master/ring/func/sync.py#L219-L261 and please feel free to ask anything you need for the work.

elyase commented 3 years ago

thanks a lot!