Right, you are using a ram cache, so you would have to explicitly 
invalidate the cached items if you want to update them. I would not rely on 
the time_expire to handle that for you, unless you are really not too 
concerned about it. The idea of caching is so that expensively computable 
objects, such as database calls or function calls will go faster if the 
result has already been given once.

If you have basic files, those should be cachable by default, and the 
browser would add a If-Not-Modified-Since header. The server can then send 
an updated version of a file or send a 304 (not modified) message. I 
believe all your controllers function results are by default not cached. 
However, things in the "static" folder are cacheable and will correctly 
respond with either a 304 or the updated file.

I would love to see the DAL automatically cache objects, and do something 
similar to what the browser does - IE adding a "modified date" to each 
table, and all queries to that table will say "if modified since (i last 
cached the data)" and if it gets results back, replace the cache and update 
the "modified since" date. I suppose you could even have an 
admin_tablelastmodified table which would store it for each table, so it 
would be a relatively speedy query.

On Friday, July 13, 2012 6:07:55 AM UTC-7, Richard wrote:
>
> Hello,
>
> If you have some concern about when the cached value will be update in 
> order to make sure your users get the latest correct information you can 
> just clear cache where in your code you want that the value to be caching 
> get "re-cache" (or update)
>
> Here how to do it :
>
> "This is a good point. For things that may not change a whole lot, I 
> usually set the time_expires=3600 or even None. Give the key a well-defined 
> name, and then when something changes, run cache.ram.clear(key) so that 
> the next query will get the changes."
>
> Worte by Ross in this thread :
>
> https://groups.google.com/forum/?fromgroups#!topic/web2py/FWS8kstJHCU
>
> So, let say you have this cached value create in models somewhere to make 
> sure they are always available :
>
> # Model
> var_name_dict = cache.ram('var_name_dict',
>     lambda: dict([(r.field1_name,r.field2_name)\
>          for r in db().select(db.table_name.ALL)]),
>     time_expire=3600)
>
> # Controller
> def function_that_request_up_to_date_var_name_dict():
>     cache.ram.clear(var_name_dict)
>     ...
>
> I think you can just call the cached var and it will be recached, but it 
> could be possible that you have to rebuild the cached var once you clear 
> the cache, but I don't think so.
>
> Richard
>
>
> On Thu, Jul 12, 2012 at 11:47 PM, david xiao <[email protected]>wrote:
>
>> Hi, 
>>    I am new to web2py these 2 days, while i am confused about how to 
>> update the cached value when i read the manual, the below section.
>>    as it explains:
>>    *the value "Hello" will be retrieved from the cache, and it will not 
>> be updated with "Goodbye"*.
>>    Does it means the second statement just have set the time_expire, not 
>> update the value with "Goodbye"?
>>    Does it means "Hello" exists still after 5 second set in the first 
>> statement ? 
>>    How to update the cached vaule if want, like to set "Goodbye" to 
>> replace "Hello"?
>>
>> Thanks
>>
>> Note, time_expire is used to compare the current time with the time the 
>> requested object was last saved in the cache. It does not affect future 
>> requests.
>> This enables time_expire to be set dynamically when an object is 
>> requested rather than being fixed when the object is saved. For example:
>> 1 message = cache.ram('message', lambda: 'Hello', time_expire=5)
>>
>> Now, suppose the following call is made 10 seconds after the above call:
>> 2 message = cache.ram('message', lambda: 'Goodbye', time_expire=20)
>>
>> Because time_expire is set to 20 seconds in the second call and only 10 
>> seconds has elapsed since the message was first saved, *the value 
>> "Hello" will be
>> retrieved from the cache, and it will not be updated with "Goodbye"*. 
>> The time_expire value of 5 seconds in the first call has no impact on the 
>> second call.
>>
>
>

Reply via email to