Class: Bin::Store
- Inherits:
-
Compatibility
- Object
- ActiveSupport::Cache::Store
- Compatibility
- Bin::Store
- Defined in:
- lib/bin/store.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #clear ⇒ Object
- #collection ⇒ Object
- #decrement(key, amount = 1) ⇒ Object
- #delete(key, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(key, options = nil) ⇒ Boolean
- #expires_in ⇒ Object
- #increment(key, amount = 1) ⇒ Object
-
#initialize(collection = nil, options = {}) ⇒ Store
constructor
A new instance of Store.
- #read(key, options = nil) ⇒ Object
- #stats ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(collection = nil, options = {}) ⇒ Store
Returns a new instance of Store.
13 14 15 |
# File 'lib/bin/store.rb', line 13 def initialize(collection=nil, ={}) @collection, = collection, end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/bin/store.rb', line 4 def end |
Instance Method Details
#clear ⇒ Object
70 71 72 |
# File 'lib/bin/store.rb', line 70 def clear collection.remove end |
#collection ⇒ Object
6 7 8 9 10 11 |
# File 'lib/bin/store.rb', line 6 def collection if defined? MongoMapper @collection ||= MongoMapper.database.collection("cache_store") end @collection end |
#decrement(key, amount = 1) ⇒ Object
64 65 66 67 68 |
# File 'lib/bin/store.rb', line 64 def decrement(key, amount=1) super do counter_key_upsert(key, -amount.abs) end end |
#delete(key, options = nil) ⇒ Object
40 41 42 43 44 |
# File 'lib/bin/store.rb', line 40 def delete(key, =nil) super do collection.remove(:_id => key.to_s) end end |
#delete_matched(matcher, options = nil) ⇒ Object
46 47 48 49 50 |
# File 'lib/bin/store.rb', line 46 def delete_matched(matcher, =nil) super do collection.remove(:_id => matcher) end end |
#exist?(key, options = nil) ⇒ Boolean
52 53 54 55 56 |
# File 'lib/bin/store.rb', line 52 def exist?(key, =nil) super do !read(key, ).nil? end end |
#expires_in ⇒ Object
17 18 19 |
# File 'lib/bin/store.rb', line 17 def expires_in @expires_in ||= [:expires_in] || 1.year end |
#increment(key, amount = 1) ⇒ Object
58 59 60 61 62 |
# File 'lib/bin/store.rb', line 58 def increment(key, amount=1) super do counter_key_upsert(key, amount) end end |
#read(key, options = nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/bin/store.rb', line 32 def read(key, =nil) super do if doc = collection.find_one(:_id => key.to_s, :expires_at => {'$gt' => Time.now.utc}) doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s) end end end |
#stats ⇒ Object
74 75 76 |
# File 'lib/bin/store.rb', line 74 def stats collection.stats end |
#write(key, value, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bin/store.rb', line 21 def write(key, value, ={}) key = key.to_s super do expires = Time.now.utc + (( && [:expires_in]) || expires_in) raw = !![:raw] value = raw ? value : BSON::Binary.new(Marshal.dump(value)) doc = {:_id => key, :value => value, :expires_at => expires, :raw => raw} collection.save(doc) end end |