Class: Duality
- Inherits:
-
Object
- Object
- Duality
- Defined in:
- lib/duality.rb
Constant Summary
- VERSION =
"0.0.5"
Instance Method Summary (collapse)
-
- (Object) delete(key)
(also: #remove)
Delete from both - async.
-
- (Object) flush
(also: #clean)
Flush caches - async.
- - (Object) flush_expired (also: #cleanup)
-
- (Object) get(key)
(also: #load)
Get from fast or slow.
-
- (Duality) initialize(fast, slow)
constructor
A new instance of Duality.
-
- (Object) method_missing(meth, *args, &block)
Add support for other methods from passed caches this adds support only, but no speed gains.
-
- (Object) set(key, value)
(also: #save)
Set to fast and slow.
Constructor Details
- (Duality) initialize(fast, slow)
A new instance of Duality
6 7 8 9 10 11 12 |
# File 'lib/duality.rb', line 6 def initialize fast, slow # check params are caches raise NotACache unless fast.respond_to?(:set) && fast.respond_to?(:get) raise NotACache unless slow.respond_to?(:set) && slow.respond_to?(:get) @fast = fast @slow = slow end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth, *args, &block)
Add support for other methods from passed caches this adds support only, but no speed gains.
-
use "strict_" to ensure both caches contain method
61 62 63 64 65 66 67 68 69 |
# File 'lib/duality.rb', line 61 def method_missing(meth, *args, &block) if meth =~ /^strict_(.+)$/ meth = $1 return run_missing_method(meth, *args, &block) if @fast.respond_to?(meth) && @slow.respond_to?(meth) elsif @fast.respond_to?(meth) || @slow.respond_to?(meth) return run_missing_method(meth, *args, &block) end super end |
Instance Method Details
- (Object) delete(key) Also known as: remove
Delete from both - async
23 24 25 |
# File 'lib/duality.rb', line 23 def delete key run_method(:delete, key) end |
- (Object) flush Also known as: clean
Flush caches - async
29 30 31 |
# File 'lib/duality.rb', line 29 def flush run_method(:flush) end |
- (Object) flush_expired Also known as: cleanup
34 35 36 |
# File 'lib/duality.rb', line 34 def flush_expired run_method(:flush_expired) end |
- (Object) get(key) Also known as: load
Get from fast or slow.
-
returns nil if none are found
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/duality.rb', line 41 def get key content = nil begin content = @fast.get(key) rescue Exception end return content unless content.nil? begin content = @slow.get(key) rescue Exception end return content end |
- (Object) set(key, value) Also known as: save
Set to fast and slow.
-
return true if both succeed
-
return false if either fail
17 18 19 |
# File 'lib/duality.rb', line 17 def set key, value run_method(:set, key, value) end |