spring-boot redis配置
来源:互联网 发布:ios阴阳师网络连接错误 编辑:程序博客网 时间:2024/05/18 14:27
1.在pom.xml中引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.3.8.RELEASE</version></dependency>
2.在application.yml中配置参数
redis: host: 127.0.0.1 port: 6379 max-idle: 5 max-total: 10 max-wait-millis: 3000
3.新建一个 参数类,来 引入配置文件中的 参数,便于集中管理
@Component@Datapublic class Parameters {// redis config start @Value("${redis.host}") private String redisHost; @Value("${redis.port}") private int redisPort; @Value("${redis.max-idle}") private int redisMaxIdle; @Value("${redis.max-total}") private int redisMaxTotal; @Value("${redis.max-wait-millis}") private int redisMaxWaitMillis; //redis config end}
4.新建一个类,初始化链接池,提供 获取连接池的方法
@Component@Slf4jpublic class JedisPoolWrapper { private JedisPool jedisPool=null; @Autowired private Parameters parameters; //注入redis的变量 //初始化redisWrapper (PostConstruct注解相当于静态代码库,方法会在类初始化的时候 进行执行) @PostConstruct public void init() throws RedisException { try { JedisPoolConfig config=new JedisPoolConfig(); config.setMaxIdle(parameters.getRedisMaxIdle()); config.setMaxTotal(parameters.getRedisMaxTotal()); config.setMaxWaitMillis(parameters.getRedisMaxWaitMillis()); jedisPool=new JedisPool(config,parameters.getRedisHost(),parameters.getRedisPort(),2000); } catch (Exception e) { log.error("fail to initialize redis pool",e); //记录日志 throw new RedisException("初始化redisPool失败"); //抛出异常 } } public JedisPool getJedisPool() { return jedisPool; }}
5.配置一些操纵redis的方法
package com.sean.mamabike.cache;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;/** *@Author sean *@Date 2017/10/9 11:59 *@Description 控制redis进行 CRUD 操作的工具 */@Component@Slf4jpublic class CommonCacheUtils { @Autowired private JedisPoolWrapper jedisPoolWrapper; //1.缓存一个key(永久) public void cache(String key,String value){ try { JedisPool jedisPool=jedisPoolWrapper.getJedisPool(); if (jedisPool != null){ try(Jedis jedis=jedisPool.getResource()){ jedis.select(0); //选择第0号 片区 存储 jedis.set(key,value); } } } catch (Exception e) { log.error("fail to cache key and value",e); } } //2.获取redis中的缓存 public String getCacheByKey(String key){ String value=null; try { JedisPool jedisPool=jedisPoolWrapper.getJedisPool(); //获取链接池 if (jedisPool != null){ try(Jedis jedis=jedisPool.getResource()){ jedis.select(0); value= jedis.get(key); } } } catch (Exception e) { log.error("fail to get cache",e); } return value; } //3.设置一个有缓存时间的key SETNX:当缓存中不存在时,设置成功,返回1;已经存在,返回0 public long cacheNxExpire(String key,String value,int expiry){ long result=0; try { JedisPool jedisPool=jedisPoolWrapper.getJedisPool(); if (jedisPool != null){ try(Jedis jedis=jedisPool.getResource()){ jedis.select(0); result=jedis.setnx(key,value); jedis.expire(key,expiry); } } } catch (Exception e) { log.error("fail to cacheNxExpire",e); } return result; } //4.删除缓存 public void deleteCache(String key){ try { JedisPool jedisPool=jedisPoolWrapper.getJedisPool(); if (jedisPool != null){ try(Jedis jedis=jedisPool.getResource()){ jedis.del(key); } } } catch (Exception e) { log.error("fail to delete cache",e); } }}
阅读全文
0 0
- spring-boot redis 配置
- spring-boot redis配置
- spring boot redis配置
- Spring boot 手动配置redis
- spring-boot配置redis cluster
- Spring Boot Redis 集成配置
- spring boot 注解配置redis
- spring boot 集成redis 配置
- spring-boot-data-redis线程池配置
- spring-boot-starter-redis配置详解
- spring boot 笔记(三):配置redis
- Spring Boot中redis的配置实例
- spring boot下redis的配置使用
- Spring Boot集成Redis java实现配置
- Spring Boot集成Redis实现自动配置
- spring Boot redis连接池配置,Spring Session配置
- spring Boot redis连接池配置,Spring Session配置
- spring boot使用redis
- JavaScript动态加载瀑布流
- Qt 学习之路 2(91):粒子系统(续)
- 将博客搬至博客园
- layer 提示层问题 按钮提示默认执行
- [日推荐]『51好书推荐』专治假期综合症~
- spring-boot redis配置
- C++ 模板详解(一)
- 通用代码 js获取URL参数
- C&C服务器
- leetcode 41 First Missing Positive
- 安装win32com
- 关于Eclipse中“ctrl+鼠标左击类”快捷键不能查看该类API源代码的解决方法
- redis info 命令查看redis使用情况
- oracle数据库常用函数:replace translate函数使用