Class: CacheMethod::CachedResult
- Inherits:
-
Object
- Object
- CacheMethod::CachedResult
- Defined in:
- lib/cache_method/cached_result.rb
Overview
:nodoc: all
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#method_id ⇒ Object
readonly
Returns the value of attribute method_id.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#original_method_id ⇒ Object
readonly
Returns the value of attribute original_method_id.
Class Method Summary collapse
Instance Method Summary collapse
- #args_digest ⇒ Object
- #cache_key ⇒ Object
- #current_epoch ⇒ Object
-
#fetch ⇒ Object
Store things wrapped in an Array so that nil is accepted.
-
#initialize(options = {}) ⇒ CachedResult
constructor
A new instance of CachedResult.
- #method_signature ⇒ Object
- #obj_hash ⇒ Object
- #ttl ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CachedResult
11 12 13 14 15 |
# File 'lib/cache_method/cached_result.rb', line 11 def initialize( = {}) .each do |k, v| instance_variable_set "@#{k}", v end end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
20 21 22 |
# File 'lib/cache_method/cached_result.rb', line 20 def args @args end |
#method_id ⇒ Object (readonly)
Returns the value of attribute method_id.
18 19 20 |
# File 'lib/cache_method/cached_result.rb', line 18 def method_id @method_id end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
17 18 19 |
# File 'lib/cache_method/cached_result.rb', line 17 def obj @obj end |
#original_method_id ⇒ Object (readonly)
Returns the value of attribute original_method_id.
19 20 21 |
# File 'lib/cache_method/cached_result.rb', line 19 def original_method_id @original_method_id end |
Class Method Details
.fetch(options = {}) ⇒ Object
5 6 7 8 |
# File 'lib/cache_method/cached_result.rb', line 5 def fetch( = {}) cached_result = new cached_result.fetch end |
Instance Method Details
#args_digest ⇒ Object
53 54 55 |
# File 'lib/cache_method/cached_result.rb', line 53 def args_digest @args_digest ||= args.empty? ? 'empty' : ::Digest::MD5.hexdigest(args.join) end |
#cache_key ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/cache_method/cached_result.rb', line 37 def cache_key if obj.is_a? ::Class [ 'CacheMethod', 'CachedResult', method_signature, current_epoch, args_digest ].join ',' else [ 'CacheMethod', 'CachedResult', method_signature, obj_hash, current_epoch, args_digest ].join ',' end end |
#current_epoch ⇒ Object
57 58 59 |
# File 'lib/cache_method/cached_result.rb', line 57 def current_epoch @current_epoch ||= Epoch.current(:obj => obj, :method_id => method_id) end |
#fetch ⇒ Object
Store things wrapped in an Array so that nil is accepted
23 24 25 26 27 28 29 30 31 |
# File 'lib/cache_method/cached_result.rb', line 23 def fetch if v = Config.instance.storage.get(cache_key) and v.is_a?(::Array) v[0] else v = obj.send original_method_id, *args Config.instance.storage.set cache_key, [v], ttl v end end |
#method_signature ⇒ Object
45 46 47 |
# File 'lib/cache_method/cached_result.rb', line 45 def method_signature @method_signature ||= ::CacheMethod.method_signature(obj, method_id) end |
#obj_hash ⇒ Object
49 50 51 |
# File 'lib/cache_method/cached_result.rb', line 49 def obj_hash @obj_hash ||= obj.hash end |
#ttl ⇒ Object
33 34 35 |
# File 'lib/cache_method/cached_result.rb', line 33 def ttl @ttl ||= Config.instance.default_ttl end |