Class: Dragonfly::SimpleCache

Inherits:
Hash
  • Object
show all
Defined in:
lib/dragonfly/simple_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ SimpleCache

Returns a new instance of SimpleCache.



4
5
6
7
# File 'lib/dragonfly/simple_cache.rb', line 4

def initialize(max_size)
  @max_size = max_size
  @keys = []
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size



9
10
11
# File 'lib/dragonfly/simple_cache.rb', line 9

def max_size
  @max_size
end

Instance Method Details

#[]=(key, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/dragonfly/simple_cache.rb', line 11

def []=(key, value)
  if !has_key?(key)
    @keys << key
    if size == max_size
      key_to_purge = @keys.shift
      delete(key_to_purge)
    end
  end
  super
end