-
Notifications
You must be signed in to change notification settings - Fork 37
added cache while project create for fetching parent project ids #1576
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
base: master
Are you sure you want to change the base?
Changes from all commits
8707f29
d474910
f4671e1
6a1470b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import org.egov.project.repository.ProjectRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
@@ -32,10 +33,13 @@ | |
| public class PfProjectIdValidator implements Validator<ProjectFacilityBulkRequest, ProjectFacility> { | ||
|
|
||
| private final ProjectRepository projectRepository; | ||
| private final RedisTemplate<String,String> redisTemplate; | ||
|
|
||
|
|
||
| @Autowired | ||
| public PfProjectIdValidator(ProjectRepository projectRepository) { | ||
| public PfProjectIdValidator(ProjectRepository projectRepository,RedisTemplate redisTemplate) { | ||
| this.projectRepository = projectRepository; | ||
| this.redisTemplate = redisTemplate; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -52,9 +56,28 @@ public Map<ProjectFacility, List<Error>> validate(ProjectFacilityBulkRequest req | |
| Map<String, ProjectFacility> eMap = getIdToObjMap(validEntities, idMethod); | ||
| if (!eMap.isEmpty()) { | ||
| List<String> entityIds = new ArrayList<>(eMap.keySet()); | ||
| List<String> existingProjectIds = projectRepository.validateIds(entityIds, | ||
| List<String> existingProjectIds = new ArrayList<>(); | ||
| List<String> cacheMissIds = new ArrayList<>(); | ||
|
|
||
| for (String id : entityIds) { | ||
| String redisKey = "project-create-cache-" + id; | ||
| try { | ||
| if (Boolean.TRUE.equals(redisTemplate.hasKey(redisKey))) { | ||
| existingProjectIds.add(id); | ||
| } else { | ||
| cacheMissIds.add(id); | ||
| } | ||
| } catch (Exception ex) { | ||
| log.error("Redis error while checking key: {}", redisKey, ex); | ||
| cacheMissIds.add(id); // safely fallback to DB | ||
| } | ||
| } | ||
| if (!cacheMissIds.isEmpty()) { | ||
| List<String> dbValidIds = projectRepository.validateIds(cacheMissIds, | ||
| getIdFieldName(idMethod)); | ||
| List<ProjectFacility> invalidEntities = validEntities.stream().filter(notHavingErrors()).filter(entity -> | ||
| existingProjectIds.addAll(dbValidIds); | ||
| } | ||
|
Comment on lines
+59
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Consider extracting common Redis caching logic. The Redis caching logic here is nearly identical to Example approach:
🤖 Prompt for AI Agents |
||
| List<ProjectFacility> invalidEntities = validEntities.stream().filter(notHavingErrors()).filter(entity -> | ||
| !existingProjectIds.contains(entity.getProjectId())) | ||
| .collect(Collectors.toList()); | ||
| invalidEntities.forEach(projectFacility -> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||||||||||||||||||
| import org.egov.project.repository.ProjectRepository; | ||||||||||||||||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||||||||||||||||||||||||
| import org.springframework.core.annotation.Order; | ||||||||||||||||||||||||||||||||||||||
| import org.springframework.data.redis.core.RedisTemplate; | ||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Component; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import java.lang.reflect.Method; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -31,10 +32,12 @@ | |||||||||||||||||||||||||||||||||||||
| public class PsProjectIdValidator implements Validator<ProjectStaffBulkRequest, ProjectStaff> { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private final ProjectRepository projectRepository; | ||||||||||||||||||||||||||||||||||||||
| private final RedisTemplate<String,String> redisTemplate; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||||||||||||||||
| public PsProjectIdValidator(ProjectRepository projectRepository) { | ||||||||||||||||||||||||||||||||||||||
| public PsProjectIdValidator(ProjectRepository projectRepository,RedisTemplate redisTemplate) { | ||||||||||||||||||||||||||||||||||||||
| this.projectRepository = projectRepository; | ||||||||||||||||||||||||||||||||||||||
| this.redisTemplate = redisTemplate; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -49,8 +52,28 @@ public Map<ProjectStaff, List<Error>> validate(ProjectStaffBulkRequest request) | |||||||||||||||||||||||||||||||||||||
| .stream().filter(notHavingErrors()).collect(Collectors.toList()), idMethod); | ||||||||||||||||||||||||||||||||||||||
| if (!eMap.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||
| List<String> entityIds = new ArrayList<>(eMap.keySet()); | ||||||||||||||||||||||||||||||||||||||
| List<String> existingProjectIds = projectRepository.validateIds(entityIds, | ||||||||||||||||||||||||||||||||||||||
| List<String> existingProjectIds = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||
| List<String> cacheMissIds = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| for (String id : entityIds) { | ||||||||||||||||||||||||||||||||||||||
| String redisKey = "project-create-cache-" + id; | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Extract Redis key pattern as a constant for better maintainability. The Redis key pattern is hardcoded. Consider extracting it as a constant to maintain consistency across the codebase. Add a constant at the class level: public class PsProjectIdValidator implements Validator<ProjectStaffBulkRequest, ProjectStaff> {
+ private static final String PROJECT_CACHE_KEY_PREFIX = "project-create-cache-";
private final ProjectRepository projectRepository;
private final RedisTemplate<String,String> redisTemplate;Then update line 59: - String redisKey = "project-create-cache-" + id;
+ String redisKey = PROJECT_CACHE_KEY_PREFIX + id;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| if (Boolean.TRUE.equals(redisTemplate.hasKey(redisKey))) { | ||||||||||||||||||||||||||||||||||||||
| existingProjectIds.add(id); // found in cache | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| cacheMissIds.add(id); // not in cache | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| }catch (Exception ex) { | ||||||||||||||||||||||||||||||||||||||
| log.error("Redis error while checking key: {}", redisKey, ex); | ||||||||||||||||||||||||||||||||||||||
| cacheMissIds.add(id); // fallback to DB if Redis fails | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| // Fallback to DB only for cache misses | ||||||||||||||||||||||||||||||||||||||
| if (!cacheMissIds.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||
| List<String> dbValidIds = projectRepository.validateIds(cacheMissIds, | ||||||||||||||||||||||||||||||||||||||
| getIdFieldName(idMethod)); | ||||||||||||||||||||||||||||||||||||||
| existingProjectIds.addAll(dbValidIds); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| List<ProjectStaff> invalidEntities = entities.stream().filter(notHavingErrors()).filter(entity -> | ||||||||||||||||||||||||||||||||||||||
| !existingProjectIds.contains(entity.getProjectId())) | ||||||||||||||||||||||||||||||||||||||
| .collect(Collectors.toList()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.