I still have the issue, I forgot this call (see arrow)
# Model file
def return_cached_dict(update_cached_dict_if_elapsed_time=900):
del(globals()['cached_dict'])
global cached_dict
cached_dict = cache.ram('cached_dict',
lambda: {r[0]: r[1] for r in
db.executesql("""SQL Query""")
},
time_expire=update_cached_dict
_if_elapsed_time)
return_cached_dict(0) # *<-------*
That should had been :
return_cached_dict(900)
When I set it at the proper value, I still have key error...
Richard
On Tue, Feb 18, 2014 at 4:28 PM, Derek <[email protected]> wrote:
> It is clear as day to me, as written here:
>
> Setting time_expire=0 (or a negative value) forces the cached item to be
> refreshed (because the elapsed time since the last save will always be >
> 0), and setting time_expire=None forces retrieval of the cached value,
> regardless of the time elapsed since it was saved (if time_expire is
> alwaysNone, the cached item will effectively never expire).
>
>
> On Tuesday, February 18, 2014 2:05:39 PM UTC-7, Richard wrote:
>
>> @Derek
>>
>> I read that, what still bug me and seems not explicited (and may be cause
>> of my lack of knowledge), is what happen if there is already an instance
>> that use the cache.ram object at the same time I set time_expire to Zero or
>> even worse, if I delete the variable...
>>
>> Richard
>>
>>
>> On Tue, Feb 18, 2014 at 4:02 PM, Richard Vézina <[email protected]>wrote:
>>
>>> Wait!
>>>
>>> I made a mistake... It seems to be solve... Should be the pointer... My
>>> last error was here (in red):
>>>
>>> if form.process().accepts:
>>> del(globals()['cached_dict'])
>>> return_cached_dict(900)
>>>
>>> Should be 0
>>>
>>> Also move the del in my factory :
>>>
>>> def return_cached_dict(update_cached_dict_if_elapsed_time=900):
>>> del(globals()['cached_dict'])
>>> global cached_dict
>>> cached_dict = cache.ram('cached_dict',
>>> lambda: {r[0]: r[1] for r in
>>> db.executesql("""SQL Query
>>> """)
>>> },
>>> time_expire=update_cached_dict
>>> _if_elapsed_time)
>>>
>>> Then :
>>>
>>> if form.process().accepts:
>>> return_cached_dict(0)
>>>
>>>
>>> Richard
>>>
>>>
>>>
>>> On Tue, Feb 18, 2014 at 3:53 PM, Derek <[email protected]> wrote:
>>>
>>>> You are using the 'examples' to make your cache. That's good that you
>>>> are following the examples, but you really should start by reading the book
>>>> first.
>>>>
>>>> http://web2py.com/book/default/chapter/04#cache
>>>>
>>>> It's explained here what's happening and what you can do to invalidate
>>>> the cache and special cases.
>>>>
>>>>
>>>>
>>>> On Monday, February 17, 2014 1:41:29 PM UTC-7, Richard wrote:
>>>>>
>>>>> 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.
>>>>
>>>
>>>
>> --
> 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.
>
--
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.