Skip to content

Commit

Permalink
Cache cassandra result in conversation history (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
pankajjangid05 authored Jul 3, 2023
1 parent 7d2c681 commit 8813f9c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<groupId>com.uci</groupId>
<artifactId>utils</artifactId>
<!--For Development we are using SNAPSHOT with version-->
<version>2.2.5</version>
<version>2.2.5-SNAPSHOT</version>

<!-- On changing, Set version in build-deploy.yml to delete the previous packages if exists and deploy new package for the specified version -->

Expand Down
122 changes: 98 additions & 24 deletions src/main/java/com/uci/utils/cache/service/RedisCacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,64 @@
@Setter
@Slf4j
public class RedisCacheService {
private RedisTemplate<String, Object> redisTemplate;
private RedisTemplate<String, Object> redisTemplate;
public Integer redisKeyTimeout;

/**
* Get is redis cache enabled or disabled
*
* @return
*/
private Boolean enabledRedis() {
if(System.getenv("REDIS_ENABLED") != null && System.getenv("REDIS_ENABLED").equalsIgnoreCase("false")) {
if (System.getenv("REDIS_ENABLED") != null && System.getenv("REDIS_ENABLED").equalsIgnoreCase("false")) {
return false;
}
return true;
}
/**

/**
* Get XMessageDao Object from cache by key
*
* @param key
* @return
*/
public Object getMinioCDNCache(String key) {
return getCache(redisKeyWithPrefix(RedisCachePrefix.MinioCDN.name(), key));
}

/**
* Set XMessageDao Object in cache by key
*
* @param key
* @param value
*/
public void setMinioCDNCache(String key, Object value) {
setCache(redisKeyWithPrefix(RedisCachePrefix.MinioCDN.name(), key), value);
}

/**
* Get Fusion Auth User ID Object from cache by key
*
* @param key
* @return
*/
public Object getFAUserIDForAppCache(String key) {
return getCache(redisKeyWithPrefix(RedisCachePrefix.FAUserID.name(), key));
}

/**
* Set Fusion Auth User ID Object in cache by key
*
* @param key
* @param value
*/
public void setFAUserIDForAppCache(String key, Object value) {
setCache(redisKeyWithPrefix(RedisCachePrefix.FAUserID.name(), key), value);
}

/**
* Delete Fusion Auth User ID Object in cache by key
*
* @param key
*/
public void deleteFAUserIDForAppCache(String key) {
Expand All @@ -85,50 +91,56 @@ public void deleteFAUserIDForAppCache(String key) {

/**
* Get Language Object from cache by name
*
* @param name
* @return
*/
public Object getLanguageCache(String name) {
return getCache(redisKeyWithPrefix(RedisCachePrefix.Language.name(), name));
}

/**
* Set Language Object in cache by name
*
* @param name
* @param value
*/
public void setLanguageCache(String name, Object value) {
setCache(redisKeyWithPrefix(RedisCachePrefix.Language.name(), name), value);
}

/**
* Delete Language Object in cache by name
*
* @param name
*/
public void deleteLanguageCache(String name) {
deleteCache(redisKeyWithPrefix(RedisCachePrefix.Language.name(), name));
}

/**
* Get XMessageDao Object from cache by key
*
* @param key
* @return
*/
public Object getXMessageDaoCache(String key) {
return getCache(redisKeyWithPrefix(RedisCachePrefix.XMessageDao.name(), key));
}

/**
* Set XMessageDao Object in cache by key
*
* @param key
* @param value
*/
public void setXMessageDaoCache(String key, Object value) {
setCache(redisKeyWithPrefix(RedisCachePrefix.XMessageDao.name(), key), value);
}

/**
* Delete XMessageDao Object in cache by key
*
* @param key
*/
public void deleteXMessageDaoCache(String key) {
Expand All @@ -137,15 +149,16 @@ public void deleteXMessageDaoCache(String key) {

/**
* Get all cache keys by prefix
*
* @param prefix
* @return
*/
public ArrayList<String> getAllCacheKeys(String prefix) {
if(enabledRedis()) {
if (enabledRedis()) {
ArrayList<String> keysList = new ArrayList();
Set<String> redisKeys = redisTemplate.keys(prefix);
Iterator<String> redisIt = redisKeys.iterator();
while(redisIt.hasNext()) {
while (redisIt.hasNext()) {
keysList.add(redisIt.next());
}
return keysList;
Expand All @@ -155,18 +168,19 @@ public ArrayList<String> getAllCacheKeys(String prefix) {

/**
* Get all cache keys by prefix
*
* @param prefix
* @return
*/
public ArrayList<Pair<String, Object>> getAllCacheKeyValuesByPattern(String prefix) {
if(enabledRedis()) {
if (enabledRedis()) {
ArrayList<Pair<String, Object>> list = new ArrayList();
String pattern = redisKeyPatternWithPrefix(prefix);
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
Set<byte[]> redisKeys = redisConnection.keys(pattern.getBytes());
Iterator<byte[]> redisIt = redisKeys.iterator();
ValueOperations<String, Object> valOperations = redisTemplate.opsForValue();
while(redisIt.hasNext()) {
while (redisIt.hasNext()) {
byte[] data = redisIt.next();
String key = new String(data, 0, data.length);
Object result = valOperations.get(key);
Expand All @@ -179,11 +193,12 @@ public ArrayList<Pair<String, Object>> getAllCacheKeyValuesByPattern(String pref

/**
* Get cache by key
*
* @param redisKey
* @return
*/
public Object getCache(String redisKey) {
if(enabledRedis()) {
if (enabledRedis()) {
ValueOperations<String, Object> valOperations = redisTemplate.opsForValue();
Object result = valOperations.get(redisKey);

Expand All @@ -194,14 +209,15 @@ public Object getCache(String redisKey) {
} catch (JsonProcessingException e) {
e.printStackTrace();
}
log.info("Find redis cache by key: "+redisKey+", val: "+val);
log.info("Find redis cache by key: " + redisKey + ", val: " + val);
return result;
}
return null;
}

/**
* Set cache value by redisKey
*
* @param redisKey
* @param value
*/
Expand All @@ -220,7 +236,7 @@ public void setCache(String redisKey, Object value) {
} catch (JsonProcessingException e) {
e.printStackTrace();
}
log.info("Set redis cache for key: " + redisKey+", val: "+redisVal);
log.info("Set redis cache for key: " + redisKey + ", val: " + redisVal);
} catch (Exception e) {
/* If redis cache not able to set, delete cache */
redisTemplate.delete(redisKey);
Expand All @@ -233,6 +249,7 @@ public void setCache(String redisKey, Object value) {

/**
* Set cache value by redisKey
*
* @param redisKey
*/
public void deleteCache(String redisKey) {
Expand All @@ -248,27 +265,30 @@ public void deleteCache(String redisKey) {
}
}
}

/**
* Add env prefix with cache key
*
* @param key
* @return
*/
private String redisKeyWithPrefix(String prefix, String key) {
return System.getenv("ENV")+"-"+prefix+":"+key;
return System.getenv("ENV") + "-" + prefix + ":" + key;
}

/**
* Add env prefix
*
* @param prefix
* @return
*/
private String redisKeyPatternWithPrefix(String prefix) {
return System.getenv("ENV")+"-"+prefix+":*";
return System.getenv("ENV") + "-" + prefix + ":*";
}

/**
* Get Cache Expiry time in seconds
*
* @return
*/
private Integer getExpireTimeInSeconds() {
Expand All @@ -277,11 +297,65 @@ private Integer getExpireTimeInSeconds() {

/**
* Check isKeyExists in Cache
*
* @param key
* @return
*/
public boolean isKeyExists(String key) {
RedisOperations<String, Object> operations = redisTemplate.opsForValue().getOperations();
return operations.hasKey(key);
}

/**
* Set Conversation History in Redis Cache
* @param redisKey
* @param value
*/
public void setConversationHistoryCache(String redisKey, Object value) {
if (enabledRedis()) {
ValueOperations<String, Object> valOperations = redisTemplate.opsForValue();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try {
valOperations.set(redisKey, value);
/* Set Expire time for key */
redisTemplate.expire(redisKey, getExpireTimeInSeconds(), TimeUnit.SECONDS);

String redisVal = "";
try {
redisVal = ow.writeValueAsString(value);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
log.info("Set Conversation-History redis cache for key: " + redisKey);
} catch (Exception e) {
/* If redis cache not able to set, delete cache */
redisTemplate.delete(redisKey);
e.printStackTrace();
log.info("Exception in redis setCache: " + e.getMessage());
}
}
}

/**
* Get Conversation History from Redis Cache
* @param redisKey
* @return
*/
public Object getConversationHistoryFromCache(String redisKey) {
if (enabledRedis()) {
ValueOperations<String, Object> valOperations = redisTemplate.opsForValue();
Object result = valOperations.get(redisKey);

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String val = "";
try {
val = ow.writeValueAsString(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
log.info("Get Conversation-History cache by key: " + redisKey);
return result;
}
return null;
}
}

0 comments on commit 8813f9c

Please sign in to comment.