Rails (as of 2.1) provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk.
Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings. Some cache store implementations, like MemoryStore, are able to cache arbitrary Ruby objects, but don’t count on every cache store to be able to do that.
The default cache stores provided with Rails include:
1) ActiveSupport::Cache::MemoryStore: A cache store implementation which stores everything into memory in the same process. If you’re running multiple Ruby on Rails server processes (which is the case if you’re using mongrel_cluster or Phusion Passenger), then this means that your Rails server process instances won’t be able to share cache data with each other. If your application never performs manual cache item expiry (e.g. when you‘re using generational cache keys), then using MemoryStore is ok. Otherwise, consider carefully whether you should be using this cache store.
MemoryStoreis not only able to store strings, but also arbitrary Ruby objects. MemoryStoreis not thread-safe. Use SynchronizedMemoryStore instead if you
need thread-safety.
ActionController::Base.cache_store = :memory_store
2) ActiveSupport::Cache::FileStore: Cached data is stored on the disk. This is the default store and the default path for this store is: /tmp/cache. Works well for all types of environments and allows all processes running from the same application directory to access the cached content. If /tmp/cache does not exist, the default store becomes MemoryStore.
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
3) ActiveSupport::Cache::DRbStore: Cached data is stored in a separate shared DRb process that all servers communicate with. This works for all environments and only keeps one cache around for all processes, but requires that you run and manage a separate DRb process.
ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
4) MemCached store: Works like DRbStore, but uses Danga’s MemCache instead. Rails uses the bundled memcached-client gem by default. This is currently the most popular cache store for production websites.
Special features:
- Clustering and load balancing. One can specify multiple memcached servers, and MemCacheStore will load balance between all available servers. If a server goes down, then MemCacheStore will ignore it until it goes back online.
- Time-based expiry support. See write and the :expires_in option.
- Per-request in memory cache for all communication with the MemCache server(s).
It also accepts a hash of additional options:
- :namespace- specifies a string that will automatically be prepended to keys when accessing the memcached store.
- :readonly- a boolean value that when set to true will make the store read-only, with an error raised on any attempt to write.
- :multithread – a boolean value that adds thread safety to read/write operations – it is unlikely you’ll need to use this option as the Rails threadsafe! method offers the same functionality.
The read and write methods of the MemCacheStore accept an options hash too. When reading you can specify :raw => true to prevent the object being marshaled (by default this is false which means the raw value in the cache is passed to Marshal.load before being returned to you.)
When writing to the cache it is also possible to specify :raw => true. This means that the value is not passed to Marshal.dump before being stored in the cache (by default this is false).
The write method also accepts an :unless_exist flag which determines whether the memcached add (when true) or set (when false) method is used to store the item in the cache and an :expires_in option that specifies the time-to-live for the cached item in seconds.
ActionController::Base.cache_store = :mem_cache_store, "localhost"
5) ActiveSupport::Cache::SynchronizedMemoryStore: Like ActiveSupport::Cache::MemoryStore but thread-safe.
ActionController::Base.cache_store = :synchronized_memory_store
6) ActiveSupport::Cache::CompressedMemCacheStore: Works just like the regular MemCacheStore but uses GZip to decompress/compress on read/write.
ActionController::Base.cache_store = :compressed_mem_cache_store, "localhost"
7) Custom store: You can define your own cache store (new in Rails 2.1)
ActionController::Base.cache_store = MyOwnStore.new("parameter")
config.cache_store can be used in place of
environment.rb.
In addition to all of this, Rails also adds the ActiveRecord::Base#cache_key method that generates a key using the class name, id and updated_at timestamp (if available).
An example:
Rails.cache.read("city") # => nil
Rails.cache.write("city", "Duckburgh")
Rails.cache.read("city") # => "Duckburgh"
ref http://guides.rubyonrails.org/caching_with_rails.html
I have been working with freelancers throughout my career and recently, thanks to services like oDesk, I find myself doing it more often. So you might think that I am happy with what I get, at least in general. Well, one of the reasons I continue to stay engaged is my high tolerance for pain – I am prepared to go through piles of hay to find that needle. And I have to tell you, looking for freelancers is very much like digging for gold – you literally have to go through tons of dirt to find it.
Interestingly enough many freelancers who have skills, knowledge and maybe even talent often torpedo themselves, aggressively sabotage their chances of getting customers right in the begging of the process. They make simple yet lethal mistakes that turn off clients before they got the chance to learn about freelancer’s ingenuity. Below are some of those mistakes:
- Not reading my post before you reply to it. Your three page long template proposal will get you in a recycle been faster than anything else. At least adjust your opening statement, show me that you read the post…
- Not using proper grammar and spelling. English is my second language and still work in progress; I still straggle with grammar myself, yet many responses I see push that envelope way too far. Grammatically poor introduction screams in my face “Communicating with this freelancer will be a real pain!” Spelling mistakes are even worse – how can I entrust my project to someone who doesn’t even make an effort to turn on a spellchecker?
- Talking with me like I am a teenager. Your slang (especially when combined with ESL marvels) comes across as complete lack of intelligence and class. By the way, spellchecker is not likely to recognize your “gonna”, “wanna”, “gimme”, take a hint.
- Being excessively polite. Your culture and language might require twenty minutes of praise and compliments before you get to business but I am an American, cut to the chase guy. More so, being overly polite and using somewhat unusual forms will telegraph a wrong image, your mentioning my “ultimate wisdom” only makes me think of a snake oil salesman.
- Not being punctual / prepared for your interview. I think of proposal / interview stage as a “honeymoon” in a relationship with a freelancer, it all goes downhill from there. Late for your Skype call? Having troubles finding your headset? Can’t introduce yourself? Chances are that’s the last time you’ll hear from me.
Don’t think I’m done here: I am only getting warmed up; it’s just my 500 word limit coming up. I guess will continue in my blog.
Nick Krym is a technology professional with over 25 years in the IT industry, and the author of the Pragmatic Outsourcing blog.