Class: CacheLib::BasicCache

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

Direct Known Subclasses

FifoCache, LirsCache, LruCache, SafeBasicCache, TtlCache

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



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

def clear
  @cache.clear
  nil
end

#eachObject



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

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

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



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

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

#expireObject



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

def expire
  nil
end

#fetch(key) ⇒ Object



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

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
32
# File 'lib/cache_lib/basic_cache.rb', line 23

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

  if has_key
    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



97
98
99
100
101
# File 'lib/cache_lib/basic_cache.rb', line 97

def inspect
  "#{self.class}, "\
  "Limit: #{@limit}, "\
  "Size: #{@cache.size}"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @cache.keys.reverse!
end

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



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

def lookup(key)
  @cache[key]
end

#rawObject



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

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

#sizeObject



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

def size
  @cache.size
end

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



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

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

#to_aObject



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

def to_a
  @cache.to_a.reverse!
end

#valuesObject



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

def values
  @cache.values.reverse!
end