Rails 3.2 where is fragment cached data getting stored? - ruby-on-rails

I have created rails application and I have used fragment caching concept. Where is cached data getting stored and how can I access those data? How do I know whether caching is working or not?

Take a look into fragment caching docs for references (you are particularly interested in ActionController::Caching::Fragments).
You may want to check
ActiveSupport::Cache.expand_cache_key(key, namespace)
to check specific key for whether it is being cached.
To check the cache storage you can check the
config.action_controller.page_cache_directory
P.S. It's high time to upgrade your Rails version ;)

Eventually I got the solution.
Cached data will stored based on cache_store used in environment configuration.
Ex)
Some available cache_stores are memory_store, file_store,.. etc
I have configured file_store in development environment then the data will be stored in rails_root/tmp/cache directory

Related

Does Rails cache templates?

I assume yes, and some tests point to yes, but I'd like to find the documentations that says Rails caches (unevaluated) templates in memory.
That is, for any .erb, .sass, .jbuilder, etc template Rails will:
read the template from file, only once
retrieve the template from memory when needed
apply data to the template on every invocation unless the generated output is cached.
All template/cache searches and documentation seem to be focused on point #3. And development Rails flags enable/disable class-caching. But finding docs that verify claim #1/#2 seem illusive. Does Rails re-read template files every time and rely on OS level file caching?
I had the same question and did some hunting.
Yes - Rails caches templates.
Pay attention to the fact cache takes locals as key, and I would say then that unevaluated template is not cached.
In production, it will if you add calls to do so. It is disabled in development mode however you can edit it to cache in development as well in your config/environments/development.rb file by changing the following line from false to true:
config.action_controller.perform_caching = false
In production, Rails has 3 main methods of caching when it comes to views (there are also rails methods for db caching). Page-caching, action-caching, and fragment-caching. To implement these in Rails you can use certain helpers like the caches_page macro-style method in a controller. This will cache the view in its entirety to disk with no further involvement by the Rails dispatcher. This should only be used when the page is completely static and has no dynamic content. caches_action and fragment caching provide much more fine-grained implementations and probably make up the majority of use-cases in Rails.

Setting the rails cache store for fragment_caching to be different

Is is possible to configure the rails fragment caching to be a file_store and have the Rails.cache object to store values using a memory store? Or are they both the same storage mechanism in the end?
As far as I'm aware, without monkey patching things, they're going to have to share the same cache store.
I'd assume it's possible to monkey patch things to have it look for which store to use elsewhere, and then get the two working separately, but that sounds like a bit of a nightmare.

Cache strategy in a rails application using membase, how do I make sure I don't delete everything?

I have a rails application.
I am using membase/memcache to cache DB objects and HTML partials.
I cache db objects with the create operation and of course find operations etc...
now, when I do User.find(1).
this is cached as an object in memcache.
I have a pretty good strategy with caching these along side with the HTML content.
now, when I deploy, one of the thing my Capistrano script is doing is to clear the cache (because of the html partials that change) but there's really no reason to invalidate the cache of the db objects.
How can I only delete part of my cache?
Can this be done?
my cache keys look like this
DB: user_find_by_id_10000
HTML: user_profile_home_1000
Would appreciate you help
Thanks.
It might also be a good idea to user separate buckets for your DB cache and your HTML cache...then you can use the 'flush_all' command to clear out a whole bucket without affecting the other one.
Also, looking forward to Couchbase Server 2.0 which will be in a developer preview at the end of this week, you'll be able to create indexes and views to return just the data that you're looking for, you can then feed that through a little process to delete all the items that match a certain criteria.
Perry Krug
Solutions Architect, Couchbase Inc.
It's fairly simple to delete a cached item based on its key:
Rails.cache.delete('user_profile_home_1000')
In the code above I'm assuming you've set Rails' cache to use Memcached.

how to use memcache to speed-up rails/heroku

Heroku supports memcache natively as an addon. my problem is, being a rails newbie still, I have no clue how to use memcache in order to speed-up my most time-consuming request (I know which they are by looking a the newrelic analysis). should we use a gem like 'cache-money' on-top of memcache? does anyone use act_as_cached anymore?
I know this is a pretty trivial questions. Yet after searching the web for hours, I could not find a decent tutorial. Any help/link appreciated!
You can watch Caching in Rails 2.1 and then read the memcached documentation (I suppose you have already read it) in Heroku.
Also, Touch and Cache is quite interesting technique to avoid writing Sweepers in order to delete cached content when you need to refresh the cached data. Using touch will auto expire cached data with almost no need to write new code.
Please note that today, the Heroku memcached integration assumes you are using Rails >= 2.3.3
The main idea is that you add the result(s) of your time consuming method to Rails.cache (which is the interface through which you access your caching mechanism). When you fetch that result(s) the caching mechanism searches to see if it can find it or if it hasn't expired.
If it finds it, it returns it very fast because it takes it from the cache.
If it doesn't find it or it has expired (you set this when you call fetch), it runs the actual slow method to add it or refresh it in cache.
Finally, it is very useful to read the Rails documentation apart from whether you are using memcached or the built in Rails caching: Caching with Rails: An overview. Among other things it talks about:
Page caching
Action caching
Fragment caching
Sweepers
SQL caching
and more ...
You can cache on memcache the action_cache or you can access to memcache with Rails.cache

how to expire page cache without sweepers

I am page caching a list of products - products.json in the public directory under the rails root.
I don't add or delete this data through a controller action. This data is loaded into the database
through a data migration script. In the development environment I delete this file from the public
directory and restart the mongrel server but it is still pulling up the cached data. Not sure from where. How do I expire this cache data.
thanks much,
ash
Have you tried?
rake cache:clear
You have to figure out where the data is being cached. Are you sure that, after you delete the cached version from /public, the data you see on the website is in fact getting pulled from the cache? Because if the base data have not changed, then of course it will recache it the moment is it re-requested (that's how page caching works, if the cache isn't present it regenerates it).
Also, note that there are different data stores available for caching: disk, memory, and more complex solutions (like Memcached). If you see differences between development and production, it could be that you are caching in different places in different environments.
If you want something that will really trash cached files on disk, you may want to try http://github.com/factore/cache_trasher

Resources