Class: CacheLib::BasicCache

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_lib/basic_cache.rb

Direct Known Subclasses

FifoCache, LirsCache, LruCache, SafeBasicCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicCache

Returns a new instance of BasicCache.



5
6
7
8
9
# File 'lib/cache_lib/basic_cache.rb', line 5

def initialize
  @limit = nil

  @cache = UtilHash.new
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



3
4
5
# File 'lib/cache_lib/basic_cache.rb', line 3

def limit
  @limit
end

Instance Method Details

#clearObject



56
57
58
59
# File 'lib/cache_lib/basic_cache.rb', line 56

def clear
  @cache.clear
  nil
end

#eachObject



61
62
63
64
65
# File 'lib/cache_lib/basic_cache.rb', line 61

def each
  @cache.each do |pair|
    yield pair
  end
end

#evict(key) ⇒ Object



52
53
54
# File 'lib/cache_lib/basic_cache.rb', line 52

def evict(key)
  @cache.delete(key)
end

#fetch(key) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/cache_lib/basic_cache.rb', line 41

def fetch(key)
  has_key = true
  value = @cache.fetch(key) { has_key = false }

  if has_key
    value
  else
    yield if block_given?
  end
end

#get(key) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/cache_lib/basic_cache.rb', line 23

def get(key)
  value = hit(key)

  if value
    value
  else
    miss(key, yield)
  end
end

#initialize_copy(source) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/cache_lib/basic_cache.rb', line 11

def initialize_copy(source)
  source_raw = source.raw

  @limit = source_raw[:limit]

  @cache = source_raw[:cache]
end

#inspectObject



92
93
94
# File 'lib/cache_lib/basic_cache.rb', line 92

def inspect
  "#{self.class} currently caching #{@cache.size} items."
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/cache_lib/basic_cache.rb', line 67

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

#keysObject



75
76
77
# File 'lib/cache_lib/basic_cache.rb', line 75

def keys
  @cache.keys.reverse!
end

#lookup(key) ⇒ Object Also known as: []



37
38
39
# File 'lib/cache_lib/basic_cache.rb', line 37

def lookup(key)
  @cache[key]
end

#rawObject



87
88
89
90
# File 'lib/cache_lib/basic_cache.rb', line 87

def raw
  { limit: @limit,
    cache: @cache.clone }
end

#sizeObject



83
84
85
# File 'lib/cache_lib/basic_cache.rb', line 83

def size
  @cache.size
end

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



33
34
35
# File 'lib/cache_lib/basic_cache.rb', line 33

def store(key, value)
  miss(key, value)
end

#to_aObject



71
72
73
# File 'lib/cache_lib/basic_cache.rb', line 71

def to_a
  @cache.to_a.reverse!
end

#valuesObject



79
80
81
# File 'lib/cache_lib/basic_cache.rb', line 79

def values
  @cache.values.reverse!
end