script.module.codequick icon indicating copy to clipboard operation
script.module.codequick copied to clipboard

callback level cache not working

Open dimkroon opened this issue 2 years ago • 0 comments

Hi willforde,

First of all many thanks for your excellent project! I'm using it for a kodi addon I'm developing, but found that callback level cache is not working. From several examples I figured that caching can be enabled by decorating a callback with @Route.register(cache_ttl=60). However, doing so the cache is never hit.

Inspecting the code I think the issue is in route.Route:

def __call__(self, route, args, kwargs):
    cache_ttl = getattr(self, "cache_ttl", -1)
    cache = Cache("listitem_cache.sqlite", cache_ttl * 60) if cache_ttl >= 0 else None

At that point cache_ttl is never an attribute of self (route.Route), so caching will never occur. Only later on, when the super class's (script.Script) __call__(...) is invoked, the attribute is added, but that is too late to enable caching.

Some simple patches in route.Route seem to work:

Obtain cache_ttl from the support.Route object passed to argument route.

def __call__(self, route, args, kwargs):
    cache_ttl = getattr(route, "parameters", {}).get("cache_ttl", -1)
    cache = Cache("listitem_cache.sqlite", cache_ttl * 60) if cache_ttl >= 0 else None

Add support.Route.parameters to self early on, acceptingScript.__call__(...) does the same again:

def __call__(self, route, args, kwargs):
    self.__dict__.update(route.parameters)
    cache_ttl = getattr(self, "cache_ttl", -1)
    cache = Cache("listitem_cache.sqlite", cache_ttl * 60) if cache_ttl >= 0 else None

Right now I'm using the second approach to monkey-patch rout.Route.__call__(...), just because it is easiest, and caching works like charm.

I'm not quite sure what the intended behaviour should be, or if I totally misunderstand the caching feature and doing it all wrong. I would please have a look at it? Many thanks for your help.

dimkroon avatar Aug 16 '22 16:08 dimkroon