Class BaseCache

java.lang.Object
com.redis.vl.extensions.cache.BaseCache
Direct Known Subclasses:
EmbeddingsCache, SemanticCache

public abstract class BaseCache extends Object
Abstract base class for all cache implementations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final String
    The name of the cache.
    protected final String
    The Redis key prefix for this cache.
    protected redis.clients.jedis.UnifiedJedis
    The Redis client connection.
    protected Integer
    Default time-to-live in seconds for cache entries.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseCache(String name, redis.clients.jedis.UnifiedJedis redisClient)
    Creates a new BaseCache instance without TTL.
    protected
    BaseCache(String name, redis.clients.jedis.UnifiedJedis redisClient, Integer ttl)
    Creates a new BaseCache instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all entries in the cache.
    void
    Disconnect from Redis.
    void
    expire(String key, Integer ttl)
    Set expiration on a key.
    protected final void
    Empty finalizer to prevent finalizer attacks on subclasses.
    Get the cache name.
    Get the cache prefix.
    Get the default TTL for cache entries.
    boolean
    Check if the cache is connected to Redis.
    protected String
    makeKey(String entryId)
    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
    Get the number of entries in the cache.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • name

      protected final String name
      The name of the cache.
    • prefix

      protected final String prefix
      The Redis key prefix for this cache.
    • ttl

      protected Integer ttl
      Default time-to-live in seconds for cache entries.
    • redisClient

      protected redis.clients.jedis.UnifiedJedis redisClient
      The Redis client connection.
  • Constructor Details

    • BaseCache

      protected BaseCache(String name, redis.clients.jedis.UnifiedJedis redisClient, Integer ttl)
      Creates a new BaseCache instance.
      Parameters:
      name - The name of the cache
      redisClient - The Redis client connection
      ttl - Default time-to-live in seconds for cache entries (null for no expiration)
    • BaseCache

      protected BaseCache(String name, redis.clients.jedis.UnifiedJedis redisClient)
      Creates a new BaseCache instance without TTL.
      Parameters:
      name - The name of the cache
      redisClient - The Redis client connection
  • Method Details

    • makeKey

      protected String makeKey(String entryId)
      Generate a Redis key with the cache prefix.
      Parameters:
      entryId - The unique identifier for the cache entry
      Returns:
      The prefixed Redis key
    • getPrefix

      public String getPrefix()
      Get the cache prefix.
      Returns:
      The cache prefix
    • getName

      public String getName()
      Get the cache name.
      Returns:
      The cache name
    • getTtl

      public Integer getTtl()
      Get the default TTL for cache entries.
      Returns:
      Time-to-live in seconds (null if no expiration)
    • setTtl

      public void setTtl(Integer ttl)
      Set the default TTL for cache entries.
      Parameters:
      ttl - Time-to-live in seconds (null for no expiration)
    • expire

      public void expire(String key, Integer ttl)
      Set expiration on a key.
      Parameters:
      key - The Redis key
      ttl - 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

      protected void setWithTtl(String key, String value, Integer ttl)
      Helper method to set a value with optional TTL.
      Parameters:
      key - The cache key
      value - The value to store
      ttl - Time-to-live in seconds (null uses default TTL)
    • setWithTtl

      protected void setWithTtl(byte[] key, byte[] value, Integer ttl)
      Helper method to set a byte array value with optional TTL.
      Parameters:
      key - The cache key as byte array
      value - The value as byte array
      ttl - 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).

      Overrides:
      finalize in class Object
      See Also: