Class: LruRedux::Cache

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

Direct Known Subclasses

ThreadSafeCache

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/lru_redux/cache.rb', line 4

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

  ignore_nil ||= false

  raise ArgumentError.new(:max_size) unless valid_max_size?(max_size)
  raise ArgumentError.new(:ignore_nil) unless valid_ignore_nil?(ignore_nil)

  @max_size = max_size
  @ignore_nil = ignore_nil
  @data = {}
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
63
64
# File 'lib/lru_redux/cache.rb', line 60

def [](key)
  found = true
  value = @data.delete(key) { found = false }
  @data[key] = value if found
end

#[]=(key, val) ⇒ Object



66
67
68
69
70
71
# File 'lib/lru_redux/cache.rb', line 66

def []=(key, val)
  @data.delete(key)
  @data[key] = val
  @data.shift if @data.length > @max_size
  val # rubocop:disable Lint/Void
end

#clearObject



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

def clear
  @data.clear
end

#countObject



107
108
109
# File 'lib/lru_redux/cache.rb', line 107

def count
  @data.size
end

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



91
92
93
# File 'lib/lru_redux/cache.rb', line 91

def delete(key)
  @data.delete(key)
end

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



73
74
75
76
# File 'lib/lru_redux/cache.rb', line 73

def each(&block)
  array = @data.to_a
  array.reverse!.each(&block)
end

#fetch(key) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/lru_redux/cache.rb', line 50

def fetch(key)
  found = true
  value = @data.delete(key) { found = false }
  if found
    @data[key] = value
  else
    yield if block_given? # rubocop:disable Style/IfInsideElse
  end
end

#getset(key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lru_redux/cache.rb', line 38

def getset(key)
  found = true
  value = @data.delete(key) { found = false }
  if found
    @data[key] = value
  else
    result = @data[key] = yield
    @data.shift if @data.length > @max_size
    result
  end
end

#ignore_nil=(ignore_nil) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/lru_redux/cache.rb', line 31

def ignore_nil=(ignore_nil)
  ignore_nil ||= @ignore_nil
  raise ArgumentError.new(:ignore_nil) unless valid_ignore_nil?(ignore_nil)

  @ignore_nil = ignore_nil
end

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

Returns:

  • (Boolean)


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

def key?(key)
  @data.key?(key)
end

#max_size=(max_size) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
# File 'lib/lru_redux/cache.rb', line 17

def max_size=(max_size)
  max_size ||= @max_size

  raise ArgumentError.new(:max_size) unless valid_max_size?(max_size)

  @max_size = max_size

  @data.shift while @data.size > @max_size
end

#to_aObject



81
82
83
84
# File 'lib/lru_redux/cache.rb', line 81

def to_a
  array = @data.to_a
  array.reverse!
end

#ttl=(_) ⇒ Object



27
28
29
# File 'lib/lru_redux/cache.rb', line 27

def ttl=(_)
  nil
end

#valuesObject



86
87
88
89
# File 'lib/lru_redux/cache.rb', line 86

def values
  vals = @data.values
  vals.reverse!
end