本次和大家分享的是在springboot集成使用redis,这里使用的是redis的jedis客户端,如下添加依赖

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

然后需要redis的相关配置(这里我的redis密码是空)

spring:redis:single: 192.168.146.28:6378jedis:pool:max-idle: 8max-active: 8max-wait: 3000timeout: 3000password:

这是redis的一般配置,具体调优可以设置这些参数,下面在JedisConfig类中读取这些设置

    @Value("${spring.redis.single}")private String strSingleNode;@Value("${spring.redis.jedis.pool.max-idle}")private Integer maxIdle;@Value("${spring.redis.jedis.pool.max-active}")private Integer maxActive;@Value("${spring.redis.jedis.pool.max-wait}")private Integer maxAWait;@Value("${spring.redis.timeout}")private Integer timeout;@Value("${spring.redis.password}")private String password;

有上面的配置,就需要有代码里面设置下,这里创建一个返回JedisPoolConfig的方法

    /*** jedis配置** @return*/public JedisPoolConfig getJedisPoolConfig() {JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(maxIdle); #最大空闲数config.setMaxWaitMillis(maxAWait); #最大等待时间config.setMaxTotal(maxActive); #最大连接数return config;}

有了配置,接下来就创建JedisPool,这里把JedisPool托管到spring中

     /*** 获取jedispool** @return*/@Beanpublic JedisPool getJedisPool() {JedisPoolConfig config = getJedisPoolConfig();System.out.println("strSingleNode:" + this.strSingleNode);String[] nodeArr = this.strSingleNode.split(":");JedisPool jedisPool = null;if (this.password.isEmpty()) {jedisPool = new JedisPool(config,nodeArr[0],Integer.valueOf(nodeArr[1]),this.timeout);} else {jedisPool = new JedisPool(config,nodeArr[0],Integer.valueOf(nodeArr[1]),this.timeout,this.password);}return jedisPool;}

上面简单区分了无密码的情况,到此jedis的配置和连接池就基本搭建完了,下面就是封装使用的方法,这里以set和get为例;首先创建个JedisComponent组件,代码如下

/*** Created by Administrator on 2018/8/18.*/
@Component
public class JedisComponent {@AutowiredJedisPool jedisPool;public boolean set(String key, String val) {Jedis jedis = null;try {jedis = jedisPool.getResource();return jedis.set(key, val).equalsIgnoreCase("OK");} finally {if (jedis != null) {jedis.close();}}}public <T> boolean set(String key, T t) {String strJson = JacksonConvert.serilize(t);if (strJson.isEmpty()) {return false;}return this.set(key, strJson);}public String get(String key) {Jedis jedis = null;try {jedis = jedisPool.getResource();return jedis.get(key);} finally {if (jedis != null) {jedis.close();}}}public <T> T get(String key, Class<T> tClass) {String strJson = this.get(key);return JacksonConvert.deserilize(strJson, tClass);}
}

有了对jedis的调用封装,我们在Controller层的测试用例如下

    @AutowiredJedisComponent jedis;@GetMapping("/setJedis/{val}")public boolean setJedis(@PathVariable String val) {return jedis.set("token", val);}@GetMapping("/getJedis")public String getJedis() {return jedis.get("token");}

运行set和get的接口效果如

springboot + redis(单机版)相关推荐

  1. Java项目:在线淘房系统(租房、购房)(java+SpringBoot+Redis+MySQL+Vue+SpringSecurity+JWT+ElasticSearch+WebSocket)

    源码获取:博客首页 "资源" 里下载! 该系统有三个角色,分别是:普通用户.房屋中介.管理员.普通用户的功能:浏览房屋信息.预约看房.和中介聊天.申请成为中介等等.房屋中介的功能: ...

  2. Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:wangzaiplus www.jianshu.com/p/ ...

  3. springboot + redis + 注解 + 拦截器 实现接口幂等性校验

    点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 来源:https://www.jianshu.com/p/6189275403ed 一.概念 ...

  4. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能...

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  5. redis 判断存在性_实战 | springboot+redis+拦截器 实现接口幂等性校验

    来源:https://www.jianshu.com/p/6189275403ed 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如: 订单接口, 不能 ...

  6. springboot redis token_Spring Boot + Redis + 注解 + 拦截器来实现接口幂等性校验

    优质文章,及时送达 作者 | wangzaiplus 链接 | www.jianshu.com/p/6189275403ed 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证 ...

  7. C#session共享+redis_技术干货分享:基于SpringBoot+Redis的Session共享与单点登录

    categories: 架构 author: mrzhou tags: SpringBoot redis session 单点登录 基于SpringBoot+Redis的Session共享与单点登录 ...

  8. 补习系列(14)-springboot redis 整合-数据读写

    目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...

  9. (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版

    http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...

  10. SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut

    SpringBoot Redis缓存 @Cacheable.@CacheEvict.@CachePut

最新文章

  1. lora和nbiot的相同点,它们之间有何区别和联系?
  2. 第十六届全国大学智能车全国总决赛竞赛闭幕式
  3. android g920p rom,三星SM-G920P(S6 美国Sprint定制版)一键救砖教程,轻松刷回官方系统...
  4. ELK错误1_Kafka-Logstash-Elasticsearch过程,Elasticsearch报grokparsefailure错误
  5. 各种拿webshell
  6. spark学习:ContextCleaner清理器
  7. hihocoder-Week243-hiho字符串
  8. eclipse 安装反编译软件jadclipse
  9. 图的最短路算法(Dijkstra和Floyd-Warshall)
  10. Android UI 之WaterFall瀑布流效果 [复制链接]
  11. FL Studio20.8.2(水果win10)中文版主要软件更新内容
  12. 【POJ2155】Matrix(二维区间修改+单点查询---二维树状数组)
  13. xxnet 360浏览器设置
  14. Qt 利用海康摄像头的ISAPI协议进行抓图等操作
  15. 管理型工业以太网交换机什么
  16. 富途、小牛与亿航股价齐飞, “新三傻”是大泡沫还是好未来?
  17. 3ds运行linux,3ds自制操作软件
  18. 颜体html标签,颜体楷书笔法32式详解,一定不能错过!(超级干货)
  19. 阿里巴巴离职DBA 35岁总结的职业生涯(转)
  20. 电梯控制系统的设计与实现

热门文章

  1. Jquery ThickBox的使用
  2. Jquery中选择器
  3. 群体智能优化算法之蚁群优化算法(ACO)
  4. 群体智能之粒子群优化(PSO)
  5. 【版本控制】分布式的版本控制系统GitHub学习资源汇总
  6. 智能优化算法:头脑风暴优化算法-附代码
  7. 从零基础入门Tensorflow2.0 ----七、33 数据padding,模型构建,训练
  8. 《剑指offer》面试题33、32——把数组排成最小的数、整数中1出现的次数
  9. 线索化二叉树的创建与遍历
  10. Mybatis-03-配置文件及Mybatis主要API详解