Skip to content

Commit

Permalink
[FEAT] redis putAll에서 put으로 변경 (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Aug 15, 2023
1 parent 1dda059 commit 2edb191
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ void test() {
TicketingCabinet ticketingCabinet = new TicketingCabinet(userName, wrongPasswordCount);
saveWithCustomPrefix(cabinetId, ticketingCabinet);

final Map<Object, Object> entries = redisTemplate.opsForHash().entries(cabinetId + ":" + userName);
final Map<Object, Object> entries = redisTemplate.opsForHash().entries(cabinetId);
System.out.println(entries);

userName = "daewoole";
wrongPasswordCount = 1;
TicketingCabinet ticketingCabinet2 = new TicketingCabinet(userName, wrongPasswordCount);
saveWithCustomPrefix(cabinetId, ticketingCabinet2);

final Map<Object, Object> entries2 = redisTemplate.opsForHash().entries(cabinetId + ":" + userName);
final Map<Object, Object> entries2 = redisTemplate.opsForHash().entries(cabinetId);
System.out.println(entries2);

// search all entries by prefix
Expand All @@ -73,11 +73,16 @@ void test() {
redisTemplate.opsForHash().keys(cabinetId).forEach(System.out::println);
}

public void saveWithCustomPrefix(String prefix, TicketingCabinet cabinet) {
String key = prefix + ":" + cabinet.getUserNameOrUserCount();
redisTemplate.opsForHash().putAll(key, convertToMap(cabinet));
// timeToLive 설정
redisTemplate.expire(key, 10, TimeUnit.SECONDS);
public void saveWithCustomPrefix(String cabinetId, TicketingCabinet tc) {
boolean hasKey = Boolean.TRUE.equals(redisTemplate.hasKey(cabinetId));
redisTemplate.opsForHash().put(cabinetId, tc.getUserNameOrUserCount(), tc.getWrongPasswordCountOrUserCount());
// redisTemplate.opsForHash().putAll(cabinetId, convertToMap(tc));
// 해당 키가 처음 생성된 것이라면 timeToLive 설정
if (!hasKey) {
System.out.println("set expire time");
// 10초 후에 삭제
redisTemplate.expire(cabinetId, 10, TimeUnit.SECONDS);
}
}

private Map<String, String> convertToMap(TicketingCabinet cabinet) {
Expand Down

0 comments on commit 2edb191

Please sign in to comment.