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, cache = nil)
Refreshes the entry.
-
- (Boolean) valid?(backend)
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 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 97 98 99 100 101 102 103 104 105 106 107 |
# 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 refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @param cache [Cache] The cache where to save the entry. # @return [Float] The new updated_at value. def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end # Checks if the entry is still valid. # # @param backend [Backends::Base] The backend to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(backend) backend.now - updated_at < 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) 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end private # Makes sure a new updated at is generated. # # @param initial [Float] Old value. # @return [Float] New value. def get_new_updated_at(initial) rv = Time.now.to_f rv += 1E6 if rv == initial 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 97 98 99 100 101 102 103 104 105 106 107 |
# 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 refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @param cache [Cache] The cache where to save the entry. # @return [Float] The new updated_at value. def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end # Checks if the entry is still valid. # # @param backend [Backends::Base] The backend to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(backend) backend.now - updated_at < 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) 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end private # Makes sure a new updated at is generated. # # @param initial [Float] Old value. # @return [Float] New value. def get_new_updated_at(initial) rv = Time.now.to_f rv += 1E6 if rv == initial 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 97 98 99 100 101 102 103 104 105 106 107 |
# 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 refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @param cache [Cache] The cache where to save the entry. # @return [Float] The new updated_at value. def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end # Checks if the entry is still valid. # # @param backend [Backends::Base] The backend to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(backend) backend.now - updated_at < 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) 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end private # Makes sure a new updated at is generated. # # @param initial [Float] Old value. # @return [Float] New value. def get_new_updated_at(initial) rv = Time.now.to_f rv += 1E6 if rv == initial 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 97 98 99 100 101 102 103 104 105 106 107 |
# 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 refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @param cache [Cache] The cache where to save the entry. # @return [Float] The new updated_at value. def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end # Checks if the entry is still valid. # # @param backend [Backends::Base] The backend to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(backend) backend.now - updated_at < 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) 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end private # Makes sure a new updated at is generated. # # @param initial [Float] Old value. # @return [Float] New value. def get_new_updated_at(initial) rv = Time.now.to_f rv += 1E6 if rv == initial 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 97 98 99 100 101 102 103 104 105 106 107 |
# 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 refresh end # Refreshes the entry. # # @param save [Boolean] If to save the refresh value in the cache. # @param cache [Cache] The cache where to save the entry. # @return [Float] The new updated_at value. def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end # Checks if the entry is still valid. # # @param backend [Backends::Base] The backend to use for the check. # @return [Boolean] `true` if the entry is still valid, `false` otherwise. def valid?(backend) backend.now - updated_at < 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) 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) rv = ::Elephas::Entry.new(key, rv, hash, ttl) end rv end private # Makes sure a new updated at is generated. # # @param initial [Float] Old value. # @return [Float] New value. def get_new_updated_at(initial) rv = Time.now.to_f rv += 1E6 if rv == initial 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 = .ensure_hash({}) ttl = [[:ttl].to_integer, 0].max hash = [:hash] || ::Elephas::Entry.hashify_key(key) 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) 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, cache = nil)
Refreshes the entry.
46 47 48 49 50 |
# File 'lib/elephas/entry.rb', line 46 def refresh(save = false, cache = nil) @updated_at = get_new_updated_at(@updated_at) cache.write(@hash, self) if save && cache @updated_at end |
- (Boolean) valid?(backend)
Checks if the entry is still valid.
56 57 58 |
# File 'lib/elephas/entry.rb', line 56 def valid?(backend) backend.now - updated_at < ttl / 1000 end |