123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- 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<Serializable, Object> redisTemplate;
- /**
- * 批量删除
- * @param keys
- */
- public void remove(String... keys) {
- for(String key :keys){
- remove(key);
- }
- }
- /**
- * 批量删除正则匹配到的
- * @param pattern
- */
- public void removePattern(String pattern) {
- Set<Serializable> 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<Serializable, Object> 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<Serializable, Object> 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<Serializable, Object> 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<String, Object> 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<Object, Object> 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);
- }
- }
|