cache.ram behaves a little different from other caching systems (with redis 
or memcache you find the "usual" cache algorithm).
Cache.ram currently behaves storing indefinitely your object, and retrieves 
(or updates) the key based on time_expire (that's why it's recommended to 
use a finite set of keys to not let it grow arbitrarily)

The example explains the circumstance quite at his best possibility.
Normal cache:
- at 00:00:00 --> cache.redis('message', lambda : 'Hello', time_expire=5) = 
'Hello'
- at 00:00:05 --> redis "auto-deletes" the key
- at 00:00:10 --> cache.redis('message', lambda : 'Goodbye', 
time_expire=20) = 'Goodbye' because 'message' is not there anymore
- at 00:00:30 --> redis "auto-deletes" the key
Cache.ram:
- at 00:00:00 --> cache.ram('message', lambda : 'Hello', time_expire=5) = 
'Hello'
- at 00:00:05 --> nothing happens
- at 00:00:10 --> cache.ram('message', lambda : 'Goodbye', time_expire=20) 
= 'Hello' because 20 seconds are not yet passed
- at 00:00:30 --> cache.ram('message', lambda : 'Goodbye', time_expire=20) 
= 'Goodbye'

This has historical reasons (and in future version we may have a cache.ram 
behaving like a normal cache)....
The exact behaviour is the following: cache.ram never deletes a key, once 
you set it, you can update it or fetch its current value.
Explaining better:
- At some point in time, let's call it 00:00:00 (hours:minutes:seconds), 
you cache with the key "message" the function that returns "Hello" for 5 
seconds.
- You might expect that retrieving its value at 00:00:10 -  from a "normal" 
cache algo - that the key is expired (hence, "removed"), and you get back 
"Goodbye".
- But (and here is the difference), if you call "manage" with a time_expire 
of 20 seconds, also if you're willing to retrieve "Goodbye", you'll get 
back "Hello" because you are basically saying "fetch the cached value at 
key 'message' and update it's value only if 20 seconds are passed since the 
time it was last set". --> translated it's :
   - 'Hello' was set on 00:00:00
   - now that are 00:00:10, has 'message' been set more than 20 seconds ago 
?
   - no, so let's not update its value, fetch the current 'Hello' value and 
return that

Rewritten in another way:
cache.ram behaves like a normal "expiring" cache only if the cache_key and 
the expire_time are the same: you cache 'Hello' with 5 seconds, you require 
'goodbye' with 5 seconds, and at 00:00:10 you get "goodbye" back.
If expire_time is different between calls, your keys are updated only if 
time_expire seconds (20) are passed between the moment you require the key 
back (00:00:10) and the time it was stored (00:00:00). If they are not 
passed, the original value is returned back.

-- 



Reply via email to