Hello,
I have an issue with app design and global variable cached python dict...
I use web2py 2.4.7
I use to create cached python dict in models like so :
update_cached_dict_if_elapsed_time = 900
cached_dict = cache.ram('cached_dict',
lambda: {r[0]: r[1] for r in
db.executesql("""...""")},
time_expire=update_cached_dict_if_elapsed_time)
cached_dict is a for representation of a given table ids... It intent to
reduce the number of requests that hit the database by using python dict as
much as possible...
But I got in trouble trying to update this dict with
update_cached_dictt_if_elapsed_time... First as cached_dict this variable
is in globals, so I use to add more logic around the
update_cached_dict_if_elapsed_time var like so :
update_cached_dict_if_elapsed_time = None
if update_cached_dict_if_elapsed_time == 0:
pass
elif request.args(0) == 'table_name' and request.function == 'create' and
request.post_vars:
update_cached_dict_if_elapsed_time = 0
I find this cumbersome and prone to error because if I have other thing
that should trigger a dict update, I have to add more logic...
So, I though I could make factory dict function like so :
def factory_cached_dict(update_cached_dict_if_elapsed_time=900):
global
cached_dict
cached_dict = cache.ram('cached_dict',
lambda: {r[0]: r[1] for r in
db.executesql("""...""")},
time_expire=update_cached_dict_if_elapsed_time)
factory_cached_dict(900)
Above is in models...
Then I though I could just update my global cached_dict var from any
controller like so :
if form.process().accepts:
factory_cached_dict(0) # Because in this form I just add a value to
the table use to create the python dict
But I am really desapointed it not seems to work because I still experiment
issue with not up to date cached_dict because there is missing key value
pairs for id and it representation.
I think the issue is that even if I call the factory after form submit the
dict get recreated at each request by the factory call in models or there
is something else going on that I don't understand (maybe issue with cache
in web2py in web2py 2.4.7??)
Thanks for any help.
Richard
--
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/groups/opt_out.