Created
June 14, 2012 13:45
-
-
Save sjoonk/2930395 to your computer and use it in GitHub Desktop.
MongoCache: Simple key/value cache store based on MongoDB/Mongoid
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
# 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