xbmcswift2 offers a few options for caching and storage to help improve the user experience of your addon. swift offers a simple storage mechanism that allows you to store arbitraty python objects to use between requests.
Warning
The current implementation of xbmcswift2’s storage is very basic and is not thread safe. If your addon does background calls via the context menu and manipulates storages in these backgound threads, you might run into some issues.
All caches/storage objects in xbmcswift2 act like python dictionaries. So to get a cache, simply call the get_storage method.
people = plugin.get_storage('people')
# now we can use people like a regular dict
people['jon'] = 'developer'
people.update({'dave': 'accountant'})
people.items()
# [('jon', 'deveoper'), ('dave', 'accountant')]
Caches are automatically persisted to disk each time an addon finishes execution. If you would like to sync the cache to disk manually, you can call cache.sync() directly. However, this is not normally necessary.
By default, caches are saved to disk in the pickle format. This is convenient since it can store Python objects. However, you can also pass ‘csv’ or ‘json’ for the file_format keyword arg to the get_storage call.
Caches also offer an optional argument, TTL, which is the max lifetime for objects specified in minutes.
people = plugin.get_storage('people', TTL=24)
xbmcswift2 provides a convenient caching decorator to automatically cache the output of a function. For example, suppose we have a function get_api_data, that goes out to a remote API and fetches lots of data. If the website only updates the API once a day, it doesn’t make sense to make this request every time the addon is run. So we can use the caching decorator with a TTL argument.
@plugin.cached(TTL=60*24)
def get_api_data();
# make remote request
data = get_remote_data()
return data
The default TTL is 1 day if not provided.
It’s also possible to cache views (functions decorated with plugin.route()). To simplify addon code, there is a special decorator called cached_route. All of the arguments to cached_route are the same as the regular route decorator. Currently, it is not possible to specify a TTL for this decorator; it defaults to 24 hours.
@plugin.cached_route('/')
def main_menu();
# do stuff
Warning
This is only currently possible for views that return lists of dictionaries. If you call plugin.finish() you cannot currently cache the view. See the below section ‘Caveats’ for more information.
Warning
It is currently only possible to attach a single cached_route to a view. If you have multiple routes on a given view, try refactoring some logic out to a new function that can be cached, instead of using the cached_route decorator.
The caching features of xbmcswift2 are still young and thus have some potential problems to be aware of.
cache = plugin.get_cache('people')
cache.clear()
cache.sync()