Class: Prefatory::Repository
Overview
A repository that temporarily saves ruby objects to a key value store (Redis or Memcached).
Constant Summary collapse
- KEY_NOT_FOUND_MSG =
"Unable to find the object with key ?"
Constants included from Keys
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete!(key) ⇒ Object
- #find(key) ⇒ Object
- #find!(key) ⇒ Object
-
#initialize(key_prefix: nil, storage: nil, config: Prefatory.config, marshaler: Marshal) ⇒ Repository
constructor
A new instance of Repository.
- #save(obj, ttl = nil) ⇒ Object
- #save!(obj, ttl = nil) ⇒ Object
- #update(key, obj, ttl = nil) ⇒ Object
- #update!(key, obj, ttl = nil) ⇒ Object
Methods included from Keys
Constructor Details
#initialize(key_prefix: nil, storage: nil, config: Prefatory.config, marshaler: Marshal) ⇒ Repository
Returns a new instance of Repository.
12 13 14 15 16 17 18 |
# File 'lib/prefatory.rb', line 12 def initialize(key_prefix: nil, storage: nil, config: Prefatory.config, marshaler: Marshal) @config = config @storage = storage || Storage::Discover.new(@config.storage, @config.ttl, key_prefix).instance @marshaler = marshaler end |
Instance Method Details
#delete(key) ⇒ Object
63 64 65 66 67 |
# File 'lib/prefatory.rb', line 63 def delete(key) return false unless @storage.key?(key) @storage.delete(key) true end |
#delete!(key) ⇒ Object
69 70 71 72 |
# File 'lib/prefatory.rb', line 69 def delete!(key) raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key) delete(key) end |
#find(key) ⇒ Object
20 21 22 23 24 |
# File 'lib/prefatory.rb', line 20 def find(key) return nil unless @storage.key?(key) value = @storage.get(key) @marshaler.load(value) end |
#find!(key) ⇒ Object
26 27 28 29 30 |
# File 'lib/prefatory.rb', line 26 def find!(key) raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key) obj = find(key) obj end |
#save(obj, ttl = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/prefatory.rb', line 32 def save(obj, ttl=nil) begin value = @marshaler.dump(obj) key = @storage.next_key(obj) @storage.set(key, value, ttl) rescue StandardError => e logger.info("An error occurred (#{e.inspect}) storing object: #{obj.inspect}") key = nil end key end |
#save!(obj, ttl = nil) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/prefatory.rb', line 44 def save!(obj, ttl=nil) key = save(obj, ttl) raise Errors::NotSaved, "Unable to save object! #{obj.inspect}. Check the log for more information." unless @storage.key?(key) key end |
#update(key, obj, ttl = nil) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/prefatory.rb', line 51 def update(key, obj, ttl=nil) return false unless @storage.key?(key) value = @marshaler.dump(obj) @storage.set(key, value, ttl) true end |
#update!(key, obj, ttl = nil) ⇒ Object
58 59 60 61 |
# File 'lib/prefatory.rb', line 58 def update!(key, obj, ttl=nil) raise Errors::NotFound, KEY_NOT_FOUND_MSG.sub('?',key) unless @storage.key?(key) update(key, obj, ttl) end |