Skip to content

Commit

Permalink
Merge pull request #9 from ruksana2808/dev-4.8.18-get
Browse files Browse the repository at this point in the history
Added the code to redis cashing in the pagination API
  • Loading branch information
sharathkashyap authored Oct 17, 2024
2 parents e2224c1 + 78a9954 commit 7551227
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/tarento/commenthub/constant/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private Constants() {
public static final String USER_NAME = "userName";
public static final String PROFILE_IMG = "profileImageUrl";
public static final String CREATED_DATE = "createdDate";
public static final String OFFSET = "offset";
public static final String LIMIT = "limit";


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.networknt.schema.ValidationMessage;
import com.tarento.commenthub.constant.Constants;
import com.tarento.commenthub.dto.CommentTreeIdentifierDTO;
import com.tarento.commenthub.dto.MultipleWorkflowsCommentResponseDTO;
import com.tarento.commenthub.dto.CommentsResoponseDTO;
import com.tarento.commenthub.dto.ResponseDTO;
import com.tarento.commenthub.dto.SearchCriteria;
Expand All @@ -29,7 +28,6 @@
import com.tarento.commenthub.service.CommentTreeService;
import com.tarento.commenthub.transactional.cassandrautils.CassandraOperation;
import com.tarento.commenthub.transactional.utils.ApiResponse;
import com.tarento.commenthub.transactional.cassandrautils.CassandraOperation;
import com.tarento.commenthub.utility.Status;
import java.io.InputStream;
import java.sql.Timestamp;
Expand Down Expand Up @@ -111,10 +109,43 @@ public ResponseDTO addNewCommentToTree(JsonNode payload) {
((ObjectNode) payload).put(Constants.COMMENT_ID, comment.getCommentId());
CommentTree commentTree = commentTreeService.updateCommentTree(payload);

Pageable pageable = PageRequest.of(defaultOffset, defaultLimit,
Sort.by(Sort.Direction.DESC, Constants.CREATED_DATE));
JsonNode childNodes = commentTree.getCommentTreeData().get(Constants.FIRST_LEVEL_NODES);
List<String> childNodeList = objectMapper.convertValue(childNodes, List.class);
List<Comment> comments = commentRepository.findByCommentIdIn(childNodeList, pageable)
.getContent();
// Fetch from db and add fetched comments into redis
List<Map<String, Object>> userList = new ArrayList<>();
userList = fetchUsersByCommentData(comments);
CommentsResoponseDTO commentsResoponseDTO = new CommentsResoponseDTO(commentTree,
comments, userList);
Optional.ofNullable(comments)
.ifPresent(commentsList -> commentsResoponseDTO.setCommentCount(childNodeList.size()));
Map<String, Object> resultMap = objectMapper.convertValue(commentsResoponseDTO, Map.class);
resultMap = objectMapper.convertValue(commentsResoponseDTO, Map.class);

deleteAndUpdateTheRedisKey(String.valueOf(payload.get(Constants.COMMENT_TREE_ID)), resultMap);
ResponseDTO responseDTO = new ResponseDTO(commentTree, comment);
return responseDTO;
}

private void deleteAndUpdateTheRedisKey(String commentTreeId, Map<String, Object> resultMap) {
String token = generateRedisJwtTokenKey(commentTreeId
, defaultOffset, defaultLimit);
deleteRedisKey(commentTreeId);
redisTemplate.opsForValue()
.set(token,
resultMap, redisTtl,
TimeUnit.SECONDS);
}

private void deleteRedisKey(String commentTreeId) {
String token = generateRedisJwtTokenKey(commentTreeId
, defaultOffset, defaultLimit);
redisTemplate.delete(token);
}

@Override
public ResponseDTO updateExistingComment(JsonNode paylaod) {
validatePayload(Constants.UPDATE_EXISTING_COMMENT_VALIDATION_FILE, paylaod);
Expand Down Expand Up @@ -147,6 +178,7 @@ public ResponseDTO updateExistingComment(JsonNode paylaod) {
CommentTree commentTree =
commentTreeService.getCommentTreeById(paylaod.get(Constants.COMMENT_TREE_ID).asText());
ResponseDTO responseDTO = new ResponseDTO(commentTree, updatedComment);
deleteRedisKey(String.valueOf(paylaod.get(Constants.COMMENT_TREE_ID)));
return responseDTO;
}

Expand Down Expand Up @@ -303,9 +335,13 @@ public ApiResponse likeComment(Map<String, Object> likePayload) {
Comment commentToBeUpdated = optComment.get();
commentToBeUpdated.setCommentData(commentData);
Comment updatedComment = commentRepository.save(commentToBeUpdated);
redisTemplate.opsForValue()
.set(COMMENT_KEY + commentToBeUpdated.getCommentId(), updatedComment, redisTtl,
TimeUnit.SECONDS);
if (likePayload.containsKey(likePayload.get(Constants.COMMENT_TREE_ID))
&& StringUtils.isBlank((String) likePayload.get(Constants.COMMENT_TREE_ID))
&& likePayload.get(Constants.COMMENT_TREE_ID) != null) {
deleteRedisKey(
generateRedisJwtTokenKey((String) likePayload.get(Constants.COMMENT_TREE_ID),
defaultOffset, defaultLimit));
}
} else {
propertyMap.put(Constants.FLAG, likePayload.get(Constants.FLAG));
cassandraOperation.insertRecord(Constants.KEYSPACE_SUNBIRD, "comment_likes", propertyMap);
Expand All @@ -319,9 +355,13 @@ public ApiResponse likeComment(Map<String, Object> likePayload) {
Comment commentToBeUpdated = optComment.get();
commentToBeUpdated.setCommentData(commentData);
Comment updatedComment = commentRepository.save(commentToBeUpdated);
redisTemplate.opsForValue()
.set(COMMENT_KEY + commentToBeUpdated.getCommentId(), updatedComment, redisTtl,
TimeUnit.SECONDS);
if (likePayload.containsKey(likePayload.get(Constants.COMMENT_TREE_ID))
&& StringUtils.isBlank((String) likePayload.get(Constants.COMMENT_TREE_ID))
&& likePayload.get(Constants.COMMENT_TREE_ID) != null) {
deleteRedisKey(
generateRedisJwtTokenKey((String) likePayload.get(Constants.COMMENT_TREE_ID),
defaultOffset, defaultLimit));
}
}
} catch (Exception e) {
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -382,31 +422,35 @@ public ApiResponse paginatedComment(SearchCriteria searchCriteria) {
}
JsonNode childNodes = commentTree.get().getCommentTreeData().get(Constants.FIRST_LEVEL_NODES);
List<String> childNodeList = objectMapper.convertValue(childNodes, List.class);
log.info("CommentServiceImpl::getComments::fetch comments from redis");
// List<Comment> comments = redisTemplate.opsForValue().multiGet(getKeys(childNodeList));
List<Comment> comments = null;
if (containsNull(comments)) {
int limit = (searchCriteria.getLimit() != null) ? searchCriteria.getLimit() : defaultLimit;
int offset =
(searchCriteria.getOffset() != null) ? searchCriteria.getOffset() : defaultOffset;
Map<String, Object> resultMap = (Map<String, Object>) redisTemplate.opsForValue()
.get(generateRedisJwtTokenKey(commentTreeId, offset, limit));
if (MapUtils.isEmpty(resultMap)) {
log.info("CommentServiceImpl::getComments::fetch Comments from postgres");
int limit = (searchCriteria.getLimit() != null) ? searchCriteria.getLimit() : defaultLimit;
int offset =
(searchCriteria.getOffset() != null) ? searchCriteria.getOffset() : defaultOffset;

Pageable pageable = PageRequest.of(offset, limit,
Sort.by(Sort.Direction.DESC, Constants.CREATED_DATE));
comments = commentRepository.findByCommentIdIn(childNodeList, pageable).getContent();
List<Comment> comments = commentRepository.findByCommentIdIn(childNodeList, pageable)
.getContent();
// Fetch from db and add fetched comments into redis
List<Map<String, Object>> userList = new ArrayList<>();
userList = fetchUsersByCommentData(comments);
CommentsResoponseDTO commentsResoponseDTO = new CommentsResoponseDTO(commentTree.get(),
comments, userList);
Optional.ofNullable(comments)
.ifPresent(commentsList -> commentsResoponseDTO.setCommentCount(commentsList.size()));
Map<String, Object> resultMap = objectMapper.convertValue(commentsResoponseDTO, Map.class);

.ifPresent(commentsList -> commentsResoponseDTO.setCommentCount(childNodeList.size()));
resultMap = objectMapper.convertValue(commentsResoponseDTO, Map.class);
response.setResult(resultMap);
redisTemplate.opsForValue()
.set(generateRedisJwtTokenKey(commentTreeId, offset, limit), resultMap, redisTtl,
TimeUnit.SECONDS);
return response;
} else {
log.info("CommentServiceImpl::getComments::fetch comments from redis");
response.setResult(resultMap);
return response;
}
return null;
}

@Override
Expand Down Expand Up @@ -482,7 +526,7 @@ private String validateLikeCommentPayload(Map<String, Object> likePayload) {
errList.add(Constants.FLAG);
} else if (!Constants.LIKE.equalsIgnoreCase(voteType) && !Constants.DISLIKE.equalsIgnoreCase(
voteType)) {
errList.add("fla must be either 'like' or 'dislike'");
errList.add("flag must be either 'like' or 'dislike'");
}
if (!errList.isEmpty()) {
str.append("Failed Due To Missing Params - ").append(errList).append(".");
Expand Down Expand Up @@ -560,4 +604,17 @@ private List<Map<String, Object>> fetchUsersByCommentData (List<Comment> comment
return userList;
}

public String generateRedisJwtTokenKey(String commentTreeId, Integer offset, Integer limit) {
try {
return JWT.create()
.withClaim(Constants.COMMENT_TREE_ID, commentTreeId) // Add commentTreeId
.withClaim(Constants.OFFSET, offset) // Add offset
.withClaim(Constants.LIMIT, limit)
.sign(Algorithm.HMAC256(jwtSecretKey));
}catch (Exception e){
log.error("Excpetion occured while creating the redis token::", e);
}
return "";
}

}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ spring.redis.host=172.18.0.3
spring.redis.port=6379
#spring.redis.password=KZ9u%Z&mki4&p35
# 14 days in second 14 * 24 * 60 * 60
redis.ttl=86400
redis.ttl=300


#----------------------------Configure Logback pattern -----------------------
logging.pattern.console=%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
Expand Down

0 comments on commit 7551227

Please sign in to comment.