SpringBoot学习笔记(8)-整合Redis

由于版本原因,SpringBoot2.0整合Redis和低版本的SpringBoot不太一样,经测试,本文这套整合方案可以使用。

更多关于SpringBoot的总结请点击:SpringBoot使用总结


一、build.gradle

//redis clienet
	compile("redis.clients:jedis:2.9.0")
	//commons pool
	compile("org.apache.commons:commons-pool2:2.6.0")
	//redis starter
	compile("org.springframework.boot:spring-boot-starter-redis:2.0.4.RELEASE")
	//redis data
	compile("org.springframework.data:spring-data-redis:2.0.5.RELEASE")

二、application.properties

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000

注意,如果使用的SpringBoot版本是1.5,那么spring.redis.jedis.pool.max-idl写成spring.redis.pool.max-idl


三、Controller

 @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping(value="/redis")
    @ResponseBody
    public String redis(){
        System.out.println("hello");
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String hello = ops.get("hello");
        ops.set("redisTest","hello Redis",10000);
        return hello;
    }

经测试,以上配置全部可用

常用工具类


@Slf4j
@Component
public class RedisUtil {
   
   		 @Autowired
        private StringRedisTemplate redisTemplate;

        /**
         * 写入redis缓存(不设置expire存活时间)
         * @param key
         * @param value
         * @return
         */
        public boolean set(final String key, String value){
            boolean result = false;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                operations.set(key, value);
                result = true;
            } catch (Exception e) {
                log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
            }
            return result;
        }

        /**
         * 写入redis缓存(设置expire存活时间)
         * @param key
         * @param value
         * @param expire 毫秒为单位
         * @return
         */
        public boolean set(final String key, String value, Long expire){
            boolean result = false;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                operations.set(key, value);
                redisTemplate.expire(key, expire, TimeUnit.MILLISECONDS);
                result = true;
            } catch (Exception e) {
                log.error("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage());
            }
            return result;
        }


        /**
         * 读取redis缓存
         * @param key
         * @return
         */
        public Object get(final String key){
            Object result = null;
            try {
                ValueOperations operations = redisTemplate.opsForValue();
                result = operations.get(key);
            } catch (Exception e) {
                log.error("读取redis缓存失败!错误信息为:" + e.getMessage());
            }
            return result;
        }

        /**
         * 判断redis缓存中是否有对应的key
         * @param key
         * @return
         */
        public boolean exists(final String key){
            boolean result = false;
            try {
                result = redisTemplate.hasKey(key);
            } catch (Exception e) {
                log.error("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage());
            }
            return result;
        }

        /**
         * redis根据key删除对应的value
         * @param key
         * @return
         */
        public boolean remove(final String key){
            boolean result = false;
            try {
                if(exists(key)){
                    redisTemplate.delete(key);
                }
                result = true;
            } catch (Exception e) {
                log.error("redis根据key删除对应的value失败!错误信息为:" + e.getMessage());
            }
            return result;
        }

        /**
         * redis根据keys批量删除对应的value
         * @param keys
         * @return
         */
        public void remove(final List<String> keys){
            for(String key : keys){
                remove(key);
            }
        }


    /**
     * hash类型写入redis缓存(针对需要持久保存的对象)
     * @param key
     * @param value
     * @return
     */
    public boolean hset(String hkey,String key, String value){
        boolean result = false;
        try {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            hashOperations.put(hkey,key,value);
            result = true;
        } catch (Exception e) {
            log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
        }
        return result;
    }

    /**
     * 获取hash数据
     * @param key
     * @param value
     * @return
     */
    public Object hget(String hkey,String key){
        Object result = null;
        try {
            HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
            Boolean aBoolean = hashOperations.hasKey(hkey, key);
            if (!aBoolean) {
                return null;
            }
            Object response = hashOperations.get(hkey, key);
            result = response;
        } catch (Exception e) {
            log.error("写入redis缓存失败!错误信息为:" + e.getMessage());
        }
        return result;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员荣荣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值