CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-redis-clients--jedis

Jedis is a blazingly small and sane Redis java client.

Overview
Eval results
Files

client-side-caching.mddocs/

Client-Side Caching

Jedis provides comprehensive client-side caching support to improve performance by reducing network round-trips and server load. The caching system supports various eviction policies, TTL, and statistics.

Capabilities

Cache Interface

Core caching abstraction for client-side caching implementations.

/**
 * Cache interface for storing and retrieving cached values
 */
public interface Cache {
    /**
     * Flush all entries from the cache
     */
    void flush();
    
    /**
     * Flush all entries from all caches  
     */
    void flushAll();
    
    /**
     * Invalidate a specific cache entry
     * @param key Cache key to invalidate
     */
    void invalidate(CacheKey key);
    
    /**
     * Get a cached value
     * @param key Cache key
     * @return Cached value or null if not found
     */
    Cacheable getValue(CacheKey key);
    
    /**
     * Store a value in the cache
     * @param key Cache key
     * @param value Value to cache
     */
    void setValue(CacheKey key, Cacheable value);
    
    /**
     * Get cache statistics
     * @return Cache statistics
     */
    CacheStats getStats();
}

Cache Configuration

Configuration object for customizing cache behavior.

/**
 * Configuration for cache instances
 */
public class CacheConfig {
    /**
     * Create cache config with size and TTL
     * @param maxSize Maximum number of entries
     * @param ttlSeconds Time-to-live in seconds
     */
    public CacheConfig(int maxSize, int ttlSeconds);
    
    /**
     * Set maximum cache size
     * @param maxSize Maximum number of entries
     * @return This config for chaining
     */
    public CacheConfig maxSize(int maxSize);
    
    /**
     * Set time-to-live for entries
     * @param ttlSeconds TTL in seconds
     * @return This config for chaining
     */
    public CacheConfig ttl(int ttlSeconds);
    
    /**
     * Set eviction policy
     * @param evictionPolicy Eviction policy to use
     * @return This config for chaining
     */
    public CacheConfig evictionPolicy(EvictionPolicy evictionPolicy);
    
    public int getMaxSize();
    public int getTtlSeconds();
    public EvictionPolicy getEvictionPolicy();
}

Default Cache Implementation

Built-in cache implementation with LRU eviction.

/**
 * Default cache implementation using LRU eviction
 */
public class DefaultCache implements Cache {
    /**
     * Create cache with maximum size
     * @param maxSize Maximum number of entries
     */
    public DefaultCache(int maxSize);
    
    /**
     * Create cache with configuration
     * @param config Cache configuration
     */
    public DefaultCache(CacheConfig config);
}

Cache Entry

Wrapper for cached values with metadata.

/**
 * Represents a cached entry with metadata
 */
public class CacheEntry {
    /**
     * Get the cached value
     * @return Cached value
     */
    public Object getValue();
    
    /**
     * Get timestamp when entry was cached
     * @return Timestamp in milliseconds
     */
    public long getTimestamp();
    
    /**
     * Check if entry has expired
     * @return true if expired
     */
    public boolean isExpired();
}

Cache Key

Key representation for cache entries.

/**
 * Key for cache entries
 */
public class CacheKey {
    public CacheKey(String key);
    public String getKey();
    public int hashCode();
    public boolean equals(Object obj);
}

Cache Statistics

Statistics and metrics for cache performance monitoring.

/**
 * Cache performance statistics
 */
public class CacheStats {
    /**
     * Get number of cache hits
     * @return Hit count
     */
    public long getHits();
    
    /**
     * Get number of cache misses
     * @return Miss count
     */
    public long getMisses();
    
    /**
     * Get hit ratio
     * @return Hit ratio between 0.0 and 1.0
     */
    public double getHitRatio();
    
    /**
     * Get current number of entries
     * @return Entry count
     */
    public long getSize();
    
    /**
     * Get number of evictions
     * @return Eviction count
     */
    public long getEvictions();
}

Eviction Policies

Cache eviction strategies for managing memory usage.

/**
 * Base eviction policy interface
 */
public interface EvictionPolicy {
    /**
     * Select entry to evict
     * @return Cache key to evict
     */
    CacheKey selectEvictionCandidate();
    
    /**
     * Notify policy of entry access
     * @param key Accessed key
     */
    void onAccess(CacheKey key);
}

/**
 * Least Recently Used eviction policy
 */
public class LRUEviction implements EvictionPolicy {
    public LRUEviction();
}

Usage Examples

Basic Caching Setup

import redis.clients.jedis.csc.*;

// Create cache with configuration
CacheConfig config = new CacheConfig(1000, 300)  // 1000 entries, 5min TTL
    .evictionPolicy(new LRUEviction());

Cache cache = new DefaultCache(config);

// Use with JedisPooled for automatic caching
JedisPooled jedis = new JedisPooled("localhost", 6379, 
    DefaultJedisClientConfig.builder().build(), 
    config);

Manual Cache Management

// Manual cache operations
CacheKey key = new CacheKey("user:123");
Cacheable value = new DefaultCacheable("John Doe");

cache.setValue(key, value);
Cacheable cached = cache.getValue(key);

// Check statistics
CacheStats stats = cache.getStats();
System.out.println("Hit ratio: " + stats.getHitRatio());
System.out.println("Cache size: " + stats.getSize());

Invalidation Patterns

// Invalidate specific key
cache.invalidate(new CacheKey("user:123"));

// Flush all entries
cache.flush();

// Flush across all cache instances  
cache.flushAll();

Integration with JedisPooled

Client-side caching integrates seamlessly with JedisPooled for transparent performance improvements:

// JedisPooled with caching
CacheConfig cacheConfig = new CacheConfig(2000, 600);
JedisPooled jedis = new JedisPooled("localhost", 6379, 
    DefaultJedisClientConfig.builder().build(), 
    cacheConfig);

// Operations are automatically cached
String value = jedis.get("key");  // Cache miss - fetches from Redis
String cached = jedis.get("key"); // Cache hit - returns from local cache

Install with Tessl CLI

npx tessl i tessl/maven-redis-clients--jedis

docs

authentication.md

client-side-caching.md

clustering.md

commands-operations.md

connection-management.md

core-clients.md

exceptions.md

index.md

modules.md

parameters.md

pubsub.md

transactions-pipelining.md

tile.json