Thomas Mango

A Cacheable Hash That Stays Synced

Rails.cache freezes values in hashes that are cached directly. CacheableHash is a wrapper for Hash objects that prevents that from happening. Changes to the Hash being wrapped are automatically persisted back to the cache store so that everything stays in sync.

You can clone (or fork) my new MIT licensed plugin over at github.

Example

An instance of CacheableHash is used like any other hash. A cache key is the only thing required.

Changes are automatically persisted.

About Synchronicity

Whenever a method is called on an instance of CacheableHash, that instance automatically writes itself back to the cache store. Often, this is unnecessary. If you call .keys, for example, there is no reason to write back to the cache store. Only when reading a value from the hash using [], is the copy in the cache store not specifically updated. But, rather than worrying about the specific methods that change the contents of the hash, it’s safer to just update the instance in the cache store whenever not specifically reading values.