Class: DS::Util::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/util/cache.rb

Constant Summary collapse

DEFAULT_MAX_SIZE =
10
UNLIMITED_SIZE =
Float::INFINITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ void

Initializes a new instance of the class with the specified maximum size.

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_MAX_SIZE)

    (DEFAULT_MAX_SIZE) the maximum size of the cache



17
18
19
20
# File 'lib/ds/util/cache.rb', line 17

def initialize max_size: DEFAULT_MAX_SIZE
  @max_size = max_size
  @items    = {}
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



10
11
12
# File 'lib/ds/util/cache.rb', line 10

def items
  @items
end

#keysArray<Object> (readonly)

Returns an array of all the keys in the cache.

Returns:

  • (Array<Object>)

    An array of keys.



78
79
80
# File 'lib/ds/util/cache.rb', line 78

def keys
  @keys
end

#max_sizeObject

Returns the value of attribute max_size.



9
10
11
# File 'lib/ds/util/cache.rb', line 9

def max_size
  @max_size
end

Instance Method Details

#[](key) ⇒ Object

Retrieves an item from the cache using the specified key.

Parameters:

  • key (Object)

    The key used to identify the item in the cache.

Returns:

  • (Object)

    The item associated with the specified key, or nil if the key is not present in the cache.



71
72
73
# File 'lib/ds/util/cache.rb', line 71

def [](key)
  get_item key
end

#add(key, item) ⇒ Object

Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.

Parameters:

  • key (Object)

    the key used to identify the item in the cache

  • item (Object)

    the item to be added to the cache

Returns:

  • (Object)

    the item that was added to the cache



37
38
39
40
41
42
# File 'lib/ds/util/cache.rb', line 37

def add key, item
  delete_item key
  items[key] = item
  cleanup
  item
end

#cleanupvoid

This method returns an undefined value.

Cleanup the cache by removing items until the size is less than or equal to the maximum size.

This method does not take any parameters.



102
103
104
105
106
107
108
# File 'lib/ds/util/cache.rb', line 102

def cleanup
  return if size < max_size
  return if keys.blank? # don't allow an infinite loop
  while size > max_size
    delete_item keys.first
  end
end

#delete_item(key) ⇒ void

This method returns an undefined value.

Deletes an item from the cache using the specified key.

Parameters:

  • key (Object)

    The key used to identify the item in the cache.



93
94
95
# File 'lib/ds/util/cache.rb', line 93

def delete_item key
  items.delete key
end

#get_item(key) ⇒ Object

Retrieves an item from the cache using the specified key.

Parameters:

  • key (Object)

    The key used to identify the item in the cache.

Returns:

  • (Object)

    The item associated with the specified key, or nil if the key is not present in the cache.



63
64
65
# File 'lib/ds/util/cache.rb', line 63

def get_item key
  items[key]
end

#get_or_add(key, item) ⇒ Object

Adds an item to the cache if it is not already present, or if the cache is not limited and the item is not already present.

Parameters:

  • key (Object)

    the key used to identify the item in the cache

  • item (Object)

    the item to be added to the cache

Returns:

  • (Object)

    the item that was added to the cache



27
28
29
30
# File 'lib/ds/util/cache.rb', line 27

def get_or_add key, item
  add(key, item) unless include? key && unlimited?
  get_item key
end

#include?(key) ⇒ Boolean

Checks if the given key is present in the cache.

Parameters:

  • key (Object)

    the key to check for in the cache

Returns:

  • (Boolean)

    true if the key is present in the cache, false otherwise



48
49
50
# File 'lib/ds/util/cache.rb', line 48

def include? key
  keys.include? key
end

#sizeInteger

Returns the number of items in the cache.

Returns:

  • (Integer)

    The number of items in the cache.



85
86
87
# File 'lib/ds/util/cache.rb', line 85

def size
  keys.size
end

#unlimited?Boolean

Checks if the cache is unlimited.

Returns:

  • (Boolean)

    true if the cache is unlimited, false otherwise



55
56
57
# File 'lib/ds/util/cache.rb', line 55

def unlimited?
  max_size == UNLIMITED_SIZE
end