Skip to content

5.2.0-beta1

Pre-release
Pre-release
Compare
Choose a tag to compare
@sazzad16 sazzad16 released this 11 Mar 02:20
· 169 commits to 5.2.0 since this release
dc35d45

What's new

This release supports server-assisted, client-side caching, and is currently beta grade.

Client-side caching is available within UnifiedJedis, JedisPooled, JedisCluster, etc classes via implementation of ClientSideCache class, with only RESP3 protocol. It is recommended to use a ClientSideCache implementation with TTL (time-to-live). We have included two implementations based on Google Guava and Caffeine libraries.

How to try Client-Side Caching

  1. Install Jedis 5.2.0-beta1
  2. Choose and install a caching library: Google Guava or Caffeine
  3. Use the following code example to get started:
import redis.clients.jedis.*;
import redis.clients.jedis.csc.*;

class CacheExample {
    public static void main() {
        HostAndPort node = HostAndPort.from("localhost:6379");
        JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
                                            .resp3()                // RESP3 protocol
                                            //.user("myuser")       // Redis server username (optional)
                                            //.password("mypass")   // Redis user's password (optional)
                                            .build();

        ClientSideCache clientSideCache;
        // Uncomment one of the following lines to use the corresponding cache backend
        // GuavaClientSideCache clientSideCache = GuavaClientSideCache.builder().maximumSize(10_000).ttl(100).build();
        // CaffeineClientSideCache clientSideCache = CaffeineClientSideCache.builder().maximumSize(10_000).ttl(100).build();

        UnifiedJedis client = new UnifiedJedis(node, clientConfig, clientSideCache);
        // JedisPooled client = new JedisPooled(node, clientConfig, clientSideCache);
        // JedisCluster client = new JedisCluster(Collections.singleton(node), clientConfig, clientSideCache);

        client.set("foo", "bar");
        client.get("foo");
        client.get("foo");          // cache hit
        client.del("foo");

        client.close();
    }
}

It is highly recommended to use a ClientSideCache implementation with TTL.
Both of our provided GuavaClientSideCache and CaffeineClientSideCache have TTL support and use a default value when not set. It is discouraged to use ttl(0) in these.

It is also a good idea to keep the idle connections busy to get more and updated notifications. It can be done easily using pool config:

GenericObjectPoolConfig<Connection> poolConfig = new ConnectionPoolConfig();
poolConfig.setTestWhileIdle(true);              // ConnectionPoolConfig by default does this.
                                                // It is still shown here for better understanding.

This pool config can be used as follows:

JedisPooled client = new JedisPooled(node, clientConfig, clientSideCache, poolConfig);
JedisCluster client = new JedisCluster(Collections.singleton(node), clientConfig, clientSideCache, poolConfig);

It is possible to limit or ignore commands and/or keys for client side caching. For example, if we want to ignore some keys based on their prefix, we can define a ClientSideCacheable:

final String IGNORE_PREFIX = "PREFIX_TO_IGNORE";
ClientSideCacheable isCacheable = new ClientSideCacheable() {
    @Override
    public boolean isCacheable(ProtocolCommand command, Object... keys) {
        for (String key : (String[]) keys) { // assuming we'll only execute methods with String keys
            if (key.startsWith(IGNORE_PREFIX)) {
                return false;
            }
        }
        return true;
    }
};

This ClientSideCacheable can be a parameter for ClientSideCache. In our provided implementations, it can be:

GuavaClientSideCache clientSideCache = GuavaClientSideCache.builder().cacheable(isCacheable).build();
CaffeineClientSideCache clientSideCache = CaffeineClientSideCache.builder().cacheable(isCacheable).build();

It is also possible to create client-side caching enabled client object using URL/URI with proper query params. Supported params are:

  • cache_lib (caching library) - required; can be either guava or caffeine
  • cache_max_size (maximum no of commands) - optional
  • cache_ttl (time-to-live, in seconds) - optional

For example:

JedisPooled client = new JedisPooled("redis://myuser:mypass@localhost:6379/?cache_lib=guava&cache_max_size=10000&cache_ttl=100");