Skip to content

Commit

Permalink
use fetch from redis concept
Browse files Browse the repository at this point in the history
IN40068837 authored and IN40068837 committed Dec 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9484afb commit eb2f985
Showing 5 changed files with 82 additions and 7 deletions.
24 changes: 22 additions & 2 deletions src/main/java/com/iemr/hwc/Application.java
Original file line number Diff line number Diff line change
@@ -32,8 +32,13 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.client.RestTemplate;

import com.iemr.hwc.data.login.Users;
import com.iemr.hwc.fhir.config.fhirRestfulServer.FhirRestfulServer;
import com.iemr.hwc.utils.IEMRApplBeans;

@@ -57,11 +62,26 @@ public RestTemplate getRestTemplate() {
return new RestTemplate();
}

//Registering new fhir servlet for CHO mobile app
// Registering new fhir servlet for CHO mobile app
@Bean
public ServletRegistrationBean ServletRegistrationBean() {
ServletRegistrationBean registration= new ServletRegistrationBean(new FhirRestfulServer(context),"/fhir/*");
ServletRegistrationBean registration = new ServletRegistrationBean(new FhirRestfulServer(context), "/fhir/*");
registration.setName("FhirServlet");
return registration;
}

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);

// Use StringRedisSerializer for keys (userId)
template.setKeySerializer(new StringRedisSerializer());

// Use Jackson2JsonRedisSerializer for values (Users objects)
Jackson2JsonRedisSerializer<Users> serializer = new Jackson2JsonRedisSerializer<>(Users.class);
template.setValueSerializer(serializer);

return template;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/iemr/hwc/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.iemr.hwc.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

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

@Configuration
@EnableCaching
public class RedisConfig {

@Bean
public ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}

@Bean
public RedisTemplate<String, Users> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Users> template = new RedisTemplate<>();
template.setConnectionFactory(factory);

// Use StringRedisSerializer for keys (userId)
template.setKeySerializer(new StringRedisSerializer());

// Use Jackson2JsonRedisSerializer for values (Users objects)
Jackson2JsonRedisSerializer<Users> serializer = new Jackson2JsonRedisSerializer<>(Users.class);
template.setValueSerializer(serializer);

return template;
}

}
5 changes: 4 additions & 1 deletion src/main/java/com/iemr/hwc/data/login/Users.java
Original file line number Diff line number Diff line change
@@ -21,18 +21,21 @@
*/
package com.iemr.hwc.data.login;

import java.io.Serializable;
import java.sql.Timestamp;

import jakarta.persistence.*;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.annotations.Expose;
import com.iemr.hwc.data.location.DistrictBranchMapping;
import lombok.Data;

@Data
@Entity
@Table(name = "m_user")
public class Users {
@JsonIgnoreProperties(ignoreUnknown = true)
public class Users implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Expose
2 changes: 1 addition & 1 deletion src/main/java/com/iemr/hwc/repo/login/UserLoginRepo.java
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
public interface UserLoginRepo extends CrudRepository<Users, Long> {
@Query(" SELECT u FROM Users u WHERE u.userName = :UserName AND u.deleted = false ")
public Users getUserByUsername(@Param("UserName") String username);

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

20 changes: 17 additions & 3 deletions src/main/java/com/iemr/hwc/utils/JwtAuthenticationUtil.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
@@ -24,7 +25,7 @@ public class JwtAuthenticationUtil {
@Autowired
private JwtUtil jwtUtil;
@Autowired
private UserLoginRepo userLoginRepo;
private RedisTemplate<String, Object> redisTemplate;
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

public JwtAuthenticationUtil(CookieUtil cookieUtil, JwtUtil jwtUtil) {
@@ -70,8 +71,8 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException {

String userId = claims.get("userId", String.class);

// Fetch user based on userId from the database or cache
Users user = userLoginRepo.getUserByUserID(Long.parseLong(userId));
// Check if user data is present in Redis
Users user = getUserFromCache(userId);
if (user == null) {
throw new IEMRException("Invalid User ID.");
}
@@ -82,4 +83,17 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException {
throw new IEMRException("Validation error: " + e.getMessage(), e);
}
}

private Users getUserFromCache(String userId) {
String redisKey = "user_" + userId; // The Redis key format
Users user = (Users) redisTemplate.opsForValue().get(redisKey);

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

return user; // Returns null if not found
}
}

0 comments on commit eb2f985

Please sign in to comment.