Class: Factorix::Cache::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/cache/base.rb

Overview

This class is abstract.

Subclasses must implement all abstract methods.

Abstract base class for cache backends.

All cache backends (FileSystem, S3, Redis) inherit from this class and implement the abstract methods defined here.

Direct Known Subclasses

FileSystem, Redis, S3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl: nil) ⇒ Base

Initialize a new cache backend.

Parameters:

  • ttl (Integer, nil) (defaults to: nil)

    time-to-live in seconds (nil for unlimited)



18
19
20
# File 'lib/factorix/cache/base.rb', line 18

def initialize(ttl: nil)
  @ttl = ttl
end

Instance Attribute Details

#ttlInteger? (readonly)

Returns time-to-live in seconds (nil for unlimited).

Returns:

  • (Integer, nil)

    time-to-live in seconds (nil for unlimited)



13
14
15
# File 'lib/factorix/cache/base.rb', line 13

def ttl
  @ttl
end

Instance Method Details

#age(key) ⇒ Float?

This method is abstract.

Get the age of a cache entry in seconds.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (Float, nil)

    age in seconds, or nil if entry doesn’t exist

Raises:

  • (NotImplementedError)


80
# File 'lib/factorix/cache/base.rb', line 80

def age(key) = raise NotImplementedError, "#{self.class}#age must be implemented"

#backend_infoHash

Return backend-specific information.

Subclasses should override this method to provide configuration details specific to their backend implementation.

Returns:

  • (Hash)

    backend-specific information (empty by default)



113
# File 'lib/factorix/cache/base.rb', line 113

def backend_info = {}

#clearvoid

This method is abstract.

This method returns an undefined value.

Clear all cache entries.

Raises:

  • (NotImplementedError)


66
# File 'lib/factorix/cache/base.rb', line 66

def clear = raise NotImplementedError, "#{self.class}#clear must be implemented"

#delete(key) ⇒ Boolean

This method is abstract.

Delete a cache entry.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (Boolean)

    true if deleted, false if not found

Raises:

  • (NotImplementedError)


60
# File 'lib/factorix/cache/base.rb', line 60

def delete(key) = raise NotImplementedError, "#{self.class}#delete must be implemented"

#each {|key, entry| ... } ⇒ Enumerator

This method is abstract.

Enumerate cache entries.

Yields [key, entry] pairs similar to Hash#each.

Yields:

  • (key, entry)

    logical key and Entry object

Yield Parameters:

  • key (String)

    logical cache key

  • entry (Entry)

    cache entry metadata

Returns:

  • (Enumerator)

    if no block given

Raises:

  • (NotImplementedError)


105
# File 'lib/factorix/cache/base.rb', line 105

def each = raise NotImplementedError, "#{self.class}#each must be implemented"

#exist?(key) ⇒ Boolean

This method is abstract.

Check if a cache entry exists and is not expired.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (Boolean)

    true if the cache entry exists and is valid

Raises:

  • (NotImplementedError)


27
# File 'lib/factorix/cache/base.rb', line 27

def exist?(key) = raise NotImplementedError, "#{self.class}#exist? must be implemented"

#expired?(key) ⇒ Boolean

This method is abstract.

Check if a cache entry has expired based on TTL.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (Boolean)

    true if expired, false otherwise

Raises:

  • (NotImplementedError)


87
# File 'lib/factorix/cache/base.rb', line 87

def expired?(key) = raise NotImplementedError, "#{self.class}#expired? must be implemented"

#read(key) ⇒ String?

This method is abstract.

Read a cached entry as a string.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (String, nil)

    cached content or nil if not found/expired

Raises:

  • (NotImplementedError)


34
# File 'lib/factorix/cache/base.rb', line 34

def read(key) = raise NotImplementedError, "#{self.class}#read must be implemented"

#size(key) ⇒ Integer?

This method is abstract.

Get the size of a cached entry in bytes.

Parameters:

  • key (String)

    logical cache key

Returns:

  • (Integer, nil)

    size in bytes, or nil if entry doesn’t exist/expired

Raises:

  • (NotImplementedError)


94
# File 'lib/factorix/cache/base.rb', line 94

def size(key) = raise NotImplementedError, "#{self.class}#size must be implemented"

#store(key, src) ⇒ Boolean

This method is abstract.

Store data in the cache.

Parameters:

  • key (String)

    logical cache key

  • src (Pathname)

    path to the source file

Returns:

  • (Boolean)

    true if stored successfully

Raises:

  • (NotImplementedError)


53
# File 'lib/factorix/cache/base.rb', line 53

def store(key, src) = raise NotImplementedError, "#{self.class}#store must be implemented"

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

This method is abstract.

Execute a block with an exclusive lock on the cache entry.

Parameters:

  • key (String)

    logical cache key

Yields:

  • block to execute with lock held

Raises:

  • (NotImplementedError)


73
# File 'lib/factorix/cache/base.rb', line 73

def with_lock(key) = raise NotImplementedError, "#{self.class}#with_lock must be implemented"

#write_to(key, output) ⇒ Boolean

This method is abstract.

Write cached content to a file.

Unlike #read which returns content as a String, this method writes directly to a file path, which is more memory-efficient for large files.

Parameters:

  • key (String)

    logical cache key

  • output (Pathname)

    path to write the cached content

Returns:

  • (Boolean)

    true if written successfully, false if not found/expired

Raises:

  • (NotImplementedError)


45
# File 'lib/factorix/cache/base.rb', line 45

def write_to(key, output) = raise NotImplementedError, "#{self.class}#write_to must be implemented"