Created
March 26, 2013 08:22
-
-
Save debop/5243867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
public class RedisCache implements Cache { | |
@Getter | |
private String name; | |
@Getter | |
private int expireSeconds; | |
private RedisTemplate redisTemplate; | |
public RedisCache(String name, RedisTemplate redisTemplate) { | |
this(name, redisTemplate, 300); | |
} | |
public RedisCache(String name, RedisTemplate redisTemplate, int expireSeconds) { | |
Guard.shouldNotBeEmpty(name, "name"); | |
Guard.shouldNotBeNull(redisTemplate, "redisTemplate"); | |
this.name = name; | |
this.redisTemplate = redisTemplate; | |
if (log.isDebugEnabled()) | |
log.debug("MongoCache를 생성합니다. name=[{}], mongodb=[{}]", name, redisTemplate); | |
} | |
@Override | |
public Object getNativeCache() { | |
return redisTemplate; | |
} | |
public String getKey(Object key) { | |
return name + ":" + key; | |
} | |
@Override | |
public ValueWrapper get(Object key) { | |
Guard.shouldNotBeNull(key, "key"); | |
if (log.isDebugEnabled()) | |
log.debug("캐시 키[{}] 값을 구합니다...", key); | |
Object result = redisTemplate.opsForValue().get(getKey(key)); | |
SimpleValueWrapper wrapper = null; | |
if (result != null) { | |
if (log.isDebugEnabled()) | |
log.debug("캐시 값을 로드했습니다. key=[{}]", key); | |
wrapper = new SimpleValueWrapper(result); | |
} | |
return wrapper; | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public void put(Object key, Object value) { | |
Guard.shouldNotBeNull(key, "key"); | |
if (log.isDebugEnabled()) | |
log.debug("캐시에 값을 저장합니다. key=[{}], value=[{}]", key, value); | |
redisTemplate.opsForValue().set(getKey(key), value, expireSeconds); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public void evict(Object key) { | |
Guard.shouldNotBeNull(key, "key"); | |
if (log.isDebugEnabled()) | |
log.debug("지정한 키[{}]의 캐시를 삭제합니다...", key); | |
try { | |
redisTemplate.delete(key); | |
} catch (Exception e) { | |
log.error("캐시 항목 삭제에 실패했습니다. key=" + key, e); | |
} | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public void clear() { | |
if (log.isDebugEnabled()) | |
log.debug("모든 캐시를 삭제합니다..."); | |
try { | |
redisTemplate.execute(new RedisCallback() { | |
@Override | |
public Object doInRedis(RedisConnection connection) throws DataAccessException { | |
connection.flushAll(); | |
return null; | |
} | |
}); | |
} catch (Exception e) { | |
log.warn("모든 캐시를 삭제하는데 실패했습니다.", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment