From Ungracious Giraffe, 5 Years ago, written in Plain Text.
Embed
  1. /*
  2.  * Copyright 2019, Pearson Education, Learning Technology Group
  3.  *
  4.  * RedisCacheServiceImpl.java
  5.  */
  6. package com.pearson.ltg.rbs.ltitoolgateway.redis.service;
  7.  
  8. import java.time.Duration;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.data.redis.connection.ReactiveRedisConnection;
  15. import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
  16. import org.springframework.data.redis.connection.RedisConnection;
  17. import org.springframework.data.redis.connection.RedisConnectionFactory;
  18. import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
  19. import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
  20. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  21. import org.springframework.data.redis.core.ReactiveRedisTemplate;
  22. import org.springframework.data.redis.core.RedisTemplate;
  23. import org.springframework.data.redis.serializer.RedisSerializationContext;
  24. import org.springframework.stereotype.Service;
  25.  
  26. import com.pearson.ltg.rbs.ltitoolgateway.model.PlatformConfiguration;
  27.  
  28. import reactor.core.publisher.Mono;
  29.  
  30. @Service
  31. public class RedisCacheServiceImpl implements IRedisCacheService {
  32.  
  33.     @Value("${redis.expiry.time : 3600}")
  34.     protected long expiryTime;
  35.  
  36.     @Autowired
  37.     ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
  38.  
  39.     /**
  40.     public Mono<PlatformConfiguration> getPlatformConfigurationFromCache(String cacheKey) {
  41.         reactiveRedisTemplate.
  42.         PlatformConfiguration platformConfiguration = (PlatformConfiguration)redisTemplate.opsForValue().get(cacheKey);
  43.         return Mono.just(platformConfiguration);
  44.     }
  45.  
  46.     public void savePlatformConfigurationInCache(String cacheKey, Mono<PlatformConfiguration> platformConfiguration) {
  47.         redisTemplate.opsForValue().set(cacheKey, platformConfiguration);
  48.         setExpiryTime(cacheKey);
  49.     }
  50.    
  51.     public String getPlatformIdFromCache(String cacheKey) {
  52.         return (String) redisTemplate.opsForValue().get(cacheKey);
  53.     }
  54.  
  55.     public void savePlatformIdInCache(String cacheKey, String platformId) {
  56.         redisTemplate.opsForValue().set(cacheKey, platformId);
  57.         setExpiryTime(cacheKey);
  58.     }
  59.     **/
  60.  
  61.     public Mono<Object> getCache(String cacheKey) {
  62.         return reactiveRedisTemplate.opsForValue().get(cacheKey);
  63.     }
  64.    
  65.     @Override
  66.     public void flushRedisCache() {
  67.         ReactiveRedisConnectionFactory redisConnectionFactory = reactiveRedisTemplate.getConnectionFactory();
  68.         if (null != redisConnectionFactory) {
  69.             redisConnectionFactory.getReactiveConnection().serverCommands().flushAll();
  70.         }
  71.     }
  72.  
  73.     @Override
  74.     public void deleteKey(String cacheKey) {
  75.         if (StringUtils.isNotEmpty(cacheKey)) {
  76.             reactiveRedisTemplate.delete(cacheKey);
  77.         }
  78.     }
  79.  
  80.     private void setExpiryTime(String cacheKey) {
  81.         reactiveRedisTemplate.expire(cacheKey, Duration.ofSeconds(expiryTime));
  82.     }
  83.    
  84.  
  85.     public static void main(String args[]) {
  86.        
  87.         LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
  88.                 .useSsl().and()
  89.                 .commandTimeout(Duration.ofSeconds(2))
  90.                 .shutdownTimeout(Duration.ZERO)
  91.                 .build();
  92.         RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("12.0.0.1", 6379);
  93.         ReactiveRedisConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
  94.  
  95.         ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(factory, RedisSerializationContext.string());
  96.         reactiveRedisTemplate.opsForValue().set("testing-key", "testing-value").block();
  97.        
  98.         Mono<String> valueMono = reactiveRedisTemplate.opsForValue().get("testing-key");
  99.         System.out.println("valueMono = " + valueMono.block());
  100.     }
  101.    
  102. }
  103.