14. Integration with frameworks

Nikita Koksharov edited this page Jan 12, 2017 · 23 revisions

14.1. Spring Cache

Redisson provides integration between Spring framework and Redis. It fully implements Spring Cache Abstraction. Each Cache instance has two important parameters: ttl and maxIdleTime and stores data infinitely if they are not defined or equal to 0.
Config example:

    @Configuration
    @ComponentScan
    @EnableCaching
    public static class Application {

        @Bean(destroyMethod="shutdown")
        RedissonClient redisson() throws IOException {
            Config config = new Config();
            config.useClusterServers()
                  .addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
            return Redisson.create(config);
        }

        @Bean
        CacheManager cacheManager(RedissonClient redissonClient) {
            Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
            // create "testMap" cache with ttl = 24 seconds and maxIdleTime = 12 seconds
            config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
            return new RedissonSpringCacheManager(redissonClient, config);
        }

    }

Cache configuration could be read from JSON or YAML configuration files:

    @Configuration
    @ComponentScan
    @EnableCaching
    public static class Application {

        @Bean(destroyMethod="shutdown")
        RedissonClient redisson(@Value("classpath:/redisson.json") Resource configFile) throws IOException {
            Config config = Config.fromJSON(configFile.getInputStream());
            return Redisson.create(config);
        }

        @Bean
        CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
            return new RedissonSpringCacheManager(redissonClient, "classpath:/cache-config.json");
        }

    }

14.1.1 Spring Cache. JSON and YAML config format

JSON:

{"testMap":{"ttl":1440000,"maxIdleTime":720000}}

YAML:

---
testMap:
  ttl: 1440000
  maxIdleTime: 720000

14.2. Hibernate Cache

Hibernate Cache implementation maintained by hibernate-redis project.

14.3 JCache API (JSR-107) implementation

Redisson provides an implementation of JCache API (JSR-107) for Redis.

Here is an example of JCache API usage. Using default config located at /redisson-jcache.json or /redisson-jcache.yaml:

MutableConfiguration<String, String> config = new MutableConfiguration<>();

CacheManager manager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = manager.createCache("namedCache", config);

Using config with custom location defined by developer:

MutableConfiguration<String, String> config = new MutableConfiguration<>();

// json config
URI redissonConfigUri = getClass().getResource("redisson-jcache.json").toURI();
// or yaml config
URI redissonConfigUri = getClass().getResource("redisson-jcache.yaml").toURI();
CacheManager manager = Caching.getCachingProvider().getCacheManager(redissonConfigUri, null);
Cache<String, String> cache = manager.createCache("namedCache", config);

Read more here about Redisson configuration.

Provided implementation fully passes TCK tests. Here is the test module.

14.4 Tomcat Redis Session Manager

Redisson provides Redis based non-sticky session management for Apache Tomcat. Versions 6.x, 7.x and 8.x are supported.

Current implementation differs from any other Tomcat Session Manager in terms of efficient storage and optimized writes. Each session attribute is written into Redis during each HttpSession.setAttribute invocation. While other solutions serialize whole session each time.

Usage:

  1. Add RedissonSessionManager into context.xml

    <Manager className="org.redisson.tomcat.RedissonSessionManager"
             configPath="${catalina.base}/redisson.conf" />

    configPath - path to Redisson JSON or YAML config. See configuration wiki page for more details.

  2. Copy two jars into TOMCAT_BASE/lib directory:

    1. For JDK 1.8+
      redisson-all-3.2.3.jar

      for Tomcat 6.x
      redisson-tomcat-6-3.2.3.jar
      for Tomcat 7.x
      redisson-tomcat-7-3.2.3.jar
      for Tomcat 8.x
      redisson-tomcat-8-3.2.3.jar

    2. For JDK 1.6+
      redisson-all-2.7.3.jar

      for Tomcat 6.x
      redisson-tomcat-6-2.7.3.jar
      for Tomcat 7.x
      redisson-tomcat-7-2.7.3.jar
      for Tomcat 8.x
      redisson-tomcat-8-2.7.3.jar

14.5 Spring Session

Redisson provides Spring Session store implementation. Each session attribute is written into Redis during each HttpSession.setAttribute invocation.

Usage:

  1. Ensure you have spring-session dependency in your classpath, add it if necessary:

    Maven

    <dependency>
       <groupId>org.springframework.session</groupId>
       <artifactId>spring-session</artifactId>
       <version>1.2.2.RELEASE</version>
    </dependency>

    Gradle

    compile 'org.springframework.session:spring-session:1.2.2.RELEASE'  
  2. Add @EnableRedissonHttpSession annotation and Redisson bean to Spring configuration:

    @EnableRedissonHttpSession 
    public class Config {
    
      @Bean
      public RedissonClient redisson() {
          return Redisson.create();
      }
    }
  3. Add application initializer which extends AbstractHttpSessionApplicationInitializer class:

    public class Initializer extends AbstractHttpSessionApplicationInitializer { 
    
        public Initializer() {
                super(Config.class); 
        }
    }