Java 코드 최적화
Pavlos Kosmetatos
Lead Engineer @Wealthyhood
캐싱은 자주 사용하는 요리 재료를 찬장이 아닌 조리대 위에 두는 것과 같습니다 - 꺼내기는 빠르지만 조리대 공간은 한정되어 있습니다
자주 캐싱하는 대상은 다음과 같습니다:
// In-memory cache using HashMap
public class SimpleCache<K, V> {
private final Map<K, V> cache = new HashMap<>();
public V get(K key) {
return cache.get(key);
}
public void put(K key, V value) {
cache.put(key, value);
}
}
Redis란:

import redis.clients.jedis.Jedis;
// Using Jedis client
Jedis jedis = new Jedis("localhost");
jedis.set("key", "value");
String value = jedis.get("key");
추가 기능:
public class RedisTimedCache {
private final Jedis jedis;
public RedisTimedCache(String host, int port) {
this.jedis = new Jedis(host, port);
}
public String get(String key) {
return jedis.get(key);
}
public void put(String key, String value, int timeToLiveSeconds) {
// Sets both the value and expiration time in seconds
jedis.setex(key, timeToLiveSeconds, value);
}
}
Java 코드 최적화