this is one of the design patterns of the cache in web2py that makes a
little more cumbersome than others cache interface to understand ...
assuming you're using cache.ram, when you do
cache.ram(key, value, 60)
for the first time what you're really doing is "store value, tag it with
key and 60 seconds of expiration"
the "difference" from normal caches lies in the fact that cache.ram never
deletes any value stored.
Normal caches more or less constantly checks for item marked as "expiring"
and deletes the ones that are "yet expired"
cache.ram instead just "plays" with the time_expire parameter to understand
if it has to return the cached value or if it has to store the new value.
cache.ram(key,value, 60) the second time will ask for a value stored at
key, and cache will replace the stored value if the expiration is passed.
Again, cache.ram(key, value, 60) does not "erase" from the cache the value
stored at "key" after 60 seconds.
It just looks for 'key', and returns the cached value if 60 seconds are not
passed yet, while caches the value and returns it if the 60 seconds are
passed.
Try it yourself
cache.ram('key', lambda: 'first_one', time_expire=5)
time.sleep(10)
cache.ram('key', lambda: 'second_one', time_expire=60)
You'll see that the second call returns 'first_one' .
So, to be sure to retrieve the cached value no matter what, supply a
time_expire=None ... to replace the value always, supply time_expire=0
BTW, 2. is the perfect candidate to be managed outside web2py, not relying
on cache.ram entirely.....
caching something that may or may be not returning something useful is a
job for a task that "replaces" the stale object(s) only if something
meaningful is calculated.
So, cache.ram can check for the value currently stored at 'key', but you
instead would like to:
- find the cached value at 'key'
- if 1 day is passed, then replace the value at 'key' with the output of
myfunction()
- but (a large one) if myfunction returns None, then restore the original
value at 'key'
either you use a different set of keys (one to check if the expiration
passed, the other one to store the actual value, the other one to store the
last meaningful value) or you change your logic ....
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.