Class: MetalArchives::Cache::Memory

Inherits:
Base
  • Object
show all
Defined in:
lib/metal_archives/cache/memory.rb

Overview

Generic LRU memory cache

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from MetalArchives::Cache::Base

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metal_archives/cache/memory.rb', line 14

def [](key)
  if keys.include? key
    MetalArchives.config.logger.debug "Cache hit for #{key}"
    keys.delete key
    keys << key
  else
    MetalArchives.config.logger.debug "Cache miss for #{key}"
  end

  cache[key]
end

#[]=(key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/metal_archives/cache/memory.rb', line 26

def []=(key, value)
  cache[key] = value

  keys.delete key if keys.include? key

  keys << key

  pop if keys.size > options[:size]
end

#clearObject



36
37
38
39
# File 'lib/metal_archives/cache/memory.rb', line 36

def clear
  cache.clear
  keys.clear
end

#delete(key) ⇒ Object



45
46
47
# File 'lib/metal_archives/cache/memory.rb', line 45

def delete(key)
  cache.delete key
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/metal_archives/cache/memory.rb', line 41

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

#validate!Object



9
10
11
12
# File 'lib/metal_archives/cache/memory.rb', line 9

def validate!
  raise Errors::InvalidConfigurationError, "size has not been configured" if options[:size].blank?
  raise Errors::InvalidConfigurationError, "size must be a number" unless options[:size].is_a? Integer
end