Caching database stuff

Hi I have a fairly large category stucture for this website I’m writing.
So we have top level sections, which have categories and sub-categories
etc for another 2 levels. These are stored in the db for easy admin with
has_many and belongs_to relationships. I also use ferret for the
majority of the searching. When the search results are returned I also
return the number of hits per category aswell. This is all fine and
ferret handles it very quickly. The slowest part of the whole search
process (80%) by far is the database access for finding the various
sub-categories to use in the ferret queries. Since these categories very
rairly change, is it possible to load the whole category stucture into
memory (maybe 1500 different categories in total) when the server is
started? How do i do this? Or how do i setup an admin action that
refreshes these global variables? Where do i store them etc…

Any and all help greatly appreciated.
Regards
Caspar

maybe you could use the backgroundrb plugin

it can run ruby code in socalled “worker classes” (who have access to
your models) in independent threats, therefore running alongside your
rails application and surviving the request-response cycle.

  1. Create a Worker that, on startup, gets all (sub)categories, maybe in
    an instance variable. Those will be available until the Worker is
    deleted/restarted. (unless my lack of ruby knowledge lead to an error
    of thinking here)
  2. add a method to the Worker that can return the categories instance
    variable to your rails app.
  3. add a method to the Worker to destroy & rebuild your category list,
    then you can call this method when you add/remove/change a category in
    your application

sounds a bit weird? read on here:

http://backgroundrb.rubyforge.org/

Caspar wrote:

memory (maybe 1500 different categories in total) when the server is
started? How do i do this? Or how do i setup an admin action that
refreshes these global variables? Where do i store them etc…

If you add acts_as_nested_set to your category class you can fetch
all sub-categories of a category in one select. Add memcache and
you’re golden.


We develop, watch us RoR, in numbers too big to ignore.