-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamCacheableRepository.java
More file actions
33 lines (25 loc) · 1.1 KB
/
TeamCacheableRepository.java
File metadata and controls
33 lines (25 loc) · 1.1 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
package system.design.interview.domain.team;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import system.design.interview.CacheableRepository;
import java.util.Optional;
@Repository
public class TeamCacheableRepository extends CacheableRepository<Team, Long> {
private static final String PREFIX = Team.class.getName() + "_";
public TeamCacheableRepository(TeamRepository teamRepository,
RedisTemplate<String, Object> redisTemplate) {
super(teamRepository, redisTemplate.opsForHash(), PREFIX);
}
@Transactional
public Optional<Team> update(Long id, String changedTeamName) {
String key = PREFIX + id.toString();
Optional<Team> entity = jpaRepository.findById(id);
if (entity.isEmpty()) {
return Optional.empty();
}
deleteInRedisIfExist(key, entity.get());
entity.get().update(changedTeamName);
return Optional.of(jpaRepository.save(entity.get()));
}
}