package com.common.redis.util;
|
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.data.redis.connection.RedisConnection;
|
import org.springframework.data.redis.core.RedisCallback;
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import java.util.Arrays;
|
import java.util.Collection;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* redis工具类
|
*
|
* @author lisheng
|
* @Date 2020/08/9 15:31
|
*/
|
public class RedisUtil {
|
|
private RedisTemplate<String, Object> template;
|
|
public RedisUtil(RedisTemplate<String, Object> template) {
|
this.template = template;
|
}
|
|
/**
|
* 设置过期时间
|
*
|
* @param key
|
* @param timeout
|
* @return
|
*/
|
public boolean expire(String key, long timeout) {
|
try {
|
if (timeout > 0) {
|
template.expire(key, timeout, TimeUnit.SECONDS);
|
}
|
return true;
|
} catch (Exception ex) {
|
return false;
|
}
|
}
|
|
/**
|
* 获取过期时间
|
*
|
* @param key
|
* @return
|
*/
|
public long getExpire(String key) {
|
return template.getExpire(key, TimeUnit.SECONDS);
|
}
|
|
/**
|
* key是否存在
|
*
|
* @param key
|
* @return
|
*/
|
public boolean hasKey(String key) {
|
try {
|
return template.hasKey(key);
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
/**
|
* 删除key
|
*
|
* @param key
|
*/
|
public void deleteKey(String... key) {
|
if (key != null && key.length > 0) {
|
if (key.length == 1) {
|
template.delete(key[0]);
|
} else {
|
template.delete(Arrays.asList(key));
|
}
|
}
|
}
|
|
/**
|
* 根据key获取value
|
*
|
* @param key
|
* @return
|
*/
|
public Object get(String key) {
|
if (key == null) {
|
return null;
|
}
|
return template.opsForValue().get(key);
|
}
|
|
/**
|
* 根据key和类型获取value
|
*
|
* @param key
|
* @param classType
|
* @param <T>
|
* @return
|
*/
|
public <T> T getObject(String key, Class<T> classType) {
|
return (T) this.get(key);
|
}
|
|
/**
|
* 根据key和类型获取value list
|
*
|
* @param key
|
* @param classType
|
* @param <T>
|
* @return
|
*/
|
public <T> List<T> getObjects(String key, Class<T> classType) {
|
return (List<T>) get(key);
|
}
|
|
/**
|
* 设置缓存
|
*
|
* @param key
|
* @param value
|
* @return
|
*/
|
public boolean set(String key, Object value) {
|
try {
|
template.opsForValue().set(key, value);
|
return true;
|
} catch (Exception ex) {
|
return false;
|
}
|
}
|
|
/**
|
* 设置过期时间缓存
|
*
|
* @param key
|
* @param value
|
* @param timeout
|
* @return
|
*/
|
public boolean set(String key, Object value, long timeout) {
|
try {
|
if (timeout > 0) {
|
template.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
|
} else {
|
return set(key, value);
|
}
|
return true;
|
} catch (Exception ex) {
|
return false;
|
}
|
}
|
|
/**
|
* 获取Redis列表
|
*
|
* @param key
|
* @return
|
*/
|
public Object lGet(String key) {
|
if (key == null) {
|
return null;
|
}
|
return template.opsForList().rightPop(key);
|
}
|
|
/**
|
* 设置缓存列表类型
|
*
|
* @param key
|
* @param values
|
* @return
|
*/
|
public boolean lSet(String key, List<?> values) {
|
try {
|
template.opsForList().rightPush(key, values);
|
return true;
|
} catch (Exception ex) {
|
return false;
|
}
|
}
|
|
/**
|
* 设置hashmap类型缓存
|
*
|
* @param key
|
* @param field
|
* @param value
|
* @param <T>
|
* @return
|
*/
|
public <T> boolean hset(String key, String field, T value) {
|
template.opsForHash().put(key, field, value);
|
return true;
|
}
|
|
public <T> boolean hset(String key, Object field, T value) {
|
template.opsForHash().put(key, field, value);
|
return true;
|
}
|
|
|
/**
|
* 按hashmap key获取缓存中的数据
|
*
|
* @param key
|
* @param fields
|
* @return
|
*/
|
public List<Object> hashMultiGet(String key, Collection<Object> fields) {
|
if (key == null) {
|
return null;
|
}
|
return template.opsForHash().multiGet(key, fields);
|
}
|
|
/**
|
* 获取单个hashmap中的值
|
*
|
* @param key
|
* @param field
|
* @return
|
*/
|
public Object hashSingleGet(String key, Object field) {
|
if (key == null) {
|
return null;
|
}
|
|
return template.opsForHash().get(key, field);
|
}
|
|
/**
|
* 获取单个hashmap中的值
|
*
|
* @param key
|
* @return
|
*/
|
public Map<Object, Object> hashGetAll(String key) {
|
if (key == null) {
|
return null;
|
}
|
|
return template.opsForHash().entries(key);
|
}
|
|
|
/**
|
* delete 指定某些hashmap中的值
|
*
|
* @param key
|
* @param fields
|
* @return
|
*/
|
public Object hDelFields(String key, Object... fields) {
|
if (key == null) {
|
return null;
|
}
|
|
return template.opsForHash().delete(key, fields);
|
}
|
|
/**
|
* 缓存自增
|
*
|
* @param key
|
* @param delta
|
* @return
|
*/
|
public Long incr(String key, long delta) {
|
return template.opsForValue().increment(key, delta);
|
}
|
|
|
public Set<String> getKeys(String prex){
|
return template.keys(prex);
|
}
|
}
|