Class: Elephas::Entry
- Inherits:
-
Object
- Object
- Elephas::Entry
- Defined in:
- lib/elephas/entry.rb
Overview
Represents a cache entry.
Instance Attribute Summary (collapse)
-
- (String) hash
The hashed (unique) key for this entry.
-
- (String) key
The key for this entry.
-
- (Fixnum) ttl
The expected TTL of the entry, in milliseconds.
-
- (Fixnum) updated_at
The last update date of the entry, in UNIX timestamp (with milliseconds).
-
- (Object) value
The value contained in this entry.
Class Method Summary (collapse)
-
+ (Entry) ensure(value, key, options = {})
Ensures that the value is an Entry.
-
+ (String) hashify_key(key)
Returns a unique hash for the key.
Instance Method Summary (collapse)
-
- (Boolean) ==(other)
Compares to another Entry.
-
- (Entry) initialize(key, value, hash = nil, ttl = 360000)
constructor
Creates a new entry.
-
- (Float) refresh(save = false)
Refreshes the entry.
-
- (Boolean) valid?(provider = nil)
Checks if the entry is still valid.
Constructor Details
- (Entry) initialize(key, value, hash = nil, ttl = 360000)
Creates a new entry.
33 34 35 36 37 38 39 |
# File 'lib/elephas/entry.rb', line 33 def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end |
Instance Attribute Details
- (String) hash
The hashed (unique) key for this entry.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/elephas/entry.rb', line 20 class Entry attr_accessor :key attr_accessor :hash attr_accessor :value attr_accessor :ttl attr_accessor :updated_at # Creates a new entry. # # @param key [String] The key for this entry. # @param value [Object] The value contained in this entry. # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided. # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached. def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @return [Float] The new updated_at value. def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end # Checks if the entry is still valid. # # @param provider [Provider::Base] The provider to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end # Compares to another Entry. # # @param other [Entry] The entry to compare with # @return [Boolean] `true` if the entries are the same, `false` otherwise. def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end # Returns a unique hash for the key. # # @param key [String] The key to hashify. # @return [String] An unique hash for the key. def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end # Ensures that the value is an Entry. # # @param value [Object] The object to check. # @param key [Object] The key associated to this object. # @param options [Hash] Options to manage the value. # @return [Entry] The wrapped object. def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end end |
- (String) key
The key for this entry.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/elephas/entry.rb', line 20 class Entry attr_accessor :key attr_accessor :hash attr_accessor :value attr_accessor :ttl attr_accessor :updated_at # Creates a new entry. # # @param key [String] The key for this entry. # @param value [Object] The value contained in this entry. # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided. # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached. def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @return [Float] The new updated_at value. def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end # Checks if the entry is still valid. # # @param provider [Provider::Base] The provider to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end # Compares to another Entry. # # @param other [Entry] The entry to compare with # @return [Boolean] `true` if the entries are the same, `false` otherwise. def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end # Returns a unique hash for the key. # # @param key [String] The key to hashify. # @return [String] An unique hash for the key. def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end # Ensures that the value is an Entry. # # @param value [Object] The object to check. # @param key [Object] The key associated to this object. # @param options [Hash] Options to manage the value. # @return [Entry] The wrapped object. def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end end |
- (Fixnum) ttl
The expected TTL of the entry, in milliseconds.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/elephas/entry.rb', line 20 class Entry attr_accessor :key attr_accessor :hash attr_accessor :value attr_accessor :ttl attr_accessor :updated_at # Creates a new entry. # # @param key [String] The key for this entry. # @param value [Object] The value contained in this entry. # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided. # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached. def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @return [Float] The new updated_at value. def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end # Checks if the entry is still valid. # # @param provider [Provider::Base] The provider to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end # Compares to another Entry. # # @param other [Entry] The entry to compare with # @return [Boolean] `true` if the entries are the same, `false` otherwise. def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end # Returns a unique hash for the key. # # @param key [String] The key to hashify. # @return [String] An unique hash for the key. def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end # Ensures that the value is an Entry. # # @param value [Object] The object to check. # @param key [Object] The key associated to this object. # @param options [Hash] Options to manage the value. # @return [Entry] The wrapped object. def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end end |
- (Fixnum) updated_at
The last update date of the entry, in UNIX timestamp (with milliseconds).
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/elephas/entry.rb', line 20 class Entry attr_accessor :key attr_accessor :hash attr_accessor :value attr_accessor :ttl attr_accessor :updated_at # Creates a new entry. # # @param key [String] The key for this entry. # @param value [Object] The value contained in this entry. # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided. # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached. def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @return [Float] The new updated_at value. def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end # Checks if the entry is still valid. # # @param provider [Provider::Base] The provider to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end # Compares to another Entry. # # @param other [Entry] The entry to compare with # @return [Boolean] `true` if the entries are the same, `false` otherwise. def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end # Returns a unique hash for the key. # # @param key [String] The key to hashify. # @return [String] An unique hash for the key. def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end # Ensures that the value is an Entry. # # @param value [Object] The object to check. # @param key [Object] The key associated to this object. # @param options [Hash] Options to manage the value. # @return [Entry] The wrapped object. def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end end |
- (Object) value
The value contained in this entry.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/elephas/entry.rb', line 20 class Entry attr_accessor :key attr_accessor :hash attr_accessor :value attr_accessor :ttl attr_accessor :updated_at # Creates a new entry. # # @param key [String] The key for this entry. # @param value [Object] The value contained in this entry. # @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided. # @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached. def initialize(key, value, hash = nil, ttl = 360000) @key = key @hash = hash.present? ? hash : self.class.hashify_key(key) @value = value @ttl = ttl self.refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @return [Float] The new updated_at value. def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end # Checks if the entry is still valid. # # @param provider [Provider::Base] The provider to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end # Compares to another Entry. # # @param other [Entry] The entry to compare with # @return [Boolean] `true` if the entries are the same, `false` otherwise. def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end # Returns a unique hash for the key. # # @param key [String] The key to hashify. # @return [String] An unique hash for the key. def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end # Ensures that the value is an Entry. # # @param value [Object] The object to check. # @param key [Object] The key associated to this object. # @param options [Hash] Options to manage the value. # @return [Entry] The wrapped object. def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end end |
Class Method Details
+ (Entry) ensure(value, key, options = {})
Ensures that the value is an Entry.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/elephas/entry.rb', line 82 def self.ensure(value, key, = {}) rv = value if !rv.is_a?(::Elephas::Entry) then = {} if !.is_a?(Hash) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key.ensure_string) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end |
+ (String) hashify_key(key)
Returns a unique hash for the key.
72 73 74 |
# File 'lib/elephas/entry.rb', line 72 def self.hashify_key(key) Digest::SHA2.hexdigest(key.ensure_string) end |
Instance Method Details
- (Boolean) ==(other)
Compares to another Entry.
64 65 66 |
# File 'lib/elephas/entry.rb', line 64 def ==(other) other.is_a?(::Elephas::Entry) && [@key, @hash, @value] == [other.key, other.hash, other.value] end |
- (Float) refresh(save = false)
Refreshes the entry.
45 46 47 48 49 |
# File 'lib/elephas/entry.rb', line 45 def refresh(save = false) @updated_at = Time.now.to_f Elephas::Cache.provider.write(@hash, self) if save @updated_at end |
- (Boolean) valid?(provider = nil)
Checks if the entry is still valid.
55 56 57 58 |
# File 'lib/elephas/entry.rb', line 55 def valid?(provider = nil) provider ||= ::Elephas::Cache.provider provider.now - self.updated_at < self.ttl / 1000 end |