Skip to content

Commit

Permalink
user fetch concept modify
Browse files Browse the repository at this point in the history
  • Loading branch information
IN40068837 authored and IN40068837 committed Dec 24, 2024
1 parent 169c3a4 commit a6327a4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/iemr/tm/data/login/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ public class Users implements Serializable {
private Long userID;
@Column(name = "UserName")
private String userName;
@Column(name = "Deleted", insertable = false, updatable = false)
private Boolean Deleted;

}
16 changes: 16 additions & 0 deletions src/main/java/com/iemr/tm/repo/login/UserLoginRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.iemr.tm.repo.login;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.iemr.tm.data.login.Users;

@Repository
public interface UserLoginRepo extends CrudRepository<Users, Long> {

@Query(" SELECT u FROM Users u WHERE u.userID = :userID AND u.Deleted = false ")
public Users getUserByUserID(@Param("userID") Long userID);

}
29 changes: 28 additions & 1 deletion src/main/java/com/iemr/tm/utils/JwtAuthenticationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Component;

import com.iemr.tm.data.login.Users;
import com.iemr.tm.repo.login.UserLoginRepo;
import com.iemr.tm.utils.exception.IEMRException;

import io.jsonwebtoken.Claims;
Expand All @@ -26,6 +27,8 @@ public class JwtAuthenticationUtil {
private JwtUtil jwtUtil;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private UserLoginRepo userLoginRepo;
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

public JwtAuthenticationUtil(CookieUtil cookieUtil, JwtUtil jwtUtil) {
Expand Down Expand Up @@ -73,6 +76,10 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException {

// Check if user data is present in Redis
Users user = getUserFromCache(userId);
if (user == null) {
// If not in Redis, fetch from DB and cache the result
user = fetchUserFromDB(userId);
}
if (user == null) {
throw new IEMRException("Invalid User ID.");
}
Expand All @@ -89,12 +96,32 @@ private Users getUserFromCache(String userId) {
Users user = (Users) redisTemplate.opsForValue().get(redisKey);

if (user == null) {
logger.warn("User not found in Redis.");
logger.warn("User not found in Redis. Will try to fetch from DB.");
} else {
logger.info("User fetched successfully from Redis.");
}

return user; // Returns null if not found
}

private Users fetchUserFromDB(String userId) {
// This method will only be called if the user is not found in Redis.
String redisKey = "user_" + userId; // Redis key format

// Fetch user from DB
Users user = userLoginRepo.getUserByUserID(Long.parseLong(userId));

if (user != null) {
// Cache the user in Redis for future requests (cache for 30 minutes)
redisTemplate.opsForValue().set(redisKey, user, 30, TimeUnit.MINUTES);

// Log that the user has been stored in Redis
logger.info("User stored in Redis with key: " + redisKey);
} else {
logger.warn("User not found for userId: " + userId);
}

return user;
}

}

0 comments on commit a6327a4

Please sign in to comment.