Skip to content

Commit

Permalink
Add Redis configuration and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinh-Tan-Dat committed Sep 15, 2024
1 parent 337c06e commit 9d27243
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@



<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.2.4</version>
</dependency>








Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/programming/userService/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.programming.userService.config;

import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.beans.factory.annotation.Value;
@Configuration
@EnableRedisRepositories
public class RedisConfig {

@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.password}")
private String redisPassword;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName(redisHost);
redisConfig.setPort(redisPort);
redisConfig.setPassword(redisPassword);

return new LettuceConnectionFactory(redisConfig);
}


@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
Expand All @@ -33,14 +34,17 @@
import com.programming.userService.util.JwtUtil;
import com.programming.userService.entity.CustomUserDetails;
import com.programming.userService.entity.AuthUser;
import org.springframework.data.redis.core.RedisTemplate;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

private final AuthUserRepository userRepository;
private final PasswordEncoder passwordEncoder;

private final RedisTemplate<String, Object> redisTemplate;
private static final String USER_CACHE = "USER_CACHE";
@Autowired
private JwtUtil jwtUtil;

Expand Down Expand Up @@ -164,10 +168,14 @@ public ResponseEntity listUserbyUsername(@RequestBody AuthUser user) {
}
}

@Cacheable(value = USER_CACHE, key = "#id")
@GetMapping("/listUserbyId/{id}")
public ResponseEntity listUserbyId(@PathVariable("id") String id) {
try {
return ResponseEntity.ok(userRepository.findById(id));
AuthUser user = userRepository.findById(id).orElseThrow(() -> new Exception("User not found"));
// Save user in Redis cache
redisTemplate.opsForHash().put(USER_CACHE, id, user);
return ResponseEntity.ok(user);
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;
import java.io.Serializable;

@Data
@Builder
@Document("user")
public class AuthUser {
public class AuthUser implements Serializable {
@Id
private String id;
@Indexed
Expand All @@ -24,4 +25,7 @@ public class AuthUser {
private byte[] avatar;
private String email;
private Date timestamp;


private static final long serialVersionUID = 1L;
}
6 changes: 6 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ spring.mail.username=21520714@gm.uit.edu.vn
spring.mail.password=vkux umrv rtkd svsy
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true


# Redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=

0 comments on commit 9d27243

Please sign in to comment.