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 StringThe name of the cache.protected final StringThe Redis key prefix for this cache.protected UnifiedJedisThe Redis client connection.protected IntegerDefault time-to-live in seconds for cache entries. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseCache(String name, UnifiedJedis redisClient) Creates a new BaseCache instance without TTL.protectedBaseCache(String name, UnifiedJedis redisClient, Integer ttl) Creates a new BaseCache instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all entries in the cache.voidDisconnect from Redis.voidSet expiration on a key.protected final voidfinalize()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.booleanCheck if the cache is connected to Redis.protected StringGenerate a Redis key with the cache prefix.voidSet the default TTL for cache entries.protected voidsetWithTtl(byte[] key, byte[] value, Integer ttl) Helper method to set a byte array value with optional TTL.protected voidsetWithTtl(String key, String value, Integer ttl) Helper method to set a value with optional TTL.longsize()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).
-