Class: LruRedux::TTL::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/lru_redux/ttl/cache.rb

Direct Known Subclasses

ThreadSafeCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lru_redux/ttl/cache.rb', line 6

def initialize(*args)
  max_size, ttl = args

  ttl ||= :none

  raise ArgumentError.new(:max_size) if
      max_size < 1
  raise ArgumentError.new(:ttl) unless
      ttl == :none || ((ttl.is_a? Numeric) && ttl >= 0)

  @max_size = max_size
  @ttl = ttl
  @data_lru = {}
  @data_ttl = {}
end

Instance Attribute Details

#max_sizeObject

Returns the value of attribute max_size.



4
5
6
# File 'lib/lru_redux/ttl/cache.rb', line 4

def max_size
  @max_size
end

#ttlObject

Returns the value of attribute ttl.



4
5
6
# File 'lib/lru_redux/ttl/cache.rb', line 4

def ttl
  @ttl
end

Instance Method Details

#[](key) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lru_redux/ttl/cache.rb', line 78

def [](key)
  ttl_evict

  found = true
  value = @data_lru.delete(key){ found = false }
  if found
    @data_lru[key] = value
  else
    nil
  end
end

#[]=(key, val) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lru_redux/ttl/cache.rb', line 90

def []=(key, val)
  ttl_evict

  @data_lru.delete(key)
  @data_ttl.delete(key)

  @data_lru[key] = val
  @data_ttl[key] = Time.now.to_f

  if @data_lru.size > @max_size
    key, _ = @data_lru.first

    @data_ttl.delete(key)
    @data_lru.delete(key)
  end

  val
end

#clearObject



145
146
147
148
# File 'lib/lru_redux/ttl/cache.rb', line 145

def clear
  @data_lru.clear
  @data_ttl.clear
end

#countObject



154
155
156
# File 'lib/lru_redux/ttl/cache.rb', line 154

def count
  @data_lru.size
end

#delete(key) ⇒ Object Also known as: evict



128
129
130
131
132
133
# File 'lib/lru_redux/ttl/cache.rb', line 128

def delete(key)
  ttl_evict

  @data_lru.delete(key)
  @data_ttl.delete(key)
end

#eachObject Also known as: each_unsafe



109
110
111
112
113
114
115
116
# File 'lib/lru_redux/ttl/cache.rb', line 109

def each
  ttl_evict

  array = @data_lru.to_a
  array.reverse!.each do |pair|
    yield pair
  end
end

#expireObject



150
151
152
# File 'lib/lru_redux/ttl/cache.rb', line 150

def expire
  ttl_evict
end

#fetch(key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lru_redux/ttl/cache.rb', line 66

def fetch(key)
  ttl_evict

  found = true
  value = @data_lru.delete(key){ found = false }
  if found
    @data_lru[key] = value
  else
    yield if block_given?
  end
end

#getset(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lru_redux/ttl/cache.rb', line 44

def getset(key)
  ttl_evict

  found = true
  value = @data_lru.delete(key){ found = false }
  if found
    @data_lru[key] = value
  else
    result = @data_lru[key] = yield
    @data_ttl[key] = Time.now.to_f

    if @data_lru.size > @max_size
      key, _ = @data_lru.first

      @data_ttl.delete(key)
      @data_lru.delete(key)
    end

    result
  end
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/lru_redux/ttl/cache.rb', line 137

def key?(key)
  ttl_evict

  @data_lru.key?(key)
end

#to_aObject



121
122
123
124
125
126
# File 'lib/lru_redux/ttl/cache.rb', line 121

def to_a
  ttl_evict

  array = @data_lru.to_a
  array.reverse!
end