Class: Bin::Store
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- Bin::Store
- Defined in:
- lib/bin/store.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #clear ⇒ Object
- #decrement(key, amount = 1) ⇒ Object
- #delete(key, options = nil) ⇒ Object
- #delete_entry(key, options) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(key, options = nil) ⇒ Boolean
- #expires_in ⇒ Object
- #increment(key, amount = 1) ⇒ Object
-
#initialize(collection, options = {}) ⇒ Store
constructor
A new instance of Store.
- #read_entry(key, options = nil) ⇒ Object
- #stats ⇒ Object
- #write_entry(key, entry, options = {}) ⇒ Object
Constructor Details
#initialize(collection, options = {}) ⇒ Store
Returns a new instance of Store.
6 7 8 |
# File 'lib/bin/store.rb', line 6 def initialize(collection, ={}) @collection, @options = collection, end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
4 5 6 |
# File 'lib/bin/store.rb', line 4 def collection @collection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/bin/store.rb', line 4 def @options end |
Instance Method Details
#clear ⇒ Object
59 60 61 |
# File 'lib/bin/store.rb', line 59 def clear collection.remove end |
#decrement(key, amount = 1) ⇒ Object
55 56 57 |
# File 'lib/bin/store.rb', line 55 def decrement(key, amount=1) counter_key_upsert(key, -amount.abs) end |
#delete(key, options = nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/bin/store.rb', line 31 def delete(key, =nil) super do collection.remove(:_id => key.to_s) end end |
#delete_entry(key, options) ⇒ Object
41 42 43 |
# File 'lib/bin/store.rb', line 41 def delete_entry(key, ) collection.remove(:_id => key.to_s) end |
#delete_matched(matcher, options = nil) ⇒ Object
37 38 39 |
# File 'lib/bin/store.rb', line 37 def delete_matched(matcher, =nil) collection.remove(:_id => matcher) end |
#exist?(key, options = nil) ⇒ Boolean
45 46 47 48 49 |
# File 'lib/bin/store.rb', line 45 def exist?(key, =nil) super do !read(key, ).nil? end end |
#expires_in ⇒ Object
10 11 12 |
# File 'lib/bin/store.rb', line 10 def expires_in @expires_in ||= [:expires_in] || 1.year end |
#increment(key, amount = 1) ⇒ Object
51 52 53 |
# File 'lib/bin/store.rb', line 51 def increment(key, amount=1) counter_key_upsert(key, amount) end |
#read_entry(key, options = nil) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/bin/store.rb', line 24 def read_entry(key, =nil) if doc = collection.find_one(:_id => key.to_s, :expires_at => {'$gt' => Time.now.utc}) value = doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s) value.is_a?(ActiveSupport::Cache::Entry) ? value : ActiveSupport::Cache::Entry.new(value, ) end end |
#stats ⇒ Object
63 64 65 |
# File 'lib/bin/store.rb', line 63 def stats collection.stats end |
#write_entry(key, entry, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/bin/store.rb', line 14 def write_entry(key, entry, ={}) key = key.to_s value = entry.value 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 |