purpose is to keep a single renewable pseudo-random data sample live
the w2p books describes a situation of a constant query :
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=caching+selects#Caching-selects
def cache_db_select():
logs = db().select(db.log.ALL, cache=(cache.ram, 60))
return dict(logs=logs)
now suppose every call a different query like here :
def cache_this():
nget = somevalue
kount = db(db.atable.id > 0).count()
offset = randint(0, kount - nget)
limitby = (offset, offset + nget)
rows = db(db.atable.id > 0).select(limitby=limitby, cache=(cache.redis, 60))
return dict(rows=rows)
this accumulates cached 'material' and returns a different rows on every call
how do I make cache_this behave like the book example ?
I could cache kount and offset variables as well :
kount = db(db.atable.id > 0).count(cache=(cache.redis, 60))
offset = cache.redis('offset', lambda: randint(0, kount-nget), time_expire=60)
but i suppose this would lead to a synchronization problem and thus possible duplicate cached select
def cache_db_select():
logs = db().select(db.log.ALL, cache=(cache.ram, 60))
return dict(logs=logs)
@cache(some_key, time_expire=60, cache_model=cache.ram)
def cache_this():
...
rows = db(db.atable.id > 0).select(..., cacheable=True)
return dict(rows=rows)
the cache select mechanism is full of mystery:
given book example code:
def cache_db_select():
logs = db().select(db.log.ALL, cache=(cache.ram, 60))
return dict(logs=logs)
what happens to next cache_db_select call after the 60 seconds has elapsed ?
Does it overwrite the previously cached rows ?
One need to know a key in order to clear a specific cached value associated with that key. When performing a cache select no key is being passed so how to clear a specific cached select item (for instance logs) ?
this gives me nerve crises........It should be simple to do a simple thing however one week digging this and I didn't move 1 inch ahead
from gluon.cache import lazy_cache
@lazy_cache('data_sample', time_expire=60, cache_model='redis')
def cache_this():
...
rows = db(db.atable.id > 0).select(limitby=limitby, cacheable=True)
return rows
actually the real function is a little more complex since it returns rows plus other datas that don't require caching. I tried to move the cache_this function to a module (don't know if this is orthodox) but then i get a decorator error:
@current.cache('data_sample', time_expire=60, cache_model=current.cache.redis)
attributeError cache object has not attribute redis
I'd prefer the module option but I need to pass time_expire as a variable (db.atable.__variableName), I don't think I can do that in a module can I ?
from mymodule import cache_this
myrows = cache.redis('myrows', cache_this, time_expire=myvariable)