|
2 | 2 |
|
3 | 3 | import org.baeldung.spring.data.redis.model.Student; |
4 | 4 | import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.data.redis.core.HashOperations; |
5 | 6 | import org.springframework.data.redis.core.RedisTemplate; |
6 | 7 | import org.springframework.stereotype.Repository; |
7 | 8 |
|
| 9 | +import javax.annotation.PostConstruct; |
8 | 10 | import java.util.Map; |
9 | 11 |
|
10 | 12 | @Repository |
11 | 13 | public class StudentRepositoryImpl implements StudentRepository { |
12 | 14 |
|
13 | 15 | private static final String KEY = "Student"; |
14 | 16 |
|
15 | | - @Autowired |
16 | 17 | private RedisTemplate<String, Object> redisTemplate; |
| 18 | + private HashOperations hashOperations; |
| 19 | + |
| 20 | + @Autowired |
| 21 | + public StudentRepositoryImpl(RedisTemplate<String, Object> redisTemplate) { |
| 22 | + this.redisTemplate = redisTemplate; |
| 23 | + } |
| 24 | + |
| 25 | + @PostConstruct |
| 26 | + private void init() { |
| 27 | + hashOperations = redisTemplate.opsForHash(); |
| 28 | + } |
17 | 29 |
|
18 | 30 | public void saveStudent(final Student student) { |
19 | | - redisTemplate.opsForHash().put(KEY, student.getId(), student); |
| 31 | + hashOperations.put(KEY, student.getId(), student); |
20 | 32 | } |
21 | 33 |
|
22 | 34 | public void updateStudent(final Student student) { |
23 | | - redisTemplate.opsForHash().put(KEY, student.getId(), student); |
| 35 | + hashOperations.put(KEY, student.getId(), student); |
24 | 36 | } |
25 | 37 |
|
26 | 38 | public Student findStudent(final String id) { |
27 | | - return (Student) redisTemplate.opsForHash().get(KEY, id); |
| 39 | + return (Student) hashOperations.get(KEY, id); |
28 | 40 | } |
29 | 41 |
|
30 | 42 | public Map<Object, Object> findAllStudents() { |
31 | | - return redisTemplate.opsForHash().entries(KEY); |
| 43 | + return hashOperations.entries(KEY); |
32 | 44 | } |
33 | 45 |
|
34 | 46 | public void deleteStudent(final String id) { |
35 | | - this.redisTemplate.opsForHash().delete(KEY, id); |
| 47 | + hashOperations.delete(KEY, id); |
36 | 48 | } |
37 | 49 | } |
0 commit comments