Skip to content

Instantly share code, notes, and snippets.

@sjoonk
Created June 14, 2012 13:45
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 sjoonk/2930395 to your computer and use it in GitHub Desktop.
Save sjoonk/2930395 to your computer and use it in GitHub Desktop.
MongoCache: Simple key/value cache store based on MongoDB/Mongoid
# encoding: UTF-8
# MongoCache
# Using MongoDB as a simple cache store
# by sjoonk@gmail.com 2012
require 'mongoid'
class MongoCache
include Mongoid::Document
cache
field :key, type: String
field :value, type: Hash, default: {}
index :key, unique: true
def self.put(key, value)
self.find_or_create_by(key: key).update_attribute(:value, value)
end
def self.get(key); self.where(key: key).first.value end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment