Add-on:Common plugin cache
Common plugin cache | ||||||||||
|
Add-ons | Common plugin cache |
Installing
This add-on is installed from the Add-on browser located in Kodi as follows:
Testing/Status
Integration and unittests are run continously by Jenkins
http://tc.tobiasussing.dk/jenkins/view/Cache%20Service/
Developers
This service works as an independant storage server to XBMC's implementation.
It provides proper thread safe locking and get set functions to store data.
Do note that important data should NOT be stored using this service.
Development and support thread: http://forum.xbmc.org/showthread.php?t=116496
Setup
To use the cacheing service edit your addon.xml file like this.
<requires> <import addon="xbmc.python" version="2.0"/> <import addon="script.common.plugin.cache" version="0.9.0"/> # Add this </requires>
And add the following to your py file.
try: import StorageServer except: import storageserverdummy as StorageServer cache = StorageServer.StorageServer() cache.table_name = "PluginName" # Name of your plugin.
You can now access the caching service through the "cache" variable.
Remember to include a fallback storageserverdummy.py file.
Debugging
To enable debugging set the following values in default.py
dbg = True # Default
Or you can change it after import with.
common.dbg = True # Default
Whenever you debug your own code you should also debug in the cache. Otherwise you should remember to DISABLE it.
cacheFunction(self, funct = False, *args)
Caching function results for one day.
The function being cached must return a tupple ( function_return, status), a status of 200 means the result is ok and should be cached. Any other value and the function result will be returned, but not cached.
returns a tupple of the results from the function called.
def pi_count(self): def arccot(x, unity): sum = xpower = unity // x n = 3 sign = -1 while 1: xpower = xpower // (x*x) term = xpower // n if not term: break sum += sign * term sign = -sign n += 2 return sum digits = 40 unity = 10**(digits + 10) pi = 4 * (4*arccot(5, unity) - arccot(239, unity)) return (pi // 10**10, 200) cache.table_name = "PluginName" # Name of your plugin. result = cache.cacheFunction(pi_count) # This will call the pi_count function, save the result in the cache, and return the result. time.sleep(300) result = cache.cacheFunction(pi_count) # This will return the cached result time.sleep(3600) result = cache.cacheFunction(pi_count) # This will again call pi_count since the result is now considered stale.
lock(self, str)
Try to aquire a lock
Returns bool
cache.table_name = "PluginName" locked = cache.lock("lock_me") # Returns True if locked: print "Got lock" new_lock = cache.lock("lock_me") # Returns False if not new_lock: print "Couldn't aquire second lock.
unlock(self, str)
Unlock
Returns bool
cache.table_name = "PluginName" lock = cache.lock("lock_me") # Returns True unlocked = cache.unlock("lock_me") # Returns True if unlocked: print "Discarded lock" new_unlocked = cache.unlock("lock_me") # Returns False if not new_unlocked: print "Couldn't discard lock."
set(self, str, object )
Store a value
Returns nothing
cache.table_name = "PluginName" cache.set("some_string", "string") cache.set("some_int", 1234) cache.set("some_dict", { "our": { "dictionary": [] } } )
setMulti(self, str, dict)
Store multiple values
Returns nothing
cache.table_name = "PluginName" save_data = { "some_string": "string", "some_int": 1234, "some_dict": { "our": { "dictionary": [] } } } cache.setMulti("pre-", save_data)
get(self, str)
Get a value
Returns stored data.
cache.table_name = "PluginName" cache.set("some_string", "string") cache.set("some_int", 1234) cache.set("some_dict", { "our": { "dictionary": [] } } ) some_string = cache.get("some_string") print some_string # Prints "string" some_int = cache.get("some_int") print some_int # prints 1234 some_dict = cache.get("some_dict") print repr(some_dict) # prints '{ "our": { "dictionary": [] } }'
getMulti(self, str, list)
Get multiple values
Retuns list of stored data elements.
cache.table_name = "PluginName" save_data = { "some_string": "string", "some_int": 1234, "some_dict": { "our": { "dictionary": [] } } } cache.setMulti("pre-", save_data) get_list = [ "some_string", "some_int", "some_dict" ] result = cache.getMulti("pre-", get_list) print result[0] # Prints "string" print result[1] # Prints 1234 print repr(result[2]) # Prints '{ "our": { "dictionary": [] } }'