【redisキャッシュ】Redisパッケージクラス
RedisパッケージHash&String
redisTemplateを簡単にパッケージ化し、使いやすい Hashタイプの Stringタイプのデータ
redisTemplateを簡単にパッケージ化し、使いやすい
public class HashRedisService {
@Autowired
private RedisTemplate redisTemplate;
/**
*
*
* @param key
* @param timeout
* @param unit
*/
public void expire(String key, long timeout, TimeUnit unit) {
if (unit == null) {
unit = TimeUnit.SECONDS;
}
redisTemplate.expire(key, timeout, unit);
}
/**
* key set
*
* @param pattern
* @return
*/
public Set keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* key
*
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* key hash hashKey
*
* @param key
* @param hashKey
* @param value
*/
public void hSet(K key, HK hashKey, HV value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* key hash hashKey, hashKey value
*
* @param key
* @param hashKey
* @param value
*/
public void hSetIfAbsent(K key, HK hashKey, HV value) {
redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
}
/**
* key hash hashKey value
*
* @param key
* @param hashKey
* @return
*/
public HV hGet(K key, HK hashKey) {
return (HV) redisTemplate.opsForHash().get(key, hashKey);
}
/**
* key hash hashMap
*
* @param key
* @param hashMap
*/
public void hMSet(K key, Map extends HK, ? extends HV> hashMap) {
redisTemplate.opsForHash().putAll(key, hashMap);
}
/**
* key hash hashKeys i value
*
* @param key
* @param hashKeys
* @return
*/
public Map hMGet(K key, Collection hashKeys) {
Map hashMap = new HashMap();
List valueList = redisTemplate.opsForHash().multiGet(key, hashKeys);
int i = 0;
for (HK hashKey : hashKeys) {
hashMap.put(hashKey, valueList.get(i));
i++;
}
return hashMap;
}
/**
* key hash hashKeys i value
*
* @param key
* @param hashKeys
* @return
*/
public List hMGet2List(final K key, final Collection hashKeys) {
return redisTemplate.opsForHash().multiGet(key, hashKeys);
}
/**
* key hash value
*
* @param key
* @return
*/
public Map hVals(K key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* key hash value
*
* @param key
* @return
*/
public List hVals2List(K key) {
return redisTemplate.opsForHash().values(key);
}
/**
* key hash hashKey
*
* @param key
* @param hashKeys
*/
public void hDel(K key, HK... hashKeys) {
redisTemplate.opsForHash().delete(key, hashKeys);
}
/**
* key hash
*
* @param key
* @return
*/
public Long hLen(K key) {
return redisTemplate.opsForHash().size(key);
}
/**
* key hash hashKey
*
* @param key
* @param hashKey
* @return
*/
public boolean hasKey(K key, HK hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* key hash
*
* @param key
* @return
*/
public boolean hasKey(K key) {
return redisTemplate.hasKey(key);
}
/**
* key hash
*
* @param key
* @return
*/
public Set hKeys(K key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* key hash hashKey value Long
*
* @param key
* @param hashKey
* @param delta
* @return
*/
public Long hIncrBy(K key, HK hashKey, Long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, delta);
}
/**
* key hash hashKey value Double
*
* @param key
* @param hashKey
* @param stepSize
* @return
*/
public Double hIncrBy(K key, HK hashKey, Double stepSize) {
return redisTemplate.opsForHash().increment(key, hashKey, stepSize);
}
}
public class StringRedisService {
@Autowired
private RedisTemplate stringRedisTemplate;
/**
*
*
* @param key
* @param timeout
* @param unit
*/
public void expire(String key, long timeout, TimeUnit unit) {
if (unit == null) {
unit = TimeUnit.SECONDS;
}
stringRedisTemplate.expire(key, timeout, unit);
}
/**
* key set
*
* @param pattern
* @return
*/
public Set keys(String pattern) {
return stringRedisTemplate.keys(pattern);
}
/**
* key
*
* @param key
*/
public void delete(String key) {
stringRedisTemplate.delete(key);
}
/**
* key value
*
* @param key
* @param val
*/
public void set(String key, String val) {
stringRedisTemplate.opsForValue().set(key, val);
}
/**
* key, ( s )
*
* @param key
* @param val
* @param timeout
*/
public void set(String key, String val, long timeout) {
stringRedisTemplate.opsForValue().set(key, val, timeout, TimeUnit.SECONDS);
}
/**
* json
*
* @param key
* @param val
*/
public void setJsonObj(String key, Object val) throws JsonProcessingException {
stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val));
}
/**
* json , ( s )
*
* @param key
* @param val
* @param timeout
*/
public void setJsonObj(String key, Object val, long timeout) throws JsonProcessingException {
stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(val), timeout, TimeUnit.SECONDS);
}
/**
* , by key
*
* @param key
* @return
*/
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
/**
* json ,
*
* @param key
* @param clazz
* @param
*/
public T getJson(String key, Class clazz) {
String val = stringRedisTemplate.opsForValue().get(key);
return JsonUtils.fromJson(val, clazz);
}
/**
* key
* @param key
* @param start
* @param end
* @return
*/
public String substr(String key, Long start, Long end){
return stringRedisTemplate.opsForValue().get(key, start,end);
}
/**
*
* @param key
* @param val
* @return
*/
public String getAndSet(String key, String val){
return stringRedisTemplate.opsForValue().getAndSet(key, val);
}
public List multiGet(Collection keys){
return stringRedisTemplate.opsForValue().multiGet(keys);
}
public void setIfAbsent(String key, String val){
stringRedisTemplate.opsForValue().setIfAbsent(key,val);
}
public Long strlen(String key){
return stringRedisTemplate.opsForValue().size(key);
}
public void append(String key, String val){
stringRedisTemplate.opsForValue().append(key,val);
}
}