Skip to content

Commit

Permalink
Merge branch 'master' of github.com:SFEley/mongo_store
Browse files Browse the repository at this point in the history
  • Loading branch information
SFEley committed Aug 30, 2010
2 parents a144ca7 + d39fb3a commit 0c13c74
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions lib/active_support/cache/mongo_store.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
require 'active_support'
require 'mongo'

module MongoStore
module Cache
module Rails2
# Inserts the value into the cache collection or updates the existing value. The value must be a valid
# MongoDB type. An *:expires_in* option may be provided, as with MemCacheStore. If one is _not_
# provided, a default expiration of 1 year is used.
def write(key, value, options=nil)
super
expires = Time.now + ((options && options[:expires_in]) || expires_in)
collection.update({'key' => key}, {'$set' => {'value' => value, 'expires' => expires}}, :upsert => true)
end

# Reads the value from the cache collection.
def read(key, options=nil)
super
if doc = collection.find_one('key' => key, 'expires' => {'$gt' => Time.now})
doc['value']
end
end

# Takes the specified value out of the collection.
def delete(key, options=nil)
super
collection.remove({'key' => key})
end
end
module Rails3
def write_entry(key, entry, options=nil)
expires = Time.now + ((options && options[:expires_in]) || expires_in)
value = entry.value
value = value.to_mongo if value.respond_to? :to_mongo
begin
collection.update({'key' => key}, {'$set' => {'value' => value, 'expires' => expires}}, :upsert => true)
rescue BSON::InvalidDocument
value = value.to_s and retry unless value.is_a? String
end
end
def read_entry(key, options=nil)
doc = collection.find_one('key' => key, 'expires' => {'$gt' => Time.now})
Entry.new(doc['value']) if doc
end
def delete_entry(key, options=nil)
collection.remove({'key' => key})
end
end
module Store
rails3 = defined?(::Rails) && ::Rails.version =~ /^3\./
include rails3 ? Rails3 : Rails2
end
end
end

module ActiveSupport
module Cache
class MongoStore < Store
attr_reader :options
include ::MongoStore::Cache::Store
attr_accessor :expires_in

# Returns a MongoDB cache store. Can take either a Mongo::Collection object or a collection name.
Expand Down Expand Up @@ -40,7 +92,6 @@ def initialize(collection = nil, options = nil)

# Set the expiration time
self.expires_in = @options[:expires_in]
super()
end

# Returns the MongoDB collection described in the options to .new (or else the default 'rails_cache' one.)
Expand All @@ -62,30 +113,7 @@ def clean_expired
def clear
collection.remove
end

# Inserts the value into the cache collection or updates the existing value. The value must be a valid
# MongoDB type. An *:expires_in* option may be provided, as with MemCacheStore. If one is _not_
# provided, a default expiration of 1 year is used.
def write(key, value, options=nil)
super
expires = Time.now + ((options && options[:expires_in]) || expires_in)
collection.update({'key' => key}, {'$set' => {'value' => value, 'expires' => expires}}, :upsert => true)
end

# Reads the value from the cache collection.
def read(key, options=nil)
super
if doc = collection.find_one('key' => key, 'expires' => {'$gt' => Time.now})
doc['value']
end
end

# Takes the specified value out of the collection.
def delete(key, options=nil)
super
collection.remove({'key' => key})
end


# With MongoDB, there's no difference between querying on an exact value or a regex. Beautiful, huh?
alias_method :delete_matched, :delete

Expand Down

0 comments on commit 0c13c74

Please sign in to comment.