Skip to content

A wrapper for Hashes to prevent frozen values when storing in memcached as well as automatically keeping the cached copy up to date.

Notifications You must be signed in to change notification settings

tsmango/cacheable_hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Plugin Details
--------------
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.

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

>> fruit = CacheableHash.new('fruit_hash_key', :hash => {:apple => 'red', :banana => 'yellow'}, :expires_in => 1.week)
>> fruit[:banana]
=> "yellow"

Changes are automatically persisted.

>> fruit = Rails.cache.read('fruit_hash_key')
>> fruit[:apple]
=> "red"

>> fruit[:apple] = 'green'

>> Rails.cache.read('fruit_hash_key')[:apple]
=> "green"

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.

License
-------
This plugin is available under the MIT license.

Authors
-------
Thomas Mango
http://slicedsoftware.com

About

A wrapper for Hashes to prevent frozen values when storing in memcached as well as automatically keeping the cached copy up to date.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages