Class: Innate::LRUHash

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/innate/lru_hash.rb

Overview

A Hash-alike LRU cache that provides fine-grained control over content restrictions.

It allows you to set:

  • a maximum number of elements

  • the maximum amount of memory used for all elements

  • the allowed memory-size per element

  • time to live

Differences to the original implementation include:

  • The Cache is now a Struct for speed

Copyright © 2002 Yoshinori K. Okuji <[email protected]> Copyright © 2009 Michael Fellinger <[email protected]>

You may redistribute it and/or modify it under the same terms as Ruby.

Defined Under Namespace

Classes: CacheObject

Constant Summary collapse

KeyError =

On 1.8 we raise IndexError, on 1.9 we raise KeyError

Module.const_defined?(:KeyError) ? KeyError : IndexError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &hook) ⇒ LRUHash

Returns a new instance of LRUHash.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/innate/lru_hash.rb', line 27

def initialize(options = {}, &hook)
  self.max_count  = options[:max_count]
  self.expiration = options[:expiration]

  avoid_insane_options

  self.hook = hook

  self.objs = {}
  self.list = []

  self.hits = self.misses = 0
end

Instance Attribute Details

#expirationObject

Returns the value of attribute expiration

Returns:

  • (Object)

    the current value of expiration



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def expiration
  @expiration
end

#hitsObject

Returns the value of attribute hits

Returns:

  • (Object)

    the current value of hits



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def hits
  @hits
end

#hookObject

Returns the value of attribute hook

Returns:

  • (Object)

    the current value of hook



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def hook
  @hook
end

#listObject

Returns the value of attribute list

Returns:

  • (Object)

    the current value of list



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def list
  @list
end

#max_countObject

Returns the value of attribute max_count

Returns:

  • (Object)

    the current value of max_count



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def max_count
  @max_count
end

#missesObject

Returns the value of attribute misses

Returns:

  • (Object)

    the current value of misses



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def misses
  @misses
end

#objsObject

Returns the value of attribute objs

Returns:

  • (Object)

    the current value of objs



18
19
20
# File 'lib/innate/lru_hash.rb', line 18

def objs
  @objs
end

Instance Method Details

#[](key) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/innate/lru_hash.rb', line 70

def [](key)
  expire

  unless objs.key?(key)
    self.misses += 1
    return
  end

  obj = objs[key]
  obj.atime = Time.now.to_i

  list.delete_if{|list_key| key == list_key }
  list << key

  self.hits += 1
  obj.content
end

#[]=(key, obj) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/innate/lru_hash.rb', line 88

def []=(key, obj)
  expire

  delete key if objs.key?(key)

  delete list.first if max_count && max_count == list.size

  objs[key] = CacheObject.new(obj, size, Time.now.to_i)
  list << key

  obj
end

#clearObject Also known as: invalidate_all



53
54
55
56
57
# File 'lib/innate/lru_hash.rb', line 53

def clear
  objs.each{|key, obj| hook.call(key, obj) } if hook
  objs.clear
  list.clear
end

#delete(key) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/innate/lru_hash.rb', line 41

def delete(key)
  return unless objs.key?(key)
  obj = objs[key]

  hook.call(key, obj.content) if hook
  objs.delete key

  list.delete_if{|list_key| key == list_key }

  obj.content
end

#expireObject



60
61
62
63
64
65
66
67
68
# File 'lib/innate/lru_hash.rb', line 60

def expire
  return unless expiration
  now = Time.now.to_i

  list.each_with_index do |key, index|
    break unless (objs[key].atime + expiration) <= now
    delete key
  end
end