From Gray Mosquito, 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.  
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
  14. import org.springframework.data.redis.core.ReactiveRedisTemplate;
  15. import org.springframework.stereotype.Service;
  16.  
  17. import com.pearson.ltg.rbs.ltitoolgateway.model.PlatformConfiguration;
  18.  
  19. import reactor.core.publisher.Mono;
  20.  
  21. @Service
  22. public class RedisCacheServiceImpl implements IRedisCacheService {
  23.  
  24.     @Value("${redis.expiry.time : 3600}")
  25.     protected long expiryTime;
  26.  
  27.     @Autowired
  28.     ReactiveRedisTemplate<String, Object> redisTemplate;
  29.  
  30.     public Mono<PlatformConfiguration> getPlatformConfigurationFromCache(String cacheKey) {
  31.         Mono<Object> configMono = redisTemplate.opsForValue().get(cacheKey);
  32.         return configMono.map(config -> {
  33.             return (PlatformConfiguration)config;
  34.         });
  35.     }
  36.  
  37.     public void savePlatformConfigurationInCache(String cacheKey, Mono<PlatformConfiguration> platformConfiguration) {
  38.         redisTemplate.opsForValue().set(cacheKey, platformConfiguration);
  39.         setExpiryTime(cacheKey);
  40.     }
  41.    
  42.     public Mono<String> getPlatformIdFromCache(String cacheKey) {
  43.         return redisTemplate.opsForValue().get(cacheKey).flatMap(cache -> {
  44.             return Mono.just((String)cache);
  45.         });
  46.     }
  47.  
  48.     public void savePlatformIdInCache(String cacheKey, String platformId) {
  49.         redisTemplate.opsForValue().set(cacheKey, platformId);
  50.         setExpiryTime(cacheKey);
  51.     }
  52.  
  53.     @Override
  54.     public void flushRedisCache() {
  55.         ReactiveRedisConnectionFactory redisConnectionFactory = redisTemplate.getConnectionFactory();
  56.         if (null != redisConnectionFactory) {
  57.             redisConnectionFactory.getReactiveConnection().serverCommands().flushAll();
  58.         }
  59.     }
  60.  
  61.     @Override
  62.     public void deleteKey(String cacheKey) {
  63.         if (StringUtils.isNotEmpty(cacheKey)) {
  64.             redisTemplate.delete(cacheKey);
  65.         }
  66.     }
  67.  
  68.     private void setExpiryTime(String cacheKey) {
  69.         redisTemplate.expire(cacheKey, Duration.ofSeconds(expiryTime));
  70.     }
  71. }
  72.  

Replies to Untitled rss

Title Name Language When
Re: Untitled brunko150 text 5 Years ago.