Class: LruRedux::Cache

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

Overview

Ruby 1.9 makes our life easier, Hash is already ordered

This is an ultra efficient 1.9 freindly implementation

Direct Known Subclasses

ThreadSafeCache

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cache

Returns a new instance of Cache.

Raises:

  • (ArgumentError)


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

def initialize(*args)
  max_size, _ = args

  raise ArgumentError.new(:max_size) if max_size < 1

  @max_size = max_size
  @data = {}
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, val) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lru_redux/cache_legacy.rb', line 28

def []=(key,val)
  @data.delete(key)
  @data[key] = val
  # this may seem odd see: http://bugs.ruby-lang.org/issues/8312
  @data.delete(@data.first[0]) if @data.length > @max_size
  val
end

#clearObject



94
95
96
# File 'lib/lru_redux/cache.rb', line 94

def clear
  @data.clear
end

#countObject



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

def count
  @data.size
end

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



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

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

#eachObject Also known as: each_unsafe



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

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

#fetch(key) ⇒ Object



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

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

#getset(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lru_redux/cache_legacy.rb', line 15

def getset(key)
  found = true
  value = @data.delete(key){ found = false }
  if found
    @data[key] = value
  else
    result = @data[key] = yield
    # this may seem odd see: http://bugs.ruby-lang.org/issues/8312
    @data.delete(@data.first[0]) if @data.length > @max_size
    result
  end
end

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

Returns:

  • (Boolean)


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

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

#max_size=(max_size) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/lru_redux/cache_legacy.rb', line 5

def max_size=(size)
  raise ArgumentError.new(:max_size) if @max_size < 1
  @max_size = size
  if @max_size < @data.size
    @data.keys[0..@max_size-@data.size].each do |k|
      @data.delete(k)
    end
  end
end

#to_aObject



77
78
79
80
# File 'lib/lru_redux/cache.rb', line 77

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

#ttl=(_) ⇒ Object



24
25
26
# File 'lib/lru_redux/cache.rb', line 24

def ttl=(_)
  nil
end