Sliced Software

About

Thomas Mango writes software, has a bulldog, listens to Long Island Hardcore music and eats sandwiches.

He is currently working freelance projects as well as on his own project, Limited Pressing.

Software

PGnJ An intuitive Database Development Environment for Mac OS X.

Mobile LIRR An iPhone optimized Long Island Railroad scheduling application.

Trends An iPhone optimized Twitter Trends application. Get the current trends and their reason for trending on the go (data by What the Trend?).

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.

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

Looking for really old posts? Until they are here at tumblr, see here!