Search code examples
javaspringcachingjedis

How to use Cacheable annotation in spring boot app when it already uses jedis as a fast storage


I'm working on java spring project (Spring Boot v2.3.3.RELEASE, Spring v5.2.8.RELEASE).

I'm want to use spring's @Cacheable annotation with customized cache (not spring default cache). I already have Jedis configured in this app to be used as fast storage. In order to use this annotation I need to configure cacheManger, but I'm not sure how to implement it so it will make use with Jedis, all I find is Redis.

any help will be appreciated!


Solution

  • Found this stack post on setting up CacheManager -> RedisCacheManager -> RedisTemplate -> JedisConnectionFactory -> Redis(Cluster/Standalone)Configuration + JedisClientConfiguration

    Think RedisStandalone Configuration best suits your needs:

    Configuration class used for setting up RedisConnection via RedisConnectionFactory using connecting to a single node Redis installation.

    There is no JedisCachemanager, only a RedisCacheManager like you said, therefore you have to setup a RedisTemplate with a JedisConnectionFactory: redisTemplate.setConnectionFactory(getJedisConnectionFactory()), then you can setup the jedisConnectionFactory with your specific configs e.g. setting database index name, redis host name, JedisClientConfiguration, timeouts, etc. See JedisConnectionFactory

    Example setup of the JedisConnectionFactory setup

      redisTemplate.setConnectionFactory(getJedisConnectionFactory())
    
      private JedisConnectionFactory getJedisConnectionFactory() {      
          JedisConnectionFactory jedisConnectionFactory = new 
    JedisConnectionFactory();      
          jedisConnectionFactory.setClientName("myClientName");      
          jedisConnectionFactory.set(new JedisClientConfiguration(
            new RedisStandaloneConfiguration(...), 
            new JedisClientConfiguration(...)))