/* * Copyright 2019, Pearson Education, Learning Technology Group * * RedisCacheServiceImpl.java */ package com.pearson.ltg.rbs.ltitoolgateway.redis.service; import java.time.Duration; import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.connection.ReactiveRedisConnection; import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.stereotype.Service; import com.pearson.ltg.rbs.ltitoolgateway.model.PlatformConfiguration; import reactor.core.publisher.Mono; @Service public class RedisCacheServiceImpl implements IRedisCacheService { @Value("${redis.expiry.time : 3600}") protected long expiryTime; @Autowired ReactiveRedisTemplate reactiveRedisTemplate; /** public Mono getPlatformConfigurationFromCache(String cacheKey) { reactiveRedisTemplate. PlatformConfiguration platformConfiguration = (PlatformConfiguration)redisTemplate.opsForValue().get(cacheKey); return Mono.just(platformConfiguration); } public void savePlatformConfigurationInCache(String cacheKey, Mono platformConfiguration) { redisTemplate.opsForValue().set(cacheKey, platformConfiguration); setExpiryTime(cacheKey); } public String getPlatformIdFromCache(String cacheKey) { return (String) redisTemplate.opsForValue().get(cacheKey); } public void savePlatformIdInCache(String cacheKey, String platformId) { redisTemplate.opsForValue().set(cacheKey, platformId); setExpiryTime(cacheKey); } **/ public Mono getCache(String cacheKey) { return reactiveRedisTemplate.opsForValue().get(cacheKey); } @Override public void flushRedisCache() { ReactiveRedisConnectionFactory redisConnectionFactory = reactiveRedisTemplate.getConnectionFactory(); if (null != redisConnectionFactory) { redisConnectionFactory.getReactiveConnection().serverCommands().flushAll(); } } @Override public void deleteKey(String cacheKey) { if (StringUtils.isNotEmpty(cacheKey)) { reactiveRedisTemplate.delete(cacheKey); } } private void setExpiryTime(String cacheKey) { reactiveRedisTemplate.expire(cacheKey, Duration.ofSeconds(expiryTime)); } public static void main(String args[]) { LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .useSsl().and() .commandTimeout(Duration.ofSeconds(2)) .shutdownTimeout(Duration.ZERO) .build(); RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("12.0.0.1", 6379); ReactiveRedisConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig); ReactiveRedisTemplate reactiveRedisTemplate = new ReactiveRedisTemplate<>(factory, RedisSerializationContext.string()); reactiveRedisTemplate.opsForValue().set("testing-key", "testing-value").block(); Mono valueMono = reactiveRedisTemplate.opsForValue().get("testing-key"); System.out.println("valueMono = " + valueMono.block()); } }