# Writing a cache module

**URL:** <https://www.ruby-forum.com/t/writing-a-cache-module/176301>\
**Category:** NGINX\
**Created:** [September 28, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/writing-a-cache-module/176301 "2009-09-28T21:11:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [September 28, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/writing-a-cache-module/176301/1 "2009-09-28T21:11:27Z")

</div>

I need to write cache module that’s not upstream based. (Long story…)

Reading through the upstream code and the proxy and fastcgi modules, it  
looks fairly simple.

Just so I know I’m on the right track:

- to get something from cache I should generate a key for r-\>cache, try  
to  
open file using ngx\_http\_file\_cache\_open, then “serve” it using  
ngx\_http\_cache\_send (or replicate what it does).

-to store something in cache, create a ngx\_temp\_file\_t store my data in  
there, generate a key, then call ngx\_http\_file\_cache\_update to add that  
file  
to cache.

Is that reasonable? Any gotchas?

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [September 28, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/writing-a-cache-module/176301/2 "2009-09-28T21:40:08Z")

</div>

On Mon, Sep 28, 2009 at 03:06:25PM -0400, Akins, Brian wrote:

> -to store something in cache, create a ngx\_temp\_file\_t store my data in  
> there, generate a key, then call ngx\_http\_file\_cache\_update to add that file  
> to cache.
> 
> Is that reasonable?

Yes.

> Any gotchas?

Do not forget ngx\_http\_file\_cache\_free() for noncacheable responses.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [September 28, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/writing-a-cache-module/176301/3 "2009-09-28T22:05:18Z")

</div>

On 9/28/09 3:06 PM, “Brian A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to write cache module that’s not upstream based. (Long story…)

Actually maybe someone else has some pointers so I may not have to start  
from scratch.

Basically, I need to replicate how apache’s mod\_cache works (but faster  
of  
course - that’s why I’m playing with nginx.)

What it does, is the handler runs really early in the response. On a  
cache  
hit, it short circuits the filter stack (skipping gzip, ssi, etc.) and  
serves to client.

It stores things in cache after most of the filter stack (after, ssi,  
gzip,  
etc.)

It uses vary. I think in nginx, for my purposes at least, I could just  
use  
configuration to control behavior of the cache key.

Here’s a talk from apachecon I did a few years ago to give a hint at  
what  
I’m wanting to do: [http://www.akins.org/files/apachecon-cnn-good.pdf](http://www.akins.org/files/apachecon-cnn-good.pdf)

I’m pretty sure I can get equal, and actually better, functionality from  
nginx, but just wondering if someone else had attempted it.
