开发者

Expiring Rails fragment cache from external application

开发者 https://www.devze.com 2022-12-21 11:38 出处:网络
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 fra

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

精彩评论

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