Class: LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/lrucache.rb,
lib/lrucache/version.rb

Overview

Not thread-safe!

Defined Under Namespace

Classes: Datum

Constant Summary collapse

VERSION =
"0.1.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LRUCache

Returns a new instance of LRUCache.



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

def initialize(opts={})
  @max_size = Integer(opts[:max_size] || 100)
  @default = opts[:default]
  @eviction_handler = opts[:eviction_handler]
  @ttl = Float(opts[:ttl] || 0)
  @soft_ttl = Float(opts[:soft_ttl] || 0)
  @retry_delay = Float(opts[:retry_delay] || 0)
  raise "max_size must not be negative" if @max_size < 0
  raise "ttl must not be negative" if @ttl < 0
  raise "soft_ttl must not be negative" if @soft_ttl < 0
  raise "retry_delay must not be negative" if @retry_delay < 0

  @pqueue = PriorityQueue.new
  @data = {}
  @counter = 0
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/lrucache.rb', line 7

def default
  @default
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



7
8
9
# File 'lib/lrucache.rb', line 7

def max_size
  @max_size
end

#retry_delayObject (readonly)

Returns the value of attribute retry_delay.



7
8
9
# File 'lib/lrucache.rb', line 7

def retry_delay
  @retry_delay
end

#soft_ttlObject (readonly)

Returns the value of attribute soft_ttl.



7
8
9
# File 'lib/lrucache.rb', line 7

def soft_ttl
  @soft_ttl
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/lrucache.rb', line 7

def ttl
  @ttl
end

Instance Method Details

#clearObject



26
27
28
29
30
# File 'lib/lrucache.rb', line 26

def clear
  @data.clear
  @pqueue.delete_min until @pqueue.empty?
  @counter = 0 #might as well
end

#delete(key) ⇒ Object



105
106
107
108
109
# File 'lib/lrucache.rb', line 105

def delete(key)
  @pqueue.delete(key)
  datum = @data.delete(key)
  datum.value unless datum.nil?
end

#empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/lrucache.rb', line 93

def empty?
  size == 0
end

#fetch(key, args = {}) ⇒ Object Also known as: []



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
# File 'lib/lrucache.rb', line 56

def fetch(key, args={})
  datum = @data[key]
  if datum.nil?
    if block_given?
      store(key, value = yield, args)
    else
      @default
    end
  elsif datum.expired?
    delete(key)
    if block_given?
      store(key, value = yield, args)
    else
      @default
    end
  elsif datum.soft_expired?
    if block_given?
      begin
        store(key, value = yield, args)
      rescue RuntimeError => e
        access(key)
        ttl, soft_ttl, retry_delay = extract_arguments(args)
        datum.soft_expiration = (Time.now + retry_delay) if retry_delay > 0
        datum.value
      end
    else
      access(key)
      datum.value
    end
  else
    access(key)
    datum.value
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lrucache.rb', line 32

def include?(key)
  datum = @data[key]
  return false if datum.nil?
  if datum.expired?
    delete(key)
    false
  else
    access(key)
    true
  end
end

#keysObject



101
102
103
# File 'lib/lrucache.rb', line 101

def keys
  @data.keys
end

#sizeObject



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

def size
  @data.size
end

#store(key, value, args = {}) ⇒ Object Also known as: []=



44
45
46
47
48
49
50
51
52
# File 'lib/lrucache.rb', line 44

def store(key, value, args={})
  evict_lru! unless @data.include?(key) || @data.size < max_size
  ttl, soft_ttl, retry_delay = extract_arguments(args)
  expiration = expiration_date(ttl)
  soft_expiration = expiration_date(soft_ttl)
  @data[key] = Datum.new(value, expiration, soft_expiration)
  access(key)
  value
end