I developed a widget pattern that resembles the Auth class. It returns a dictionary when you create at the controller level. During the first creation of the widget (which is in a module) it builds structure and passes it out in a dict() to make the page. Later when Ajax is called it bypasses the creation of the structure and just goes straight to the bit that returns json.
I guess I could cache it at the controller level. But the incoming url is like "scheme://app/control/func/subfunc?vars...". So I guess I'd parse it at the controller level and then apply caching only for "func/subfunc?vars..." which is the json-returning part. I've not really used cache.action much and it has a ton of options. But maybe I should bite the bullet and plan on using something like memcache or redis for this. I realized that these json call backs are ideal to cache because their data is (a) relatively large (~50k), (b) relatively costly to get (~300ms), and (c) unchanging for all time and all users. They depend only on the supplied vars for uniqueness. When I cache them the app really pops! -- Joe On Wednesday, August 1, 2018 at 11:28:39 AM UTC-7, Anthony wrote: > > By the way, why do you want to use cache.action for a non-action? Even if > this is an Ajax request, can't you still decorate the action that receives > the request? If you only want to cache part of the generated response, then > why not just use current.cache.ram, etc.? > > Anthony > > On Wednesday, August 1, 2018 at 11:17:55 AM UTC-4, Anthony wrote: >> >> Sorry, didn't realize you were trying to decorate a method. cache.action >> is designed to cache controller actions, so it is not expecting to decorate >> any functions that take arguments (methods always take at least the self >> argument). >> >> On Wednesday, August 1, 2018 at 4:27:02 AM UTC-4, Joe Barnhart wrote: >>> >>> Ah, well. It seems a bit beyond me. I think it's failing because I'm >>> caching a bound instance method (i.e. has "self"). I get "wrapped+_f takes >>> no arguments, 1 given)". >>> >>> def lazy_cache_action(time_expire=DEFAULT_TIME_EXPIRE, cache_model=None, >>> prefix=None, session=False, vars=True, lang=True, >>> user_agent=False, public=True, valid_statuses=None, >>> quick=None): >>> def decorator(f, time_expire=time_expire, cache_model=cache_model, >>> prefix=prefix, >>> session=session, vars=vars, lang=lang, >>> user_agent=user_agent, public=public, >>> valid_statuses=valid_statuses, quick=quick): >>> def g(*c, **d): >>> from gluon import current >>> return current.cache.action(time_expire, cache_model, >>> prefix, session, >>> vars, lang, user_agent, public, valid_statuses, >>> quick)(f)(*c, **d) >>> g.__name__ = f.__name__ >>> return g >>> return decorator >>> >>> >>> >>> >>> On Wednesday, August 1, 2018 at 12:50:12 AM UTC-7, Joe Barnhart wrote: >>>> >>>> Oops, I meant of course; >>>> >>>> current.cache.action >>>> >>>> instead of >>>> >>>> current.cache >>>> >>>> >>>> >>>> >>>> On Wednesday, August 1, 2018 at 12:48:00 AM UTC-7, Joe Barnhart wrote: >>>>> >>>>> You're a fountain of ideas! I missed that one in the book. >>>>> >>>>> I wonder if this would work. Off to go try it... >>>>> >>>>> def lazy_cache_action(self, time_expire=DEFAULT_TIME_EXPIRE, >>>>> cache_model=None, >>>>> prefix=None, session=False, vars=True, lang=True, >>>>> user_agent=False, public=True, valid_statuses=None, >>>>> quick=None): >>>>> def decorator(f, time_expire, cache_model, prefix, session, vars, >>>>> lang, >>>>> user_agent, public, valid_statuses, quick): >>>>> def g(*c, **d): >>>>> from gluon import current >>>>> return current.cache(f, time_expire, cache_model, prefix, >>>>> session, vars, >>>>> lang, user_agent, public, valid_statuses, >>>>> quick)(f)(*c, **d) >>>>> g.__name__ = f.__name__ >>>>> return g >>>>> return decorator >>>>> >>>>> >>>>> >>>>> On Tuesday, July 31, 2018 at 11:25:10 AM UTC-7, Anthony wrote: >>>>>> >>>>>> On Tuesday, July 31, 2018 at 1:57:46 AM UTC-4, Joe Barnhart wrote: >>>>>>> >>>>>>> I was wondering about this. I tried to search the group but didn't >>>>>>> find anything relevant. Took a look at the source code and it seemed >>>>>>> like >>>>>>> I could use in a module which is called to produce a string of >>>>>>> Javascript >>>>>>> on demand of an Ajax routine. >>>>>>> >>>>>>> Beforehand, I save the global "cache" var in my "current" object. >>>>>>> Then I rename my method "content" to "__content__", and last I do this: >>>>>>> >>>>>>> def content(self): >>>>>>> c = current.cache >>>>>>> return c.action(cache_model=c.disk, >>>>>>> quick="VP")(self.__content__)() >>>>>>> >>>>>>> Seems to work. Am I asking for trouble? Is there anything I should >>>>>>> watch for? >>>>>>> >>>>>> >>>>>> Seems reasonable. You could also create a custom decorator, similar >>>>>> to lazy_cache >>>>>> <https://github.com/web2py/web2py/blob/master/gluon/cache.py#L728-L746> >>>>>> (see the end of this section: >>>>>> http://web2py.com/books/default/chapter/29/04/the-core#Warning--Do-not-use-the-current-object-in-global-scope-in-a-module >>>>>> ). >>>>>> >>>>>> 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.