Created
January 17, 2012 20:21
-
-
Save kowsik/1628665 to your computer and use it in GitHub Desktop.
Monkey patch CouchRest for supporting redis caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CouchRest::Database | |
alias :old_save_doc :save_doc | |
alias :old_delete_doc :delete_doc | |
attr_writer :rcache | |
# Automatically add the created_at and updated_at time fields to the | |
# document and also save it back to redis with a ttl. | |
def save_doc doc, bulk = false, batch = false | |
doc['created_at'] ||= Time.now | |
doc['updated_at'] = Time.now | |
res = old_save_doc doc, bulk, batch | |
rcache.set doc['_id'], doc | |
return res | |
end | |
# Purge the key from redis after a successful delete | |
def delete_doc doc | |
res = old_delete_doc doc | |
rcache.del doc['_id'] | |
return res | |
end | |
# Fetch from redis first and if not, go to CouchDB. | |
def cache_get id | |
rcache.get id do | |
self.get id rescue nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment