Class: OllamaChat::RedisCache
- Inherits:
-
Object
- Object
- OllamaChat::RedisCache
- 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.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Retrieves a value from the cache by key.
-
#[]=(key, value) ⇒ String
Stores a value in the cache with the given key.
-
#clear ⇒ OllamaChat::RedisCache
Clears all entries from the cache with this prefix.
-
#delete(key) ⇒ true, false
Deletes a key from the cache.
-
#each {|key, value| ... } ⇒ OllamaChat::RedisCache
Iterates over all entries in the cache.
-
#initialize(prefix:, url: ENV['REDIS_URL'], ex: nil) ⇒ RedisCache
constructor
Initializes a new RedisCache instance.
-
#key?(key) ⇒ true, false
Checks if a key exists in the cache.
-
#pre(key) ⇒ String
private
Prepends the prefix to a key.
-
#redis ⇒ Redis
Returns the Redis connection instance.
-
#set(key, value, ex: nil) ⇒ String
Stores a value in the cache with optional expiration.
-
#size ⇒ Integer
Gets the number of entries in the cache.
-
#ttl(key) ⇒ Integer
Gets the time-to-live for a key.
-
#unpre(key) ⇒ String
private
Removes the prefix from a key.
Constructor Details
#initialize(prefix:, url: ENV['REDIS_URL'], ex: nil) ⇒ RedisCache
Initializes a new RedisCache instance
38 39 40 41 42 43 |
# File 'lib/ollama_chat/redis_cache.rb', line 38 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
59 60 61 62 |
# File 'lib/ollama_chat/redis_cache.rb', line 59 def [](key) value = redis.get(pre(key)) value end |
#[]=(key, value) ⇒ String
Stores a value in the cache with the given key
69 70 71 |
# File 'lib/ollama_chat/redis_cache.rb', line 69 def []=(key, value) set(key, value) end |
#clear ⇒ OllamaChat::RedisCache
Clears all entries from the cache with this prefix
125 126 127 128 |
# File 'lib/ollama_chat/redis_cache.rb', line 125 def clear redis.scan_each(match: "#{@prefix}*") { |key| redis.del(key) } self end |
#delete(key) ⇒ true, false
Deletes a key from the cache
109 110 111 |
# File 'lib/ollama_chat/redis_cache.rb', line 109 def delete(key) redis.del(pre(key)) == 1 end |
#each {|key, value| ... } ⇒ OllamaChat::RedisCache
Iterates over all entries in the cache
134 135 136 137 |
# File 'lib/ollama_chat/redis_cache.rb', line 134 def each(&block) redis.scan_each(match: "#{@prefix}*") { |key| block.(key, self[unpre(key)]) } self end |
#key?(key) ⇒ true, false
Checks if a key exists in the cache
101 102 103 |
# File 'lib/ollama_chat/redis_cache.rb', line 101 def key?(key) !!redis.exists?(pre(key)) end |
#pre(key) ⇒ String (private)
Prepends the prefix to a key
145 146 147 |
# File 'lib/ollama_chat/redis_cache.rb', line 145 def pre(key) [ @prefix, key ].join end |
#redis ⇒ Redis
Returns the Redis connection instance
This method lazily initializes the Redis connection to avoid establishing connections until they’re actually needed.
51 52 53 |
# File 'lib/ollama_chat/redis_cache.rb', line 51 def redis @redis ||= Redis.new(url: @url) end |
#set(key, value, ex: nil) ⇒ String
Stores a value in the cache with optional expiration
79 80 81 82 83 84 85 86 87 |
# File 'lib/ollama_chat/redis_cache.rb', line 79 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 |
#size ⇒ Integer
Gets the number of entries in the cache
116 117 118 119 120 |
# File 'lib/ollama_chat/redis_cache.rb', line 116 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
93 94 95 |
# File 'lib/ollama_chat/redis_cache.rb', line 93 def ttl(key) redis.ttl(pre(key)) end |
#unpre(key) ⇒ String (private)
Removes the prefix from a key
153 154 155 |
# File 'lib/ollama_chat/redis_cache.rb', line 153 def unpre(key) key.sub(/\A#@prefix/, '') end |