-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheableRepository.java
More file actions
69 lines (60 loc) · 2.37 KB
/
CacheableRepository.java
File metadata and controls
69 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package system.design.interview;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.transaction.annotation.Transactional;
import system.design.interview.domain.BaseEntity;
import java.util.List;
import java.util.Optional;
@RequiredArgsConstructor
@Slf4j
public abstract class CacheableRepository<E extends BaseEntity, ID> {
protected final JpaRepository<E, ID> jpaRepository;
private final HashOperations<String, String, E> redisHashSupport;
private final String prefix;
@Transactional
public E save(E entity) {
E savedEntity = jpaRepository.save(entity);
String key = prefix + savedEntity.getId();
log.info("try to save {" + savedEntity + "} in redis.");
redisHashSupport.put(key, key, savedEntity);
log.info("save ok {" + savedEntity + "} in redis.");
log.info("try to save {" + savedEntity + "} in database.");
return savedEntity;
}
public List<E> findAll() {
return jpaRepository.findAll();
}
@SuppressWarnings("unchecked")
public Optional<E> findById(ID id) {
String key = prefix + id.toString();
E value = redisHashSupport.get(key, key);
if (value == null) {
log.info("{" + key + "} is not exist in redis. try to find data in db...");
Optional<E> entity = jpaRepository.findById(id);
if (entity.isEmpty()) {
log.info("{" + key + "} is not exist in db.");
return Optional.empty();
}
log.info("{" + entity + "} is exist in db. insert in redis and return.");
redisHashSupport.put(key, key, entity.get());
return entity;
}
return Optional.of(value);
}
@Transactional
public void deleteById(ID id) {
String key = prefix + id.toString();
E value = redisHashSupport.get(key, key);
deleteInRedisIfExist(key, value);
log.info("{" + value + "} will delete from db.");
jpaRepository.deleteById(id);
}
protected void deleteInRedisIfExist(String key, E value) {
if (value != null) {
log.info("{" + value + "} is exist in redis. delete from redis.");
redisHashSupport.delete(key, key);
}
}
}