开发者

Expiring Rails fragment cache from external application

I hav开发者_开发百科e a Rails application with database that is updated from external C++ program. I'd like to speed up the app using fragment caching but when a record is updated a corresponding fragment cache entry needs to be expired.


If the C++ application talks directly to the database, you can use fragment caching in combination with the cache_key.

It's easy and built-in in Rails. All you have to do, is to make sure your C++ application updates the updated_at field of the record when the main record content is updated. Then, you can scope your fragment to the record object and the cache will automatically expire when the record is changed.

<% cache [@article, :metadata] do %>
here the content to be cached
for example the <%= @article.title %>.
:metadata is simply a name for this fragment.
<% end %>

Assuming the @article record was saved on 2010-02-10 18:00:00, the fragment will be cached at /path/to/cache/.../24-20100210180000. When your C++ updates the record and changes the updated_at column, the cache will be invalidated and the next request will automatically hit the content again.

For more information about caching, check out http://railslab.newrelic.com/scaling-rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜