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.



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

def initialize(*args)
  max_size, ttl, ignore_nil = args

  max_size ||= 1000
  ttl ||= :none
  ignore_nil ||= false

  validate_max_size!(max_size)
  validate_ttl!(ttl)
  validate_ignore_nil!(ignore_nil)

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

Instance Attribute Details

#ignore_nilObject

Returns the value of attribute ignore_nil.



6
7
8
# File 'lib/lru_redux/ttl/cache.rb', line 6

def ignore_nil
  @ignore_nil
end

#max_sizeObject

Returns the value of attribute max_size.



6
7
8
# File 'lib/lru_redux/ttl/cache.rb', line 6

def max_size
  @max_size
end

#ttlObject

Returns the value of attribute ttl.



6
7
8
# File 'lib/lru_redux/ttl/cache.rb', line 6

def ttl
  @ttl
end

Instance Method Details

#[](key) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lru_redux/ttl/cache.rb', line 85

def [](key)
  evict_expired

  key_found = true
  value = @data_lru.delete(key) { key_found = false }
  return unless key_found

  @data_ttl.delete(key)
  @data_ttl[key] = Time.now.to_f
  @data_lru[key] = value
end

#[]=(key, val) ⇒ Object



97
98
99
100
101
# File 'lib/lru_redux/ttl/cache.rb', line 97

def []=(key, val)
  evict_expired

  store_item(key, val)
end

#clearObject



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

def clear
  @data_ttl.clear
  @data_lru.clear
end

#countObject



142
143
144
# File 'lib/lru_redux/ttl/cache.rb', line 142

def count
  @data_lru.size
end

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



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

def delete(key)
  evict_expired

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

#each(&block) ⇒ Object Also known as: each_unsafe



103
104
105
106
107
# File 'lib/lru_redux/ttl/cache.rb', line 103

def each(&block)
  evict_expired

  @data_lru.to_a.reverse_each(&block)
end

#expireObject



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

def expire
  evict_expired
end

#fetch(key) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lru_redux/ttl/cache.rb', line 70

def fetch(key)
  evict_expired

  key_found = true
  value = @data_lru.delete(key) { key_found = false }

  if key_found
    @data_ttl.delete(key)
    @data_ttl[key] = Time.now.to_f
    @data_lru[key] = value
  else
    yield if block_given? # rubocop:disable Style/IfInsideElse
  end
end

#getset(key) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lru_redux/ttl/cache.rb', line 53

def getset(key)
  evict_expired

  key_found = true
  value = @data_lru.delete(key) { key_found = false }

  if key_found
    @data_ttl.delete(key)
    @data_ttl[key] = Time.now.to_f
    @data_lru[key] = value
  else
    result = yield
    store_item(key, result)
    result
  end
end

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

Returns:

  • (Boolean)


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

def key?(key)
  evict_expired

  @data_lru.key?(key)
end

#to_aObject



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

def to_a
  evict_expired

  @data_lru.to_a.reverse
end

#valuesObject



116
117
118
119
120
# File 'lib/lru_redux/ttl/cache.rb', line 116

def values
  evict_expired

  @data_lru.values.reverse
end