In general, the approach David suggests
(https://groups.google.com/d/msg/web2py/4cBbis_B1i0/KkKnNwUw8lcJ) is
probably preferable, but below are answers to your questions...
user_data = cache.ram('user_data', lambda:dict(), time_expire=None)
>
> # add the data from this user, this should also update the cached dict?
> user_data[this_user_id] = submitted_data
>
The above would not update the dict in the cache -- you'd have to do that
explicitly:
cache.ram('user_data', lambda: user_data, time_expire=[some positive value])
The above implies that the 'if' check is made upon every request by
> individual users, which is somewhat inefficient, but I am not sure if there
> is other better way(s) to implement this logic within web2py.
>
You're already reading the object from the cache on each request --
checking the length should not be a big deal. Note, it doesn't necessarily
have to be on every request -- just requests that deal with this aspect of
the application (i.e., only in the relevant controller function).
> 1. when exactly does an object expire in cache? My understanding is that,
> as long as the 'time_expire' value of *the current request* is greater
> than the gap between *the time of current request* and *the time at which
> the requested object was last updated in cache*, then the object behaves
> as if it never expires in cache. This is illustrated by an example in the
> book, if I understand it correctly. Therefore, the only perceivable way (to
> me) for an object to expire is to supply a 'time_expire' value in a request
> that is smaller than the gap between the time of this request and the time
> when the requested object was updated in cache. In other words, if I do:
>
> something = cache.ram('something', lambda:'blah blah', time_expire=1)
>
> effectively, 'something' never expires in cache until I call something
> like the following after t seconds (with t > make_it_expire):
>
> something = cache.ram('something', lambda:'blah blah',
> time_expire=make_it_expire
> )
>
Yes, that's how it works.
> 2. An follow-up question is that, when an objects eventually expires in
> cache, does it mean it is completely removed from the cache? i.e.
> equivalent to:
>
> cache.ram(key, None)
>
> or something else happens?
>
No, expiring simply means that it gets updated with the new value instead
of the existing value being retrieved. So, in your first example, if
make_it_expire is greater than the elapsed time since the object was last
saved, the "something" key would not be deleted from the cache -- rather,
the value associated with the "something" key would be changed to "blah
blah" (which in this case happens to be the same as the old value, but you
get the idea).
Perhaps "time_expire" isn't the best name, as it may imply that the object
will be expunged after some elapsed expiration time. Maybe something like
"update_if_elapsed_time" would be more descriptive.
> 3. The manual says that by setting 'time_expire = 0', it forces the cached
> object to be refreshed. Am I correct in understanding this as the cached
> object will be immediately set to whatever is supplied by the second
> argument of the 'cache.ram' call? Thus 'refreshed' here means 'updated'.
>
Yes.
Anthony