|
@@ -3,51 +3,51 @@ package com.ym.mec.common.redis.service;
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.ym.mec.common.cache.Cache;
|
|
|
import com.ym.mec.common.cache.CacheException;
|
|
|
|
|
|
-@Component
|
|
|
-public class RedisCache implements Cache {
|
|
|
+public class RedisCache<K, V> implements Cache<K, V> {
|
|
|
|
|
|
- @Autowired
|
|
|
- private RedisTemplate<String, Object> redisTemplate;
|
|
|
+ private RedisTemplate<K, V> redisTemplate;
|
|
|
+
|
|
|
+ public RedisCache(RedisTemplate<K, V> redisTemplate) {
|
|
|
+ this.redisTemplate = redisTemplate;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
- public Object get(String key) throws CacheException {
|
|
|
- ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
|
|
|
+ public V get(K key) throws CacheException {
|
|
|
+ ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
|
|
|
return valueOps.get(key);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void put(String key, Object value) throws CacheException {
|
|
|
- ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
|
|
|
+ public void put(K key, V value) throws CacheException {
|
|
|
+ ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
|
|
|
valueOps.set(key, value);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void put(String key, Object value, int expireTimes) throws CacheException {
|
|
|
- ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
|
|
|
+ public void put(K key, V value, int expireTimes) throws CacheException {
|
|
|
+ ValueOperations<K, V> valueOps = redisTemplate.opsForValue();
|
|
|
valueOps.set(key, value, expireTimes, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void delete(String key) throws CacheException {
|
|
|
+ public void delete(K key) throws CacheException {
|
|
|
redisTemplate.delete(key);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean exists(String key) {
|
|
|
+ public boolean exists(K key) {
|
|
|
return redisTemplate.hasKey(key);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Set<String> keys() throws CacheException {
|
|
|
- return redisTemplate.keys("*");
|
|
|
+ public Set<K> keys() throws CacheException {
|
|
|
+ return redisTemplate.keys((K) "*");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -56,11 +56,11 @@ public class RedisCache implements Cache {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void expire(String key, int expireTimes) {
|
|
|
+ public void expire(K key, int expireTimes) {
|
|
|
redisTemplate.expire(key, expireTimes, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
- public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
|
|
|
this.redisTemplate = redisTemplate;
|
|
|
}
|
|
|
}
|