astrXbian/.install/.kodi/addons/script.module.simplecache
qo-op e3a31c4111 .kodi default 2020-12-17 21:52:17 +01:00
..
lib .kodi default 2020-12-17 21:52:17 +01:00
resources .kodi default 2020-12-17 21:52:17 +01:00
LICENSE .kodi default 2020-12-17 21:52:17 +01:00
README.md .kodi default 2020-12-17 21:52:17 +01:00
addon.xml .kodi default 2020-12-17 21:52:17 +01:00

README.md

script.module.simplecache

Codacy Badge

A simple object cache for Kodi addons

Help needed with maintaining !

I am very busy currently so I do not have a lot of time to work on this project or watch the forums. Be aware that this is a community driven project, so feel free to submit PR's yourself to improve the code and/or help others with support on the forums etc. If you're willing to really participate in the development, please contact me so I can give you write access to the repo. I do my best to maintain the project every once in a while, when I have some spare time left. Thanks for understanding!

Usage

You can use this python library as module within your own Kodi scripts/addons. Just make sure to import it within your addon.xml:

<requires>
    <import addon="script.module.simplecache" version="1.0.14" />
</requires>

Now, to use it in your Kodi addon/script, make sure to import it and you can access its methods.

import simplecache

# instantiate the cache
_cache = simplecache.SimpleCache()

# get data from cache
mycache = _cache.get("MyAddon.MyChunkOfData")
if mycache:
    my_objects = mycache
else:
    # do stuff here
    my_objects = mymethod()

    # write results in cache
    _cache.set( "MyAddon.MyChunkOfData", my_objects, expiration=datetime.timedelta(hours=12))

The above example will check the cache for the key "MyAddon.MyChunkOfData". If there is any data (and the cache is not expired) it will be returned as the original object.

If the cache is empty, you perform the usual stuff to get the data and save that to the cache


Available methods

get(endpoint, checksum="", json=False)

    Returns the data from the cache for the specified endpoint. Will return None if there is no cache.

    parameters:
    endpoint --> Your unique reference/key for the cache object. TIP: To prevent clashes with other addons, prefix with your addon ID.
    checksum --> Optional argument to check for a checksum in the file (Will only work if you store the checksum with the set method). Can be any python object which can be serialized with eval.
    json --> Optional argument. Default is False. For JSON data it is recommended to switch it to True to avoid Memomy Error exceptions or other issues. If you set the global "data_is_json" bool to True, it will always handle your data as JSON.


    Example: _cache.get("MyAddon.MyChunkOfData", checksum=len(myvideos))

    This example will return the data in the cache but only if the length of the list myvideos is the same as whatever is stored as checksum in the cache.

set(endpoint, data, checksum="", expiration=timedelta(days=30), json=False)

    Stores the data in the cache for the specified endpoint.

    parameters:
    endpoint --> Your unique reference/key for the cache object. TIP: To prevent clashes with other addons, prefix with your addon ID.
    data --> Your objectdata. Can be any python object which can be serialized with eval.
    checksum --> Optional argument to store as checksum in the file. Can be any python object which can be serialized with eval.
    expiration --> Optional argument to specify the amount of time the data may be cached as python timedelta object. Defaults to 30 days if ommitted.
    json --> Optional argument. Default is False. For JSON data it is recommended to switch it to True to avoid Memomy Error exceptions or other issues. If you set the global "data_is_json" bool to True, it will always handle your data as JSON.

    Example: _cache.set("MyAddon.MyGreatChunkOfData", my_objects, checksum=len(myvideos), expiration=timedelta(hours=1))

    This example will store the data in the cache which will expire after 1 hours. Additionally a checksum is stored in the cache object.

Notes

  1. By default objects will be stored both in memory and on disk, it is however possible to override that:
    _cache.enable_mem_cache = False

In that case, objects will only be stored on disk (database)

  1. Cache objects are auto cleaned from memory after 2 hours to prevent unused objects loaded in memory.

  2. Cache objects on disk are stored in a self-maintaining sqllite database. Expired objects will be auto cleaned from the database.

  3. If your data is only JSON you can set a global bool to handle all input/ouput requests as JSON. This is recommended to avoid problems and issues on slower devices.

    _cache.data_is_json = True