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.executesql("""SELECT lotns_sample.sample_id,
(lotns_lot_number.lot_number_computed
|| '-') ||
lotns_sample.sample_code AS sample
FROM lotns_sample
JOIN lotns_lot_number
ON lotns_lot_number.id =
lotns_sample.lot_number_id
WHERE NOT (lotns_sample.sample_id IN (
SELECT
lotns_sample_study_mn.sample_id
FROM lotns_sample
JOIN lotns_sample_study_mn
ON
lotns_sample_study_mn.sample_id =
lotns_sample.sample_id))
UNION
SELECT lotns_sample.sample_id,
(((lotns_lot_number.lot_number_computed
|| '-') ||
lotns_sample.sample_code) || '-') ||
lotns_sample_study_mn.study_code AS
sample
FROM lotns_sample
JOIN lotns_lot_number ON lotns_lot_number.id
=
lotns_sample.lot_number_id
JOIN lotns_sample_study_mn
ON lotns_sample_study_mn.sample_id =
lotns_sample.sample_id
ORDER BY 2;""")
# 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 <[email protected]> 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 [email protected].
> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.