Package com.redis.vl.extensions.cache
Class BaseCache
java.lang.Object
com.redis.vl.extensions.cache.BaseCache
- Direct Known Subclasses:
EmbeddingsCache
,SemanticCache
Abstract base class for all cache implementations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final String
The name of the cache.protected final String
The Redis key prefix for this cache.protected UnifiedJedis
The Redis client connection.protected Integer
Default time-to-live in seconds for cache entries. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
BaseCache
(String name, UnifiedJedis redisClient) Creates a new BaseCache instance without TTL.protected
BaseCache
(String name, UnifiedJedis redisClient, Integer ttl) Creates a new BaseCache instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear all entries in the cache.void
Disconnect from Redis.void
Set expiration on a key.protected final void
finalize()
Empty finalizer to prevent finalizer attacks on subclasses.getName()
Get the cache name.Get the cache prefix.getTtl()
Get the default TTL for cache entries.boolean
Check if the cache is connected to Redis.protected String
Generate a Redis key with the cache prefix.void
Set the default TTL for cache entries.protected void
setWithTtl
(byte[] key, byte[] value, Integer ttl) Helper method to set a byte array value with optional TTL.protected void
setWithTtl
(String key, String value, Integer ttl) Helper method to set a value with optional TTL.long
size()
Get the number of entries in the cache.
-
Field Details
-
name
The name of the cache. -
prefix
The Redis key prefix for this cache. -
ttl
Default time-to-live in seconds for cache entries. -
redisClient
The Redis client connection.
-
-
Constructor Details
-
BaseCache
Creates a new BaseCache instance.- Parameters:
name
- The name of the cacheredisClient
- The Redis client connectionttl
- Default time-to-live in seconds for cache entries (null for no expiration)
-
BaseCache
Creates a new BaseCache instance without TTL.- Parameters:
name
- The name of the cacheredisClient
- The Redis client connection
-
-
Method Details
-
makeKey
Generate a Redis key with the cache prefix.- Parameters:
entryId
- The unique identifier for the cache entry- Returns:
- The prefixed Redis key
-
getPrefix
Get the cache prefix.- Returns:
- The cache prefix
-
getName
Get the cache name.- Returns:
- The cache name
-
getTtl
Get the default TTL for cache entries.- Returns:
- Time-to-live in seconds (null if no expiration)
-
setTtl
Set the default TTL for cache entries.- Parameters:
ttl
- Time-to-live in seconds (null for no expiration)
-
expire
Set expiration on a key.- Parameters:
key
- The Redis keyttl
- Time-to-live in seconds (uses default if null)
-
clear
public void clear()Clear all entries in the cache. -
size
public long size()Get the number of entries in the cache.- Returns:
- The number of cache entries
-
isConnected
public boolean isConnected()Check if the cache is connected to Redis.- Returns:
- true if connected, false otherwise
-
disconnect
public void disconnect()Disconnect from Redis. -
setWithTtl
Helper method to set a value with optional TTL.- Parameters:
key
- The cache keyvalue
- The value to storettl
- Time-to-live in seconds (null uses default TTL)
-
setWithTtl
Helper method to set a byte array value with optional TTL.- Parameters:
key
- The cache key as byte arrayvalue
- The value as byte arrayttl
- Time-to-live in seconds (null uses default TTL)
-
finalize
protected final void finalize()Empty finalizer to prevent finalizer attacks on subclasses.Per SEI CERT OBJ11-J, classes that throw exceptions in constructors should have an empty final finalizer to prevent finalizer attacks through subclassing.
Note: finalize() is deprecated but required here for security (prevent finalizer attacks).
-