package io.renren.common.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** * @program: ivvdata-security * @description: redis工具類 * @author: HYJ * @create: 2019-09-27 18:00 */ @Component public class RedisUtil { @Autowired private StringRedisTemplate stringRedisTemplate; @Resource private RedisTemplate<String, Object> redisTemplate; /** * 存儲數據String類型 */ public void set(String key, String value){ ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); ops.set(key, value); } /** * 獲取數據Stirng類型 */ public String get(String key){ ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); return ops.get(key); } /** * 存儲數據 * @param key * @param value */ public void setObject(String key, Object value) { ValueOperations<String, Object> ops = redisTemplate.opsForValue(); ops.set(key, value); } /** * 獲取數據 * @param key * @return */ public Object getObject(String key) { ValueOperations<String, Object> ops = redisTemplate.opsForValue(); return ops.get(key); } /** * 添加數據到list集合【操作List】 * @param key List名稱 * @param value 存放的數值 * @return 存放完畢之后,當前List的長度 */ public Long leftPush(String key, String value) { ListOperations<String, Object> opsForList = redisTemplate.opsForList(); Long length = opsForList.leftPush(key,value); return length; } /** * 向List集合中存放集合 * @param key 被操作的List * @param list 需要添加的List * @return 存放完畢之后,當前List的長度 */ public Long leftPushAll(String key, List<Object> list) { ListOperations<String, Object> opsForList = redisTemplate.opsForList(); return opsForList.leftPushAll(key,list); } /** * 獲取List集合中的數據 * @param key List名稱 * @return list中存放的數據 */ public List<Object> range(String key) { ListOperations<String, Object> opsForList = redisTemplate.opsForList(); return opsForList.range(key, 0, -1); } /** * 移除List中的元素 * @param key 被操作的List * @param count 要刪除的元素個數【為負數表示從尾部,向頭部刪除;正數則相反】 * @param value 要刪除的元素值 * @return 移除完畢之后,當前List的長度 */ public Long remove(String key, long count, Object value) { ListOperations<String, Object> opsForList = redisTemplate.opsForList(); return opsForList.remove(key,count,value); } /** * 存儲數據或修改數據 * * @param modelMap * @param mapName */ public void setKey(String mapName, Map<String, Object> modelMap) { HashOperations<String, String, Object> hps = redisTemplate.opsForHash(); hps.putAll(mapName, modelMap); } /** * 獲取數據Map * * @param mapName * @return */ public Map<String, Object> getMapValue(String mapName) { HashOperations<String, String, Object> hps = this.redisTemplate.opsForHash(); return hps.entries(mapName); } /** * 獲取數據value * * @param mapName * @param hashKey * @return */ public Object getValue(String mapName, String hashKey) { HashOperations<String, String, Object> hps = this.redisTemplate.opsForHash(); return hps.get(mapName, hashKey); } /** * 批量刪除緩存數據 * * @param keys */ public void deleteData(List<String> keys) { // 執行批量刪除操作時先序列化template redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer()); redisTemplate.delete(keys); } /** * 設置超期時間 */ public boolean expire(String key, long expire){ Boolean expire2 = stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS); return expire2; } /** * 刪除String類型數據 */ public void remove(String key){ stringRedisTemplate.delete(key); }
/**
* 刪除數據
*/
public void removex(String key) {
redisTemplate.delete(key);
}
/** * 自增操作 * @param delta 自增步長 */ public Long increment(String key, long delta){ return stringRedisTemplate.opsForValue().increment(key,delta); } }