Skip to content

Commit 5c72cf6

Browse files
committed
Add Redis configuration and dependencies
1 parent 091e215 commit 5c72cf6

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@
8787

8888

8989

90+
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
91+
<dependency>
92+
<groupId>redis.clients</groupId>
93+
<artifactId>jedis</artifactId>
94+
<version>5.1.0</version>
95+
</dependency>
96+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
97+
<dependency>
98+
<groupId>org.springframework.boot</groupId>
99+
<artifactId>spring-boot-starter-data-redis</artifactId>
100+
<version>3.2.4</version>
101+
</dependency>
90102

91103

92104

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.programming.videoService.config;
2+
3+
import org.springframework.data.redis.core.RedisTemplate;
4+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
5+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
6+
import org.springframework.data.redis.serializer.StringRedisSerializer;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.data.redis.connection.RedisConnectionFactory;
10+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
11+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
12+
import org.springframework.beans.factory.annotation.Value;
13+
@Configuration
14+
@EnableRedisRepositories
15+
public class RedisConfig {
16+
@Value("${spring.redis.host}")
17+
private String redisHost;
18+
19+
@Value("${spring.redis.port}")
20+
private int redisPort;
21+
22+
@Value("${spring.redis.password}")
23+
private String redisPassword;
24+
25+
26+
@Bean
27+
public RedisConnectionFactory redisConnectionFactory() {
28+
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
29+
redisConfig.setHostName(redisHost);
30+
redisConfig.setPort(redisPort);
31+
redisConfig.setPassword(redisPassword);
32+
33+
return new LettuceConnectionFactory(redisConfig);
34+
}
35+
36+
@Bean
37+
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
38+
RedisTemplate<String, Object> template = new RedisTemplate<>();
39+
template.setConnectionFactory(connectionFactory);
40+
template.setKeySerializer(new StringRedisSerializer());
41+
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
42+
return template;
43+
}
44+
}

src/main/java/com/programming/videoService/service/VideoService.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.data.mongodb.core.query.Update;
1818
import org.springframework.data.mongodb.gridfs.GridFsOperations;
1919
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
20+
import org.springframework.data.redis.core.RedisTemplate;
2021
import org.springframework.stereotype.Service;
2122
import org.springframework.web.multipart.MultipartFile;
2223

@@ -25,6 +26,7 @@
2526
import java.io.InputStream;
2627
import java.sql.Timestamp;
2728
import java.util.ArrayList;
29+
import java.util.HashMap;
2830
import java.util.List;
2931
import java.util.Map;
3032

@@ -40,6 +42,11 @@ public class VideoService {
4042
@Autowired
4143
private MongoTemplate mongoTemplate;
4244

45+
@Autowired
46+
private RedisTemplate<String, Object> redisTemplate;
47+
48+
private final String VIDEO_CACHE_PREFIX = "video_";
49+
4350
public String addVideo(MultipartFile upload, String userID, byte[] thumbnail, Timestamp timestamp, String description, String userName, String videoName)
4451
throws IOException {
4552
DBObject videoMetadata = new BasicDBObject();
@@ -64,6 +71,17 @@ public String addVideo(MultipartFile upload, String userID, byte[] thumbnail, Ti
6471
template.store(new ByteArrayInputStream(thumbnail), upload.getOriginalFilename() + "_thumbnail", "image/png",
6572
thumbnailMetadata);
6673

74+
75+
// Save video information to Redis
76+
Map<String, Object> videoDetails = new HashMap<>();
77+
videoDetails.put("videoId", videoID.toString());
78+
videoDetails.put("userID", userID);
79+
videoDetails.put("title", videoName);
80+
videoDetails.put("description", description);
81+
videoDetails.put("views", 0);
82+
redisTemplate.opsForHash().putAll(VIDEO_CACHE_PREFIX + videoID.toString(), videoDetails);
83+
84+
6785
return videoID.toString();
6886
}
6987

@@ -149,11 +167,25 @@ public void setInputStream(InputStream inputStream) {
149167
}
150168

151169
public Map<String, Object> getDetails(String videoId) {
170+
// Checked data in Redis
171+
Map<Object, Object> cachedVideo = redisTemplate.opsForHash().entries(VIDEO_CACHE_PREFIX + videoId);
172+
173+
if (!cachedVideo.isEmpty()) {
174+
// Convert cachedVideo to Map<String, Object>
175+
Map<String, Object> stringKeyVideoMap = new HashMap<>();
176+
cachedVideo.forEach((key, value) -> stringKeyVideoMap.put(String.valueOf(key), value));
177+
return stringKeyVideoMap;
178+
}
179+
152180
Query query = new Query(Criteria.where("_id").is(videoId));
153181
DBObject dbObject = mongoTemplate.findOne(query, DBObject.class, "fs.files");
154182
if (dbObject != null) {
155-
return dbObject.toMap();
183+
Map<String, Object> videoDetails = dbObject.toMap();
184+
// Save video information to Redis
185+
redisTemplate.opsForHash().putAll(VIDEO_CACHE_PREFIX + videoId, videoDetails);
186+
return videoDetails;
156187
}
188+
157189
return null;
158190
}
159191

src/main/resources/application.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ [email protected]
2020
spring.mail.password=vkux umrv rtkd svsy
2121
spring.mail.properties.mail.smtp.auth=true
2222
spring.mail.properties.mail.smtp.starttls.enable=true
23+
24+
25+
26+
# Redis
27+
spring.redis.host=localhost
28+
spring.redis.port=6379
29+
spring.redis.password=

0 commit comments

Comments
 (0)