It's not clear what you are trying to achieve. If this code is in a model 
file, then "id_represent" will never be in globals() before it is called 
(each request is executed in an isolated, ephemeral environment, so 
subsequent requests will not see the "id_represent" from previous 
requests). Also, why define "id_represent" as global and assign it within 
the function rather than simply returning it and assigning it in the model 
when calling the function (which will make it globally available in any 
controller or view)?

In other words, why not just use the cache as usual:

id_represent = cache.redis('id_represent',
                           lambda: db().select(db.mytable.id, db.mytable.
field).as_dict(),
                           time_expire=expiration)

Or just cache the select directly:

id_represent = db().select(db.mytable.id, db.mytable.field,
                           cache=(cache.redis, expiration), cacheable=True)

In the latter case, lookups might be a little slower because you'll have to 
us the rows.find method, which will scan the records, but you should do 
some profiling to see if this is really an issue for your use case.

Anthony

On Friday, January 15, 2016 at 9:21:16 AM UTC-5, Richard wrote:
>
> I ever use cache.ram... Function and function call is in models, so 
> variable is accessible from every controller files...
>
>
>
> With Redis, this work :
>
> from gluon.contrib.redis_cache import RedisCache
> cache.redis = RedisCache('localhost:6379', db=None, debug=True, 
> with_lock=False, password=None)
>
>
> def redis_set_id_represent(update_id_represent_if_elapsed_time=None, 
> update_cache=None):
>     """
>     Calling this function will create in globals the "id_represent" 
> variable. If "id_represent" already
>     present in globals() we do nothing. To update redis.cache key 
> "id_represent" the whole dictionary has to be
>     recreate, to do so, someone should call this function with 
> "update_chache=True".
>
>     In module, to avoid cached dictionary to be recreated all the time 
> this function should be call without passing
>     "update_cache" parameter or by passing "update_cache=False".
>
>     :param update_cache:
>     :param update_id_represent_if_elapsed_time:
>     """
>
>     if 'id_represent' not in globals() or update_cache is not None:
>
>         # print 'id_represent not in globals() or init_cache'
>
>         # Clear Redis Key
>         cache.redis.clear(regex='id_represent')
>
>         # Delete dictionary from global
>         if 'id_represent' in globals():
>             del globals()['id_represent']
>
>         # Query the database
>         id_represent_query = \
>             db().select(db.table_name.id,
>                               db.table_name.represent_field,
>                               orderby=db.table_name.represent_field)
>
>         # Create New global and assign Redis Cache variable
>         global id_represent
>         id_represent = cache.redis('id_represent',
>                                           lambda: {r[0]: r[1] for r in 
> id_represent_query},
>                                           
> time_expire=update_id_represent_if_elapsed_time)
>
> redis_set_id_represent(update_id_represent_if_elapsed_time=None)
>
> Richard
>
> On Fri, Jan 15, 2016 at 12:58 AM, Anthony <abasta...@gmail.com> wrote:
>
>> If I don't use cache.ram dict123 is in globals() and function return the 
>>> else: part of the function...
>>>
>>
>> What do you mean by "if I don't use cache.ram"? Are you saying you are 
>> using cache.disk, or no cache at all? If the latter, how is dict123 in 
>> globals() (i.e., where do you define it)?
>>
>> Anthony
>>
>> -- 
>> Resources:
>> - http://web2py.com
>> - http://web2py.com/book (Documentation)
>> - http://github.com/web2py/web2py (Source code)
>> - https://code.google.com/p/web2py/issues/list (Report Issues)
>> --- 
>> 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 web2py+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to