Creating a cache in rails

I need to create a small hash of values that persists across requests in
Rails. I cannot store this information in the database or filesystem
and only want to do it in memory. I was looking for a simple solution
to implement this and read somewhere that memcache is pretty much
overkill. This app is only running on one server so putting in memory
should be just fine. I’m very new to rails and ruby and have attempted
to implement a class variable like @@cache in my controller and that
didn’t work. I’ve also attempted to create a singleton class (include
Singleton) outside of the controller and that didn’t work either.
Here is the singleton class that I created to cache the information.
Any help would be appreciated. Thanks.

class BlahMemStore
include Singleton

def initialize()
@blah_by_user_id = {}
end

def add(blah_data, user_id)
@blah_by_user_id[user_id] = blah_data
end

def remove(user_id)
@blah_by_user_id[user_id] = nil
end

def find(user_id)
return @blah_by_user_id[user_id]
end

end

Any help would be appreciated. Thanks.
This would work as long as you have a single app server. As soon as
you
have two or more then they won’t be able to share this information and
you
have syncing problems… which I’m guessing from below is going to
matter
to you (for a cached list of states say it wouldn’t).

Memcache isn’t hard to setup and does exactly what you want – but keep
in
mind it’s not persistant (meaning if it fills up, it will remove old
entries).

Why can’t you use the database or /tmp files?

Memcache isn’t hard to setup and does exactly what you want – but keep
in
mind it’s not persistant (meaning if it fills up, it will remove old
entries).

Why can’t you use the database or /tmp files?

The code I posted is running in a single app server and isn’t working

By single app server do you mean you have one mongrel process (or
equivelant) or one physical server?

for me for some reason. We are taking credit card information and don’t
want to store it anywhere other than memory. Thanks.

memcache is ram only…

Philip H. wrote:

Any help would be appreciated. Thanks.
This would work as long as you have a single app server. As soon as
you
have two or more then they won’t be able to share this information and
you
have syncing problems… which I’m guessing from below is going to
matter
to you (for a cached list of states say it wouldn’t).

Memcache isn’t hard to setup and does exactly what you want – but keep
in
mind it’s not persistant (meaning if it fills up, it will remove old
entries).

Why can’t you use the database or /tmp files?

The code I posted is running in a single app server and isn’t working
for me for some reason. We are taking credit card information and don’t
want to store it anywhere other than memory. Thanks.

On 12/12/06, Philip H. [email protected] wrote:

equivelant) or one physical server?

for me for some reason. We are taking credit card information and don’t
want to store it anywhere other than memory. Thanks.

memcache is ram only…

There is this guy who had exactly the same problem and He came up with:

http://boogaloo.rubyforge.org/doc/

with drb its trivial to roll your own caching mechanism.


There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.