Class: OllamaChat::RedisCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ollama_chat/redis_cache.rb

Overview

A Redis-based cache implementation for OllamaChat

This class provides a wrapper around Redis that offers a simple key-value caching interface with support for expiration times and namespace isolation. It’s designed to be used as a cache backend for various components in the OllamaChat application.

Examples:

Basic usage

cache = OllamaChat::RedisCache.new(prefix: 'myapp-', url: 'redis://localhost:6379')
cache['key'] = 'value'
value = cache['key']
cache.delete('key')

With expiration

cache = OllamaChat::RedisCache.new(prefix: 'expiring-', url: 'redis://localhost:6379', ex: 3600)
cache['key'] = 'value' # Automatically expires in 1 hour

Iteration

cache.each do |key, value|
  puts "#{key}: #{value}"
end

Cache management

cache.clear # Remove all entries with this prefix
size = cache.size # Get number of entries

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, url: ENV['REDIS_URL'], ex: nil) ⇒ RedisCache

Initializes a new RedisCache instance

Parameters:

  • prefix (String)

    The prefix to use for all keys in this cache

  • url (String, nil) (defaults to: ENV['REDIS_URL'])

    The Redis connection URL (defaults to ENV)

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

    Default expiration time in seconds

Raises:

  • (ArgumentError)

    If no Redis URL is provided



39
40
41
42
43
44
# File 'lib/ollama_chat/redis_cache.rb', line 39

def initialize(prefix:, url: ENV['REDIS_URL'], ex: nil)
  @prefix = prefix
  @url = url
  @ex = ex
  raise ArgumentError, 'require redis url' unless @url
end

Instance Method Details

#[](key) ⇒ String?

Retrieves a value from the cache by key

Parameters:

  • key (String)

    The cache key to retrieve

Returns:

  • (String, nil)

    The cached value or nil if not found



60
61
62
63
# File 'lib/ollama_chat/redis_cache.rb', line 60

def [](key)
  value = redis.get(pre(key))
  value
end

#[]=(key, value) ⇒ String

Stores a value in the cache with the given key

Parameters:

  • key (String)

    The cache key

  • value (String)

    The value to cache

Returns:

  • (String)

    The cached value



70
71
72
# File 'lib/ollama_chat/redis_cache.rb', line 70

def []=(key, value)
  set(key, value)
end

#clearOllamaChat::RedisCache

Clears all entries from the cache with this prefix

Returns:



126
127
128
129
# File 'lib/ollama_chat/redis_cache.rb', line 126

def clear
  redis.scan_each(match: "#{@prefix}*") { |key| redis.del(key) }
  self
end

#delete(key) ⇒ Boolean

Deletes a key from the cache

Parameters:

  • key (String)

    The cache key to delete

Returns:

  • (Boolean)

    true if the key was deleted, false if it didn’t exist



110
111
112
# File 'lib/ollama_chat/redis_cache.rb', line 110

def delete(key)
  redis.del(pre(key)) == 1
end

#each {|key, value| ... } ⇒ OllamaChat::RedisCache

Iterates over all entries in the cache

Yields:

  • (key, value)

    Yields each key-value pair

Returns:



135
136
137
138
# File 'lib/ollama_chat/redis_cache.rb', line 135

def each(&block)
  redis.scan_each(match: "#{@prefix}*") { |key| block.(key, self[unpre(key)]) }
  self
end

#key?(key) ⇒ Boolean

Checks if a key exists in the cache

Parameters:

  • key (String)

    The cache key to check

Returns:

  • (Boolean)

    true if the key exists, false otherwise



102
103
104
# File 'lib/ollama_chat/redis_cache.rb', line 102

def key?(key)
  !!redis.exists?(pre(key))
end

#pre(key) ⇒ String (private)

Prepends the prefix to a key

Parameters:

  • key (String)

    The key to prefix

Returns:

  • (String)

    The prefixed key



146
147
148
# File 'lib/ollama_chat/redis_cache.rb', line 146

def pre(key)
  [ @prefix, key ].join
end

#redisRedis

Returns the Redis connection instance

This method lazily initializes the Redis connection to avoid establishing connections until they’re actually needed.

Returns:

  • (Redis)

    The Redis client instance



52
53
54
# File 'lib/ollama_chat/redis_cache.rb', line 52

def redis
  @redis ||= Redis.new(url: @url)
end

#set(key, value, ex: nil) ⇒ String

Stores a value in the cache with optional expiration

Parameters:

  • key (String)

    The cache key

  • value (String)

    The value to cache

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

    Expiration time in seconds (overrides default)

Returns:

  • (String)

    The cached value



80
81
82
83
84
85
86
87
88
# File 'lib/ollama_chat/redis_cache.rb', line 80

def set(key, value, ex: nil)
  ex ||= @ex
  if !ex.nil? && ex < 1
    redis.del(pre(key))
  else
    redis.set(pre(key), value, ex:)
  end
  value
end

#sizeInteger

Gets the number of entries in the cache

Returns:

  • (Integer)

    The number of entries



117
118
119
120
121
# File 'lib/ollama_chat/redis_cache.rb', line 117

def size
  s = 0
  redis.scan_each(match: "#{@prefix}*") { |key| s += 1 }
  s
end

#ttl(key) ⇒ Integer

Gets the time-to-live for a key

Parameters:

  • key (String)

    The cache key

Returns:

  • (Integer)

    The remaining time-to-live in seconds, or -1 if not found



94
95
96
# File 'lib/ollama_chat/redis_cache.rb', line 94

def ttl(key)
  redis.ttl(pre(key))
end

#unpre(key) ⇒ String (private)

Removes the prefix from a key

Parameters:

  • key (String)

    The prefixed key

Returns:

  • (String)

    The key without prefix



154
155
156
# File 'lib/ollama_chat/redis_cache.rb', line 154

def unpre(key)
  key.sub(/\A#@prefix/, '')
end