Search in sources :

Example 1 with RedissonSyncCache

use of org.redisson.micronaut.cache.RedissonSyncCache in project redisson by redisson.

the class RedissonCacheTest method testCache.

@Test
public void testCache() throws InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("redisson.threads", "10");
    map.put("redisson.single-server-config.address", "redis://127.0.0.1:6379");
    // map.put("redisson.clusterServersConfig.scanInterval", "3333");
    // map.put("redisson.clusterServersConfig.nodeAddresses", Arrays.asList("redis://127.0.0.2:6379","redis://127.0.0.3:6379"));
    map.put("redisson.caches.test.expire-after-write", "10s");
    map.put("redisson.caches.test.expire-after-access", "3s");
    ApplicationContext ac = ApplicationContext.run(map);
    RedissonClient client = ac.getBean(RedissonClient.class);
    assertThat(client).isNotNull();
    RedissonSyncCache cache = ac.getBean(RedissonSyncCache.class, Qualifiers.byName("test"));
    cache.put(1, 2);
    Thread.sleep(3500);
    assertThat(cache.get(1, Integer.class).isPresent()).isFalse();
    cache.put(3, 4);
    Thread.sleep(2000);
    cache.get(3, Integer.class);
    Thread.sleep(2000);
    assertThat(cache.get(3, Integer.class).isPresent()).isTrue();
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) RedissonClient(org.redisson.api.RedissonClient) HashMap(java.util.HashMap) RedissonSyncCache(org.redisson.micronaut.cache.RedissonSyncCache) Test(org.junit.jupiter.api.Test)

Example 2 with RedissonSyncCache

use of org.redisson.micronaut.cache.RedissonSyncCache in project redisson by redisson.

the class RedissonFactory method cache.

@EachBean(RedissonCacheConfiguration.class)
public RedissonSyncCache cache(@Parameter RedissonCacheConfiguration configuration, RedissonClient redisson, ConversionService<?> conversionService, @Named(TaskExecutors.IO) ExecutorService executorService) {
    Codec codec = Optional.ofNullable(configuration.getCodec()).orElse(redisson.getConfig().getCodec());
    if (configuration.getExpireAfterAccess().toMillis() != 0 || configuration.getExpireAfterWrite().toMillis() != 0 || configuration.getMaxSize() != 0) {
        RMapCache<Object, Object> mapCache = redisson.getMapCache(configuration.getName(), codec);
        return new RedissonSyncCache(conversionService, mapCache, mapCache, executorService, configuration);
    }
    RMap<Object, Object> map = redisson.getMap(configuration.getName(), codec);
    return new RedissonSyncCache(conversionService, null, map, executorService, configuration);
}
Also used : Codec(org.redisson.client.codec.Codec) RedissonSyncCache(org.redisson.micronaut.cache.RedissonSyncCache)

Aggregations

RedissonSyncCache (org.redisson.micronaut.cache.RedissonSyncCache)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 RedissonClient (org.redisson.api.RedissonClient)1 Codec (org.redisson.client.codec.Codec)1