Andrew T. wrote:
On Thu, Apr 9, 2009 at 5:40 PM, Shak Shak
[email protected] wrote:
doesn’t need to (in fact, shouldn’t) last past a single request.
An instance variable in ApplicationController sounds like it would do
this, but I would say such data should be kept with the model. Of course
a class variable in the model will keep its value each request (in
production) so is inappropriate for this.
It may be useful to understand a little more of what you’re trying to
If you’re needing to keep something around for a request, an instance
variable in the controller may the right place because that manages
the the request.
My application often needs a list of application-specific “types”. This
is a relatively static list of AR models. I also create sub lists based
on attributes of those types - so all the ones whose names which begin
with the letter “A”, for instance. Each list is formed from a traversal
of the full list. These sub lists are accessible via the Class -
ApplicationTypes.get_all_a, for example.
To not repeat these traversals, I cache the computed lists. At the
moment, I’m using MemoryStore. Caching AR models there requires a
Marshal due to some odd (but known) behaviour
Taking stuff from the cache requires an unmarshall. Other cache stores
implicitly Marshal anyway.
So to save the repeated unmarshalling, I wanted a way to keep the
results of unmarshalling things from the cache on a per-request basis.
It’s interesting to note that Rails 2.3 has this built in (for remote
caches, anyway). From the release notes:
"5.8 Improved Caching Performance
Rails now keeps a per-request local cache of read from the remote cache
stores, cutting down on unnecessary reads and leading to better site
performance. While this work was originally limited to MemCacheStore, it
is available to any remote store than implements the required methods."
I just want this kind of behaviour for a memorystore backed cache.
At the moment, I’ve just created a local hash of cached objects (created
on retrieval), which I clear using a public function called from an
before_filter in the ApplicationController. I guess this is the MVC way
of doing it too (maybe?). I do wonder how the mechanism in Rails 2.3
works though (since my way isn’t really threadsafe), so any further
ideas would be welcome.