Using memcache and file caching at the same time

Hi,

I want to cache some objects (models) in memcache and some objects
(images) in file store. I have successfully configured both
independently. Is it possible to use them both at the same time?

My setup is Apache load balancing to 7 Mongrel instances: 2 on the same
server and 5 on a second server. I’ve configured Apache so that any
writes to the image cache happen on one of the local Mongrels:

If we are going to expire or write to the cache, do it on this

machine
RewriteRule /edit/(.*)$ http://127.0.0.1:1234%{REQUEST_URI} [P,QSA,L]

And got it to check for static files before handing off to Mongrel:

RewriteCond /path/to/my/file/cache/%{REQUEST_FILENAME}.cache !-f
RewriteRule ^/(.*)$ balancer://mongrel_foo%{REQUEST_URI} [P,QSA,L]

Coupled with an AliasMatch directive it works pretty well.

I’m caching the image with an action cache:

caches_action :image

And elsewhere, using memcache, I use write/read fragment manually:

read_fragment(name)
write_fragment(name, Marshall.dump(object), {:ttl => 30.seconds})

I’m not sure how I go about configuring file store AND memcache at the
same time though. Is that possible?

Thanks,
Bill

Nevermind. I didn’t work out how to get this to work, but following
advice from #rubyonrails IRC I decided to simplify things a little :slight_smile: I
now store images on the filesystem and do all my caching using memcache.

Bill,
I have a similar problem.

I am using page caching for my RSS feed.

I think once I run mongrels in a clustered of machines, I’d have
problems with page caching because the page caching can’t be expired on
all machines automatically.

Can you give me a solution for solving this problem?

Thanks.
Yaxm

Bill Horsman wrote:

Nevermind. I didn’t work out how to get this to work, but following
advice from #rubyonrails IRC I decided to simplify things a little :slight_smile: I
now store images on the filesystem and do all my caching using memcache.

Hi Yaxm,

Can you explain how your problem is similar? Mine was that I was wanting
to mix file caching with memcache. My solution was to not do so! For the
record, I’m much happier now that I’m storing my images on the
filesystem anyway.

I haven’t needed to get page caching working with memcache yet, but I do
foresee needing to do so. At the moment, I just cache models explicitly.
I’ll try and remember to post my experience here when I come across a
solution.

Cheers,
Bill

Bill?

Do you cache and expire images in file system?

When you run mongrels on different servers,
do you cache images on a particular server?
if you cache images on all servers, would you need to expire cached
images on all servers?

Bill Horsman wrote:

Hi Yaxm,

Can you explain how your problem is similar? Mine was that I was wanting
to mix file caching with memcache. My solution was to not do so! For the
record, I’m much happier now that I’m storing my images on the
filesystem anyway.

I haven’t needed to get page caching working with memcache yet, but I do
foresee needing to do so. At the moment, I just cache models explicitly.
I’ll try and remember to post my experience here when I come across a
solution.

Cheers,
Bill

Yaxm Y. wrote:

Do you cache and expire images in file system?

I don’t really cache them. The file system is the store. They’re not
stored anywhere else. The database just has a reference to the file
name.

When you run mongrels on different servers,

Yes.

do you cache images on a particular server?

Yes. My setup is Apache load balancing to a cluster of Mongrels spread
over two machines. I’ve configured Apache to serve all static content
directly so it doesn’t even involve the cluster.

The only tricky bit is when Rails updates an image (for instance, if a
user uploads a new one) I have to make sure it uses a “local” Mongrel
instance on the same machine. Snippet from my httpd.conf:

Direct all image uploads to local Mongrel instance

RewriteRule /upload_an_image_path/(.*)$
http://127.0.0.1:3000%{REQUEST_URI} [P,QSA,L]
[snip]

Rewrite all non-static requests to cluster

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

This is where file_column stores images

RewriteCond /home/me/images/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_clusterA%{REQUEST_URI} [P,QSA,L]

  • Bill