Java对Redis的简单封装
相关文章:
Linux安装Redis
SpringBoot连接Redis
Redis常见问题
Redis实现主从复制
封装说明:
项目中对于Redis的简单封装,仅供参考学习使用,不完全保证生产环境中不会出现问题
方法汇总:
- 存储对象,不设置过期时间
- 存储对象,设置过期时间
- 获取对象
- 判断Key是否存在
- 值自增
- 值自减
- Redis 充当分布式锁
- 释放锁
- Redis存放Hash
- 获取hash值
- 刷新过期时间
完整代码如下:
package com.xcx.cache.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* @Auther: chenlong
* @Date: 2018/9/16 14:22
* @Description:封装Redis
*/
@Service
public class RedisCache {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
//日志
private