# How to expire cache in AR backend?

**URL:** <https://www.ruby-forum.com/t/how-to-expire-cache-in-ar-backend/186278>\
**Category:** Rails I18n\
**Created:** [April 3, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/how-to-expire-cache-in-ar-backend/186278 "2010-04-03T14:10:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 3, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/how-to-expire-cache-in-ar-backend/186278/1 "2010-04-03T14:10:02Z")

</div>

Hey,

how to expire cache in AR backend i.e. after updating a translation? I  
think there’s a problem with cache\_key method - it simply takes the  
arguments passed to #translate and generates cache key based on them.  
The problem is i.e. with count - it generates different cache keys for  
these translations:

I18n.t(“key.with.count”, :count =\> 2)  
I18n.t(“key.with.count”, :count =\> 3)  
I18n.t(“key.with.count”, :count =\> 4)

because it passes {:count =\> x} to the cache\_key method instead  
of :other. This also causes a problem when I’m trying to expire cache,  
because I can’t generate cache\_key inside Translation model, because I  
don’t have :count =\> x option.

How do you guys expire cache in AR backend? Or maybe it would be  
possible to modify cache\_key method to figure out the pluralization  
first (i.e. :one, :other) and use this to build the cache key?

BTW. Isn’t Cache#cache\_key going to generate different keys for normal  
interpolation as well?

Regards,  
Szymon

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 5, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/how-to-expire-cache-in-ar-backend/186278/2 "2010-04-05T16:50:23Z")

</div>

I’ve created ExpirableCache module for i18n:

> <https://gist.github.com/szimek/356383>

that caches uninterpolated translations. I haven’t written any tests  
for it yet, but if any of the i18n guys could take a look at it and  
check if it makes any sense it would be great.

One of the problems is that currently if the translation in the  
database has nil value, it can’t be stored in the cache:

cache\_store.write(“key”, nil)  
cache\_store.exist?(“key”) =\> false

thus it always hits the db if the translation is missing. Maybe  
storing false instead of nil could help here…

Cheers,  
Szymon
