# Using memcache and file caching at the same time

**URL:** <https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266>\
**Category:** Rails\
**Created:** [November 15, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266 "2007-11-15T12:05:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [November 15, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/1 "2007-11-15T12:05:30Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [November 15, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/2 "2007-11-15T15:31:26Z")

</div>

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 🙂 I  
now store images on the filesystem and do all my caching using memcache.

---

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [January 10, 2008, 3:43am UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/3 "2008-01-10T03:43:56Z")

</div>

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 🙂 I  
> now store images on the filesystem and do all my caching using memcache.

---

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [January 10, 2008, 9:01am UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/4 "2008-01-10T09:01:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [January 10, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/5 "2008-01-10T20:45:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wbharding](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8797f3/32.png) [@wbharding](https://www.ruby-forum.com/u/wbharding)\
**Post date:** [January 10, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/using-memcache-and-file-caching-at-the-same-time/120266/6 "2008-01-10T21:08:57Z")

</div>

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](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
