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) ⇒ Boolean
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) ⇒ Boolean
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
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
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
70 71 72 |
# File 'lib/ollama_chat/redis_cache.rb', line 70 def []=(key, value) set(key, value) end |
#clear ⇒ OllamaChat::RedisCache
Clears all entries from the cache with this prefix
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
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
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
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
146 147 148 |
# File 'lib/ollama_chat/redis_cache.rb', line 146 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.
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
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 |
#size ⇒ Integer
Gets the number of entries in the cache
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
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
154 155 156 |
# File 'lib/ollama_chat/redis_cache.rb', line 154 def unpre(key) key.sub(/\A#@prefix/, '') end |