Module: Kotoshu::Cache::Cache Abstract

Included in:
LookupCache
Defined in:
lib/kotoshu/cache/cache.rb

Overview

This module is abstract.

Subclass must implement #fetch, #write, #read, #delete, #clear

Base cache interface.

All cache implementations should follow this interface.

Instance Method Summary collapse

Instance Method Details

#clearself

This method is abstract.

Subclass must implement

Clear all entries from cache.

Returns:

  • (self)

    Self for chaining

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/kotoshu/cache/cache.rb', line 53

def clear
  raise NotImplementedError
end

#delete(key) ⇒ Object?

This method is abstract.

Subclass must implement

Delete a value from cache.

Parameters:

  • key (Object)

    The cache key

Returns:

  • (Object, nil)

    The deleted value or nil

Raises:

  • (NotImplementedError)


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

def delete(key)
  raise NotImplementedError
end

#fetch(key) { ... } ⇒ Object

This method is abstract.

Subclass must implement

Retrieve a value from cache, or compute it.

Parameters:

  • key (Object)

    The cache key

Yields:

  • Block to compute value on cache miss

Returns:

  • (Object)

    The cached or computed value

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/kotoshu/cache/cache.rb', line 17

def fetch(key, &block)
  raise NotImplementedError
end

#key?(key) ⇒ Boolean

This method is abstract.

Subclass must implement

Check if key exists in cache.

Parameters:

  • key (Object)

    The cache key

Returns:

  • (Boolean)

    True if key exists

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/kotoshu/cache/cache.rb', line 62

def key?(key)
  raise NotImplementedError
end

#read(key) ⇒ Object?

This method is abstract.

Subclass must implement

Read a value from cache.

Parameters:

  • key (Object)

    The cache key

Returns:

  • (Object, nil)

    The cached value or nil

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/kotoshu/cache/cache.rb', line 36

def read(key)
  raise NotImplementedError
end

#reset_statsself

This method is abstract.

Subclass must implement

Reset statistics counters.

Returns:

  • (self)

    Self for chaining

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/kotoshu/cache/cache.rb', line 86

def reset_stats
  raise NotImplementedError
end

#sizeInteger

This method is abstract.

Subclass must implement

Get number of entries in cache.

Returns:

  • (Integer)

    Number of entries

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/kotoshu/cache/cache.rb', line 70

def size
  raise NotImplementedError
end

#statsHash

This method is abstract.

Subclass must implement

Get cache statistics.

Returns:

  • (Hash)

    Statistics including :hits, :misses, :size, :hit_rate

Raises:

  • (NotImplementedError)


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

def stats
  raise NotImplementedError
end

#write(key, value) ⇒ Object

This method is abstract.

Subclass must implement

Write a value to cache.

Parameters:

  • key (Object)

    The cache key

  • value (Object)

    The value to store

Returns:

  • (Object)

    The stored value

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/kotoshu/cache/cache.rb', line 27

def write(key, value)
  raise NotImplementedError
end