Skip to content

Instantly share code, notes, and snippets.

@kowsik
Created January 17, 2012 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowsik/1628665 to your computer and use it in GitHub Desktop.
Save kowsik/1628665 to your computer and use it in GitHub Desktop.
Monkey patch CouchRest for supporting redis caching
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