Class: Elephas::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/elephas/entry.rb

Overview

Represents a cache entry.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, hash = nil, ttl = 360000) ⇒ Entry

Creates a new entry.

Parameters:

  • key (String)

    The key for this entry.

  • value (Object)

    The value contained in this entry.

  • hash (String) (defaults to: nil)

    The hash for this entry. Should be unique. It is automatically created if not provided.

  • ttl (Integer) (defaults to: 360000)

    The time to live (TTL) for this entry. If set to 0 then the entry is not cached.



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

#hashString

Returns The hashed (unique) key for this entry.

Returns:

  • (String)

    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, options = {})
    rv = value

    if !rv.is_a?(::Elephas::Entry) then
      options = options.ensure_hash({})

      ttl = [options[:ttl].to_integer, 0].max
      hash = options[: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

#keyString

Returns The key for this entry.

Returns:

  • (String)

    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, options = {})
    rv = value

    if !rv.is_a?(::Elephas::Entry) then
      options = options.ensure_hash({})

      ttl = [options[:ttl].to_integer, 0].max
      hash = options[: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

#ttlFixnum

Returns The expected TTL of the entry, in milliseconds.

Returns:

  • (Fixnum)

    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, options = {})
    rv = value

    if !rv.is_a?(::Elephas::Entry) then
      options = options.ensure_hash({})

      ttl = [options[:ttl].to_integer, 0].max
      hash = options[: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

#updated_atFixnum

Returns The last update date of the entry, in UNIX timestamp (with milliseconds).

Returns:

  • (Fixnum)

    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, options = {})
    rv = value

    if !rv.is_a?(::Elephas::Entry) then
      options = options.ensure_hash({})

      ttl = [options[:ttl].to_integer, 0].max
      hash = options[: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

#valueObject

Returns The value contained in this entry.

Returns:

  • (Object)

    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, options = {})
    rv = value

    if !rv.is_a?(::Elephas::Entry) then
      options = options.ensure_hash({})

      ttl = [options[:ttl].to_integer, 0].max
      hash = options[: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

.ensure(value, key, options = {}) ⇒ Entry

Ensures that the value is an Entry.

Parameters:

  • value (Object)

    The object to check.

  • key (Object)

    The key associated to this object.

  • options (Hash) (defaults to: {})

    Options to manage the value.

Returns:

  • (Entry)

    The wrapped object.



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, options = {})
  rv = value

  if !rv.is_a?(::Elephas::Entry) then
    options = options.ensure_hash({})

    ttl = [options[:ttl].to_integer, 0].max
    hash = options[:hash] || ::Elephas::Entry.hashify_key(key)

    rv = ::Elephas::Entry.new(key, rv, hash, ttl)
  end

  rv
end

.hashify_key(key) ⇒ String

Returns a unique hash for the key.

Parameters:

  • key (String)

    The key to hashify.

Returns:

  • (String)

    An 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

#==(other) ⇒ Boolean

Compares to another Entry.

Parameters:

  • other (Entry)

    The entry to compare with

Returns:

  • (Boolean)

    true if the entries are the same, false otherwise.



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

#refresh(save = false, cache = nil) ⇒ Float

Refreshes the entry.

Parameters:

  • save (Boolean) (defaults to: false)

    If to save the refresh value in the cache.

  • cache (Cache) (defaults to: nil)

    The cache where to save the entry.

Returns:

  • (Float)

    The new updated_at value.



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

#valid?(backend) ⇒ Boolean

Checks if the entry is still valid.

Parameters:

Returns:

  • (Boolean)

    true if the entry is still valid, false otherwise.



56
57
58
# File 'lib/elephas/entry.rb', line 56

def valid?(backend)
  backend.now - updated_at < ttl / 1000
end