[web2py] Re: Caching Logic in web2py

2015-05-04 Thread Chris Modzelewski
Awesome! Thank you very much for this explanation!

All the best,
Chris

On Tuesday, April 28, 2015 at 6:22:49 PM UTC-4, Leonel Câmara wrote:

 This one:

 A. Does the cached object get updated:

 The cached object is updated immediately because it's the same object. 
 Cache ram does not clone it. Notice that the behaviour will be different if 
 you're using cache disk.

 Do know that doing this is a very bad idea, as you will run into all sorts 
 of thread safety problems. You should consider cached values immutable if 
 you don't want to worry about safety yourself.


 And finally, if I wish to force a changed value of some_object to be saved 
 to the cache, am I correct that I should then call:
 some_object.arg3 = 
 some_object = cache.ram(self.id_attribute, lambda self, time=0)


 This will force the change, but, again, if it's the same object the change 
 will already be there.

  Does this mean that the next time someone instantiates an object of 
 class SomeClass with the same arg1 value but a different time_expire 
 value, the cached object will be overwritten?

 It will be overwritten if the cached value has a timestamp that is expired 
 with the given time_expire value. 
   


 I think that maybe your problem is that you're not understanding what the 
 cache is actually saving in your example. When you put an object in cache 
 ram, what's actually in the cache is a reference to the object. So if you 
 have a reference to that same object somewhere else and you modify it, 
 you're actually modifying the same object that cache is referencing.

 Cache disk is different, because cache disk pickles your object and when 
 you ask it for a value it unpickles it, so each time you will get a new 
 object albeit with the same attributes.


-- 
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.


[web2py] Caching Logic in web2py

2015-04-28 Thread Chris Modzelewski
Hi Everyone,

I have some questions about how caching works in web2py. I'm not interested 
at this point in understanding how to cache entire views or anything like 
that - in this case, I'm interested in understanding the principles behind 
how web2py implements caching:

Let's say that I have a class that accepts some arguments that is defined 
like so:

class SomeClass():
def __init__(self, arg1 = None, arg2 = None, arg3 = None):
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3

if arg1 is not None:
self.id_attribute = 'ID Value Here' + arg1
self = cache.ram(self.id_attribute, lambda self, time=(60*60*24*
30))

return self

Now the *first* time I instantiate an object of SomeClass, say by calling:
some_object = SomeClass(arg1=123, arg2=456, arg3=789)

Then this will create an object of class SomeClass, and (because arg1 is 
not None) will save that object to the cache (in ram) with the following 
attributes:
some_object.arg1 == 123
some_object.arg2 == 456
some_object.arg3 == 789
some_object.id_attribute == 'ID Value Here123'

Is the above so far correct?

If so, then what happens if I perform an operation on some_object, like so:

some_object.arg2 = 654

In this case:

A. Does the cached object get updated:

   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?
   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?

or:

B. The local object gets updated, but the cached value does not change:

   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?
   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?

or: C. Something else?

And then what happens when I have some other function try to do something 
using some_object?

some_other_value = some_object.arg2 + 987

Specifically, does the value of some_object.arg2 get read from the cache or 
from the object itself:

   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?
   - if this statement gets executed  (60*60*24*30) seconds from when the 
   object was instantiated?

And finally, if I wish to force a changed value of some_object to be saved 
to the cache, am I correct that I should then call:
some_object.arg3 = 
some_object = cache.ram(self.id_attribute, lambda self, time=0)

which will explicitly change the cached version of some_object? Does this 
mean that the next time someone instantiates an object of class SomeClass 
with the same arg1 value but a different time_expire value, the cached 
object will be overwritten?

Any help / clarification / explanation would be much appreciated!

Thanks in advance,
Chris

-- 
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.