First, http://pylonshq.com/docs/en/1.0/caching/ has been updated and is a bit more detailed than some of the earlier documentation. http://beaker.groovie.org/caching.html is a good reference as well.
On May 26, 9:26 pm, writeson <[email protected]> wrote: > 1) I''d like the cache to expire after X number of minutes, this is > no problem using beaker_cache, but I'd also like to invalidate the > cache if the underlying data is modified by an AJAX/JSON call, say > from a browser form action. I've seen that beaker_cache has an > invalidate method, but I'm not sure how best to use it and still get > effective use out of the cache. Any recommendations? That's a write-through cache. Invalidating or storing the new cached query results when the resultset is updated is a fairly good way to do it. With the decorator, I believe you need to use invalidate, and the next time the decorated function is called, the result is cached. If you are calling the cache through the API rather than the decorators, you can do a write-through. Caching is the easy part. Intelligently purging/invalidating becomes difficult as you get to a multiple caches and multiple servers hosting your application. Some caching strategies are to use a separate memcached server based on a hash so that you know your cached data exists in only one place. Of course, if memcached is restarted, your backend needs to be ready to rebuild that cache from a cold start. That can sometimes cause problems with your app. > 2) I'm running my Pylons application under uwsgi, so there will be > multiple "processes" that are answering requests from multiple users. > If multiple users are looking at the same pages, I'd like to feed > those pages with the same cached data. What's a good way to handle > this? Should the cache be stored in the "dbm" backend so it's > available to all processes? Any other hints? memcached may work better in your instance since each process would have access to the same cache. I am not 100% sure whether uwsgi processes share memory between them or if they are isolated processes in terms of the in-memory caching that beaker offers. Otherwise, depending on the amount of data you need to cache, in-memory would probably work pretty well. Since you're using uwsgi, I'm guessing you're using nginx. Nginx does have a very unique feature that allows it to serve results right from memcached. Since you are generating json data, you could conceivably write the json data to memcached and have nginx serve it straight from memcached without ever hitting Pylons/uwsgi. I am not a real fan of using rotational media for storing cache data, but, since it is just a key-value store, you could get away with it if you are consolidating expensive queries to a simple result set. A database or file backed cache would still be less CPU than running an expensive query. > 3) In the beaker_cache invalidate method there is a "key" parameter > that is used (I think) to uniquely identify the cache item. What's a > good way to generate this key? In my class I'm trying to generate a > key based on the function name and the parameters it's passed so that > calls to the same function, but with different parameters (model > calls), will get cached as separate items. Is this a good idea, and if > so, am I going about it in an intelligent way? MySQL's querycache does lower(query) and uses that as the unique key. Whether that is a good strategy or not is left for debate, but, the same query with arguments swapped, is two different cache items. So, you might want to enforce some consistency with your key if you are passing the arguments and values into your key. If I recall, you need to use positional arguments when using the beaker decorator which partially addresses this. However, if you are passing a dictionary, I don't believe it sorts the dictionary when generating the key. So, a functionally identical dictionary with arguments in a different order would end up being two cache entries. -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
