Class: Mpp::MemoryStore
Overview
In-memory key-value store for development/testing. Production implementations should use Redis, DynamoDB, etc.
Duck type interface (Store):
get(key) -> value or nil
put(key, value) -> void
delete(key) -> void
put_if_absent(key, value) -> bool
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #put(key, value) ⇒ Object
- #put_if_absent(key, value) ⇒ Object
Constructor Details
#initialize ⇒ MemoryStore
Returns a new instance of MemoryStore.
17 18 19 20 |
# File 'lib/mpp/store.rb', line 17 def initialize @data = T.let({}, T::Hash[T.untyped, T.untyped]) @mutex = T.let(Mutex.new, Thread::Mutex) end |
Instance Method Details
#delete(key) ⇒ Object
33 34 35 |
# File 'lib/mpp/store.rb', line 33 def delete(key) @mutex.synchronize { @data.delete(key) } end |
#get(key) ⇒ Object
23 24 25 |
# File 'lib/mpp/store.rb', line 23 def get(key) @mutex.synchronize { @data[key] } end |
#put(key, value) ⇒ Object
28 29 30 |
# File 'lib/mpp/store.rb', line 28 def put(key, value) @mutex.synchronize { @data[key] = value } end |
#put_if_absent(key, value) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/mpp/store.rb', line 40 def put_if_absent(key, value) @mutex.synchronize do return false if @data.key?(key) @data[key] = value true end end |