The example was a bit simplified. In the real process I fetch data from the database, calculate scores and rankings and then return the result. So it's not just for database queries, but all calculations.
The reason I'm using cache_property is that many methods in the class use the same data, so if I call "classinstance.participant_ranking" and "classinstance.all_participant_rankings", they both use the same cached data in the class (you cannot calculate ranking of one participant without calculating it for all). Otherwise I would need to manually check if the query and calculation has been already made if I don't want to duplicate the calculation. I've understood that caches (cache.ram, cache.disk, memcache and redis) are server level caches. So if my data changes per user, I cannot really use them(?) Ykä On Friday, August 16, 2013 12:39:20 PM UTC+3, Niphlod wrote: > > That's a good way to eat up memory in the python process. I would have > saved a lot of headaches and just used select() with cache, possibly > exploiting memcache or redis to do the hard job. Don't take this the wrong > way, the cached_property is a neat trick, it's just not that suitable for > storing your zillions participant when you'll grow big. > > On Friday, August 16, 2013 10:25:32 AM UTC+2, Ykä Marjanen wrote: >> >> Hi, >> >> After learning Python and web2py deeper, I've restructured my web2py >> application so that all functions and data are now in classes with lazy >> methods in my own module. The lazy property class turns any method into a >> "cached attribute", thus I can make a db query that returns rows and if it >> is called again it uses the previous result. >> >> What this has allowed me to do, is to clean practically all code in my >> controller as the logic and variables are in a class. Now I just pass the >> class to the view and the view prints the "attributes" directly without >> separate call and saving to dict. I've read that some don't like any logic >> in the view, but that's unavoidable as in the view you need to print stuff >> depending who the user is and is he logged in or not. >> >> I'd like your opinions and comments on my strategy. Here are few >> simplified examples from my code: >> >> Module: >> >> class MyClass(object): >> def __init__(self): >> self.db = current.db >> >> @cached_property >> def all_participants(self): >> return self.db(self.db.participant.id>0).select() >> >> class MyClassParticipant(MyClass): >> "Inherits base class" >> def __init__(self, auth_user_id=None): >> self.auth_user_id = auth_user_id >> >> super(MyClassParticipant, self).__init__() >> >> @cached_property >> def participant_stats(self): >> return >> self.db(self.db.participant.auth_user_id==self.auth_user_id).select() >> >> Controller: >> >> def index(): >> if auth.user: >> xclassinstance = >> module.MyClassParticipant(auth_user_id=auth.user_id) >> else: >> xclassinstance = module.MyClass() >> >> return dict(xclassinstance=xclassinstance) >> >> View: >> >> {{if auth.user:}} >> Your ranking is {{=xclassinstance.participant_stats.ranking}} >> >> Rankings of all participants >> <br /> >> {{for participant in xclassinstance.all_participants:}} >> {{=participant.name}} ranking is {{=participant.ranking}} >> <br /> >> >> Ykä >> >> >> -- --- 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/groups/opt_out.