package com.caimei.components; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /** * Redis 服务工具类 * * @author : Charles * @date : 2021/3/4 */ @Slf4j @Service public class RedisService { @Resource private RedisTemplate redisTemplate; /** * 批量删除 * @param keys */ public void remove(String... keys) { for(String key :keys){ remove(key); } } /** * 批量删除正则匹配到的 * @param pattern */ public void removePattern(String pattern) { Set keys = redisTemplate.keys(pattern); assert keys != null; if (keys.size() > 0){ redisTemplate.delete(keys); } } /** * 删除 * @param key */ public void remove(String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否存在 * @param key * @return boolean */ public boolean exists(String key) { return StringUtils.isBlank(key) ? false : redisTemplate.hasKey(key); } /** * 读取缓存 * @param key * @return */ public Object get(String key) { Object result = null; ValueOperations operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 写入缓存 * @param key * @param value * @return */ public boolean set(String key, Object value) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存并加上过期时间(秒) * @param key * @param value * @param expireTimeSeconds * @return */ public boolean set(String key, Object value, Long expireTimeSeconds) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入过期时间(秒) * @param key * @param expireTimeSeconds * @return */ public boolean expire(String key, Long expireTimeSeconds) { boolean result = false; try { redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /* **************************** 针对list操作的方法 **************************** */ /** * 在key对应list的尾部添加 * @param key * @param value * @return */ public long rightPushForList(String key, Object value) { return redisTemplate.opsForList().rightPush(key, value); } /** * 在key对应list的头部添加 * @param key * @param value * @return */ public long leftPushForList(String key, Object value) { return redisTemplate.opsForList().leftPush(key, value); } /** * key对应list的长度 * @param key * @return */ public long listSize(String key) { return redisTemplate.opsForList().size(key); } /** * 获取list集合 * @param Key * @param begin * @param end * @return */ public List getList(String Key, int begin, int end) { return redisTemplate.opsForList().range(Key, begin, end); } /** * 在key对应list的尾部移除 * @param key * @return */ public Object rightPopForList(String key) { return redisTemplate.opsForList().rightPop(key); } /** * 在key对应list的头部移除 * @param key * @return */ public Object leftPopForList(String key) { return redisTemplate.opsForList().leftPop(key); } /** * 移除list的index位置上的值 * @param Key * @param index * @param value */ public void removeList(String Key, long index, Object value) { redisTemplate.opsForList().remove(Key, index, value); } /** * 重置list的index位置上的值 * @param Key * @param index * @param value */ public void setList(String Key, long index, Object value) { redisTemplate.opsForList().set(Key, index, value); } /** * 写入list * @param key * @param list */ public void setList(String key, List list) { if(list!=null&&list.size()>0){ for (Object object : list) { rightPushForList(key,object); } } } /** * 写入map * @param key * @param map */ public void setMap(String key, Map map) { redisTemplate.opsForHash().putAll(key, map); } /** * 获取map * @param key * @param mapKey * @return */ public Object get(String key, String mapKey) { return redisTemplate.opsForHash().get(key, mapKey); } /** * 写入map * @param key * @param hashKey * @param hashValue */ public void setMapByKV(String key, Object hashKey, Object hashValue) { redisTemplate.opsForHash().put(key, hashKey,hashValue); } /** * 删除map中的某个key-value * @param key * @param hashKey */ public void removeHash(String key, String hashKey) { redisTemplate.opsForHash().delete(key, hashKey); } /** * * @param key * @return */ public Map getEntries(String key) { return redisTemplate.opsForHash().entries(key); } /** * * @param key * @param step * @return */ public long increase(String key, long step) { return redisTemplate.opsForValue().increment(key, step); } /** * 获取失效时间 * @param key * @return */ public long getExpireTime(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } }