SringCloud集成Redis工具类

1、pom文件

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、redis工具类

@Service(value = "redisService")
@Slf4j
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 批量删除对应的value
     *
     * @param keys key数组
     */
    public void removeBatchKey(final String... keys) {
        for (String key : keys) {
            removeByKey(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern string
     */
    @SuppressWarnings("unchecked")
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (!CollectionUtils.isEmpty(keys)) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除key
     *
     * @param key string
     */
    @SuppressWarnings("unchecked")
    public void removeByKey(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param keys keys
     * @return boolean false or true
     */
    @SuppressWarnings("unchecked")
    public boolean existsBatchKey(final String... keys) {
        for (String key : keys) {
            if (!redisTemplate.hasKey(key)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key key
     * @return boolean false or true
     */
    @SuppressWarnings("unchecked")
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key string
     * @return object
     */
    @SuppressWarnings("unchecked")
    public Object get(final String key) {
        Object result;
        ValueOperations<Serializable, Object> operations = redisTemplate
                .opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存,长久有效
     *
     * @param key   string
     * @param value object
     * @return boolean false or true
     */
    @SuppressWarnings("unchecked")
    public boolean setNoExpire(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate
                    .opsForValue();
            operations.set(key, value);
            result = true;
        } catch (DataAccessException e) {
            log.error("Write RedisCache failure:" + e);
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key        string
     * @param value      object
     * @param expireTime 过期时间
     * @return boolean false or true
     */
    @SuppressWarnings("unchecked")
    public boolean setTakeOverTime(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate
                    .opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (DataAccessException e) {
            log.error("Write RedisCache failure:" + e);
        }
        return result;
    }


    /**
     * 向hash表写入数据
     *
     * @param key   键名,即hash表名
     * @param field 字段名
     * @param value 值
     */
    @SuppressWarnings("unchecked")
    public void setKeyValue(String key, Object field, Object value) {
        if (StringUtils.isEmpty(key) || null == field || null == value) {
            log.debug("Read RedisCache failure: Parameter cannot Empty");
        }
        redisTemplate.opsForHash().put(key, field, value);
    }

    /**
     * 向hash表写入数据
     *
     * @param key 键名,即hash表名
     */
    @SuppressWarnings("unchecked")
    public long getHashKeylen(String key) {
        if (StringUtils.isEmpty(key)) {
            log.debug("Read RedisCache failure: Parameter cannot Empty");
        }
        return redisTemplate.opsForHash().size(key);
    }


    /**
     * 获取为哈希表 key 中的域
     *
     * @param key   键名,即hash表名
     * @param field 字段名
     */
    @SuppressWarnings("unchecked")
    public Object get(String key, Object field) {
        if (StringUtils.isEmpty(key) || null == field) {
            log.debug("Read RedisCache failure: Parameter cannot Empty");
        }
        return redisTemplate.opsForHash().get(key, field);
    }

    /**
     * 删除为哈希表 key
     *
     * @param key   键名,即hash表名
     * @param field 字段名
     */
    @SuppressWarnings("unchecked")
    public Object deleteHash(String key, String field) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
            log.debug("Read RedisCache failure: Parameter cannot Empty");
        }
        return redisTemplate.opsForHash().delete(key, field);
    }


    /**
     * 判断hash中是否有相应字段
     *
     * @param key   键名,即hash表名
     * @param field 字段名
     * @return 存在返回true, 否则返回false
     */
    @SuppressWarnings("unchecked")
    public boolean existKeyValue(String key, String field) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
            return false;
        } else {
            HashOperations<String, String, Object> operations = redisTemplate.opsForHash();
            //redis中存储的sessionId前缀spring:session:sessions:
            return operations.hasKey("spring:session:sessions:" + key, field);
        }
    }

    @SuppressWarnings("unchecked")
    public void setExpireTime(Object key, Long timeout, TimeUnit timeUnit) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }


    /**
     * 为哈希表 key 中的域 field 的值加上增量 increment
     *
     * @param key   键名,即hash表名
     * @param value 值
     */
    @SuppressWarnings("unchecked")
    public long increment(String key, long value) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
            log.debug("Read RedisCache failure: Parameter cannot Empty");
        }
        return redisTemplate.opsForValue().increment(key, value);
    }


}

你可能感兴趣的:(java,redis,redis,java,spring)