Class: CountedCacheItem

Inherits:
Object
  • Object
show all
Defined in:
lib/counted_cache/counted_cache_item.rb

Overview

A container for items in a counted cache.

Constant Summary collapse

EMPTY =

A marker for empty items.

:counted_cache_item_empty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CountedCacheItem

Setup an empty data item.



14
15
16
17
18
# File 'lib/counted_cache/counted_cache_item.rb', line 14

def initialize(key)
  @data  = EMPTY
  @key   = key
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

The reference count of this item.



8
9
10
# File 'lib/counted_cache/counted_cache_item.rb', line 8

def count
  @count
end

#dataObject

Retrieve the data, maintain a reference count.



32
33
34
35
# File 'lib/counted_cache/counted_cache_item.rb', line 32

def data
  @count += 1
  @data
end

Instance Method Details

#empty?Boolean

Is this item empty?



21
22
23
# File 'lib/counted_cache/counted_cache_item.rb', line 21

def empty?
  @data == EMPTY
end

#purge(save_block) ⇒ Object

Erase the data associated with the given key.



26
27
28
29
# File 'lib/counted_cache/counted_cache_item.rb', line 26

def purge(save_block)
  save_block.call(@key, @data)
  @data = EMPTY
end