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
-
#initialize(options = {}) ⇒ CachedResult
constructor
A new instance of CachedResult.
- #method_signature ⇒ Object
- #obj_hash ⇒ Object
- #ttl ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CachedResult
Returns a new instance of 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
48 49 50 |
# File 'lib/cache_method/cached_result.rb', line 48 def args_digest @args_digest ||= ::Digest::MD5.hexdigest(args.flatten.join) end |
#cache_key ⇒ Object
36 37 38 |
# File 'lib/cache_method/cached_result.rb', line 36 def cache_key [ method_signature, current_epoch, obj_hash, args_digest ].join ',' end |
#current_epoch ⇒ Object
52 53 54 |
# File 'lib/cache_method/cached_result.rb', line 52 def current_epoch @current_epoch ||= Epoch.current(:obj => obj, :method_id => method_id) end |
#fetch ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/cache_method/cached_result.rb', line 22 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
40 41 42 |
# File 'lib/cache_method/cached_result.rb', line 40 def method_signature @method_signature ||= ::CacheMethod.method_signature(obj, method_id) end |
#obj_hash ⇒ Object
44 45 46 |
# File 'lib/cache_method/cached_result.rb', line 44 def obj_hash @obj_hash ||= obj.hash end |
#ttl ⇒ Object
32 33 34 |
# File 'lib/cache_method/cached_result.rb', line 32 def ttl @ttl ||= Config.instance.default_ttl end |