Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis cache fixes #161

Open
wants to merge 15 commits into
base: cbrelease-4.0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,26 @@
<artifactId>keycloak-admin-client</artifactId>
<version>18.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>

<build>
Expand Down
379 changes: 0 additions & 379 deletions sb-cb-ext.iml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package org.sunbird.cache;
package org.sunbird.assessment;

import java.util.*;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.sunbird.common.util.CbExtServerProperties;
import org.sunbird.common.util.Constants;
import org.sunbird.core.logger.CbExtLogger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisCacheMgr {

private static final int cache_ttl = 84600;

@Autowired
private RedisTemplate<String, Object> redisTemplate;
private JedisPool jedisPool;

@Autowired
CbExtServerProperties cbExtServerProperties;

private CbExtLogger logger = new CbExtLogger(getClass().getName());

public void putCache(String key, Object object) {
public Jedis getJedis() {
try (Jedis jedis = jedisPool.getResource()) {
return jedis;
}
}

public void putCache(String key, String object) {
try {
int ttl = cache_ttl;
if (!StringUtils.isEmpty(cbExtServerProperties.getRedisTimeout())) {
ttl = Integer.parseInt(cbExtServerProperties.getRedisTimeout());
}
redisTemplate.opsForValue().set(Constants.REDIS_COMMON_KEY + key, object);
redisTemplate.expire(Constants.REDIS_COMMON_KEY + key, ttl, TimeUnit.SECONDS);
getJedis().set(Constants.REDIS_COMMON_KEY + key, object);
getJedis().expire(Constants.REDIS_COMMON_KEY + key, ttl);
logger.info("Cache_key_value " + Constants.REDIS_COMMON_KEY + key + " is saved in redis");
} catch (Exception e) {
logger.error(e);
Expand All @@ -40,7 +45,8 @@ public void putCache(String key, Object object) {

public boolean deleteKeyByName(String key) {
try {
redisTemplate.delete(Constants.REDIS_COMMON_KEY + key);
key = key.toUpperCase();
getJedis().del(Constants.REDIS_COMMON_KEY + key);
logger.info("Cache_key_value " + Constants.REDIS_COMMON_KEY + key + " is deleted from redis");
return true;
} catch (Exception e) {
Expand All @@ -49,12 +55,12 @@ public boolean deleteKeyByName(String key) {
}
}

public boolean deleteAllCBExtKey() {
public boolean deleteAllKey() {
try {
String keyPattern = Constants.REDIS_COMMON_KEY + "*";
Set<String> keys = redisTemplate.keys(keyPattern);
Set<String> keys = getJedis().keys(keyPattern);
for (String key : keys) {
redisTemplate.delete(key);
getJedis().del(key);
}
logger.info("All Keys starts with " + Constants.REDIS_COMMON_KEY + " is deleted from redis");
return true;
Expand All @@ -64,34 +70,54 @@ public boolean deleteAllCBExtKey() {
}
}

public Object getCache(String key) {
public String getCache(String key) {
try {
return redisTemplate.opsForValue().get(Constants.REDIS_COMMON_KEY + key);
return getJedis().get(Constants.REDIS_COMMON_KEY + key);
} catch (Exception e) {
logger.error(e);
return null;
}
}

public List<Object> mget(List<String> fields) {
public List<String> mget(List<String> fields) {
try {
List<String> ls = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
ls.add(Constants.REDIS_COMMON_KEY + Constants.QUESTION_ID + fields.get(i));
}
Collection<String> questionIdList = ls;
return redisTemplate.opsForValue().multiGet(questionIdList);
String[] keysForRedis = ls.toArray(new String[ls.size()]);
return getJedis().mget(keysForRedis);
} catch (Exception e) {
logger.error(e);
}
return null;
}

public Set<String> getAllKeyNames() {
public boolean deleteCache() {
try {
String keyPattern = "*";
Set<String> keys = getJedis().keys(keyPattern);
if (!keys.isEmpty()) {
for (String key : keys) {
getJedis().del(key);
}
logger.info("All Keys in Redis Cache is Deleted");
return true;
} else {
return false;
}
} catch (Exception e) {
logger.error(e);
return false;
}
}

public Set<String> getAllKeys() {
Set<String> keys = null;
try {
String keyPattern = Constants.REDIS_COMMON_KEY + "*";
keys = redisTemplate.keys(keyPattern);
String keyPattern = "*";
keys = getJedis().keys(keyPattern);

} catch (Exception e) {
logger.error(e);
return Collections.emptySet();
Expand All @@ -102,13 +128,13 @@ public Set<String> getAllKeyNames() {
public List<Map<String, Object>> getAllKeysAndValues() {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
try {
String keyPattern = Constants.REDIS_COMMON_KEY + "*";
String keyPattern = "*";
Map<String, Object> res = new HashMap<>();
Set<String> keys = redisTemplate.keys(keyPattern);
Set<String> keys = getJedis().keys(keyPattern);
if (!keys.isEmpty()) {
for (String key : keys) {
Object entries;
entries = redisTemplate.opsForValue().get(key);
entries = getJedis().get(key);
res.put(key, entries);
}
result.add(res);
Expand All @@ -119,4 +145,4 @@ public List<Map<String, Object>> getAllKeysAndValues() {
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ public ResponseEntity<?> readQuestionList(@Valid @RequestBody Map<String, Object
SBApiResponse response = assessmentServiceV2.readQuestionList(requestBody, authUserToken);
return new ResponseEntity<>(response, response.getResponseCode());
}

@GetMapping("/v1/quml/assessment/retake/{assessmentIdentifier}")
public ResponseEntity<SBApiResponse> retakeAssessment(
@PathVariable("assessmentIdentifier") String assessmentIdentifier,
@RequestHeader(Constants.X_AUTH_TOKEN) String token) throws Exception {
SBApiResponse readResponse = assessmentServiceV2.retakeAssessment(assessmentIdentifier, token);
return new ResponseEntity<>(readResponse, readResponse.getResponseCode());
}
// QUML based Assessment APIs
// =======================
}
Loading