Class: TeguGears::MemoRepository
- Defined in:
- lib/tegu_gears/memo_repository.rb
Overview
This works like a pass through to other caching tools, or keeps things in a hash. Each class hosting TeguGear has its own entry in this repository, as does each composition. Other caching tools may be useful to monitor the cache size, distribute it, or reduce different kinds of caching. I use a key/value interface, because that should work well with Memcache, Tokyo Cabinet, Redis, Hash, CouchDB, and other tools.
I refactored to a centrally-managed repository because distributed caching doesn’t make sense for expensive computations. There’s probably another abstraction available that will allow distributed caching only when configured, but I’ll get to that when I write a configuration harness.
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Class Method Summary collapse
Instance Method Summary collapse
- #flush_for(caller) ⇒ Object
- #for(caller, key = nil) ⇒ Object
-
#initialize(store = Hash.new) ⇒ MemoRepository
constructor
A new instance of MemoRepository.
- #method_missing(sym, *args, &block) ⇒ Object
-
#set(caller, key, value) ⇒ Object
Expects a hash on the other end, for now.
Constructor Details
#initialize(store = Hash.new) ⇒ MemoRepository
Returns a new instance of MemoRepository.
32 33 34 |
# File 'lib/tegu_gears/memo_repository.rb', line 32 def initialize(store=Hash.new) @store = store end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
36 37 38 |
# File 'lib/tegu_gears/memo_repository.rb', line 36 def method_missing(sym, *args, &block) self.store.send(sym, *args, &block) end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
31 32 33 |
# File 'lib/tegu_gears/memo_repository.rb', line 31 def store @store end |
Class Method Details
.instance ⇒ Object
22 23 24 |
# File 'lib/tegu_gears/memo_repository.rb', line 22 def instance @@instance ||= new(REPOSITORY_CLASS.new) end |
.method_missing(sym, *args, &block) ⇒ Object
26 27 28 |
# File 'lib/tegu_gears/memo_repository.rb', line 26 def method_missing(sym, *args, &block) instance.send(sym, *args, &block) end |
Instance Method Details
#flush_for(caller) ⇒ Object
58 59 60 |
# File 'lib/tegu_gears/memo_repository.rb', line 58 def flush_for(caller) self.store[caller] = REPOSITORY_CLASS.new end |
#for(caller, key = nil) ⇒ Object
53 54 55 56 |
# File 'lib/tegu_gears/memo_repository.rb', line 53 def for(caller, key=nil) self.store[caller] ||= REPOSITORY_CLASS.new key ? self.store[caller][key] : self.store[caller] end |
#set(caller, key, value) ⇒ Object
Expects a hash on the other end, for now.
41 42 43 44 45 |
# File 'lib/tegu_gears/memo_repository.rb', line 41 def set(caller, key, value) key = simplify_key(key) self.store[caller] ||= REPOSITORY_CLASS.new self.store[caller][key] = value end |