强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan​

【前言】

最近自己在整理过去搭建过的框架,将用到的各个组件进行了梳理并融入自己新建的项目中(https://github.com/dangnianchuntian/springboot),一是对过去项目的整理;二是在整理的过程中查漏补缺;三是以后可以拿过去就用;

【整合Redis实战】

         一、Pom中引入相应的Jar包

<!-- redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version>
</dependency>

         二、重要代码展示

1、读取配置文件中配置连接Redis属性值

package com.zhanghan.zhboot.properties;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@Data
@ConfigurationProperties
public class RedisProperties {@Value("${spring.redis.database}")private int database;@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private int port;@Value("${spring.redis.password}")private String password;@Value("${spring.redis.pool.max-idle}")private int maxIdle;@Value("${spring.redis.pool.min-idle}")private int minIdle;@Value("${spring.redis.pool.max-active}")private int maxActive;@Value("${spring.redis.pool.max-wait}")private int maxWait;@Value("${spring.redis.timeout}")private int timeout;}

2、创建相应的Redis操作template

package com.zhanghan.zhboot.config;import com.zhanghan.zhboot.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {@Autowiredprivate RedisProperties redisProperties;@Beanpublic JedisConnectionFactory jedisConnectionFactory() {RedisStandaloneConfiguration rf = new RedisStandaloneConfiguration();rf.setDatabase(redisProperties.getDatabase());rf.setHostName(redisProperties.getHost());rf.setPort(redisProperties.getPort());rf.setPassword(RedisPassword.of(redisProperties.getPassword()));JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb =(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());jedisPoolConfig.setEvictorShutdownTimeoutMillis(redisProperties.getTimeout());jpb.poolConfig(jedisPoolConfig);JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(rf, jpb.build());return jedisConnectionFactory;}@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}@BeanRedisTemplate<Object, Object> redisTemplate() {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(jedisConnectionFactory());return redisTemplate;}@BeanRedisTemplate<String, String> strRedisTemplate() {final RedisTemplate<String, String> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericToStringSerializer<>(String.class));template.setValueSerializer(new GenericToStringSerializer<>(String.class));return template;}@BeanRedisTemplate<String, Long> longRedisTemplate() {final RedisTemplate<String, Long> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericToStringSerializer<>(Long.class));template.setValueSerializer(new GenericToStringSerializer<>(Long.class));return template;}@BeanRedisTemplate<String, Boolean> booleanRedisTemplate() {final RedisTemplate<String, Boolean> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericToStringSerializer<>(Boolean.class));template.setValueSerializer(new GenericToStringSerializer<>(Boolean.class));return template;}
}

3、使用相应的RedisTemplate演示

package com.zhanghan.zhboot.controller;import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
public class RedisController {@Autowiredprivate RedisTemplate<String, String> strRedisTemplate;@Autowiredprivate RedisTemplate<String, Long> longRedisTemplate;@Autowiredprivate RedisTemplate<String, Boolean> booleanRedisTemplate;@RequestMapping(value = "/get/redis", method = RequestMethod.GET)public Map xmlAnalysis() {String strRedisKey = "zh:boot:String";String longRedisKey = "zh:boot:long";String booleanRedisKey = "zh:boot:bollean";String strRedisValue = strRedisTemplate.opsForValue().get(strRedisKey);if (StringUtils.isNullOrEmpty(strRedisValue)) {strRedisTemplate.opsForValue().set(strRedisKey, "张晗");}Long longRedisValue = longRedisTemplate.opsForValue().get(longRedisKey);if (ObjectUtils.isEmpty(longRedisValue)) {longRedisTemplate.opsForValue().set(longRedisKey, 1L);}Boolean booleanRedisValue = booleanRedisTemplate.opsForValue().get(booleanRedisKey);if (ObjectUtils.isEmpty(booleanRedisValue)) {booleanRedisTemplate.opsForValue().set(booleanRedisKey, true);}strRedisValue = strRedisTemplate.opsForValue().get(strRedisKey);longRedisValue = longRedisTemplate.opsForValue().get(longRedisKey);booleanRedisValue = booleanRedisTemplate.opsForValue().get(booleanRedisKey);Map result = new HashMap();result.put(strRedisKey, strRedisValue);result.put(longRedisKey, longRedisValue);result.put(booleanRedisKey, booleanRedisValue);return result;}}

         三、效果展示

四、项目地址及代码版本

1、地址:https://github.com/dangnianchuntian/springboot

2、代码版本:1.0.0-Release

【总结】

1、总结,提炼,查漏补缺;

2、要有前瞻性。

SpringBoot实战(四):SpringBoot整合Redis相关推荐

  1. redis序列化_实例讲解Springboot以Template方式整合Redis及序列化问题

    1 简介 之前讲过如何通过Docker安装Redis,也讲了Springboot以Repository方式整合Redis,建议阅读后再看本文效果更佳: (1) Docker安装Redis并介绍漂亮的可 ...

  2. SpringBoot实战(十七):Redis Pipeline 轻松实现百倍性能提升(续)

    前言 最近在做业务的时候,需要批量操作Redis,虽然Redis的速度非常快,但是for循环操作Redis还是会有问题,在之前的基础上又对批量操作Redis进行了汇总: 批量操作Redis: 批量Se ...

  3. SpringBoot实战(六):Redis Pipeline 轻松实现百倍性能提升

    强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 今天在优化通知平台的路由部分时发现每次当路由初始化到Redis时异常慢,早就听闻Pip ...

  4. mysql springboot 缓存_Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 一.缓 ...

  5. 继承redis spring_实例讲解Springboot以Repository方式整合Redis

    1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...

  6. SpringBoot 实战 (十二) | 整合 thymeleaf

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 Thymeleaf ,并整合 Thymeleaf 开发一个简陋版的学生信息管理系统. Spring ...

  7. SpringBoot(四)-- 整合Servlet、Filter、Listener

    SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...

  8. SpringBoot(四)整合视图

    一.SpringBoot整合Thymeleaf模板 首先在pom.xml中添加对Thymeleaf的相关依赖: <!--thymeleaf--> <dependency>< ...

  9. springboot获取sessionid_Spring Boot 整合Redis, 用起来真简单!

    点击上方"Java技术前线",选择"置顶或者星标" 与你一起成长- 作者:java_老男孩  https://blog.51cto.com/14230003/2 ...

  10. SpringBoot实战(四)之使用JDBC和Spring访问数据库

    这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识 H2数据库是一个开源的关系型数据库. H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提 ...

最新文章

  1. Cocos2d-x学习笔记(三十)之 游戏存档
  2. finereport 登录界面的代码文件_【干货下载】多彩包含网页登录界面等4款WEB模板素材作品集源文件...
  3. C#复制文件到指定文件夹
  4. oracle自增自删分区的脚本,oracle实现自增方法(错误ora-04098解决)
  5. docker lnmp php
  6. MoCo不适用于目标检测?MSRA提出对象级对比学习的目标检测预训练方法SoCo!性能SOTA!(NeurIPS 2021)...
  7. linux mysql 磁盘_Linux运维知识之为Linux MySQL数据库设置磁盘限额
  8. Keil(MDK-ARM-STM32)系列教程(七)菜单
  9. 计算字符串的相似度(编辑距离)
  10. 一次Mysql 死锁事故
  11. 威伦触摸屏入门布局提升题
  12. MySQL 计算年龄
  13. 任天堂如何通过旧技术赢得胜利
  14. Diy-Scratch(4) 大家来找茬
  15. 计算机视觉与机械专业相关吗,计算机视觉在早期森林火灾探测中的应用研究-精密仪器及机械专业论文.docx...
  16. 灰色关联分析法详细步骤解释
  17. 钢丝流-BISU的战斗哲学
  18. 计算机科学家事迹,【CCF会员故事】计算机软件科学家谢涛:星辰大海,求思进取...
  19. L1、L2正则VS L1、L2 loss
  20. 邮箱smtp服务器及端口收集

热门文章

  1. 智能社官网顶部导航实现demo
  2. 主机映射(域名与ip之间映射)
  3. 酷睿i5 12600HX怎么样 相当于什么水平
  4. QX5299人体感应太阳能LED灯控制器
  5. 天津科技大学计算机组成原理答案,2017年天津科技大学842自命题计算机学科专业基础综合[专硕]之计算机组成原理考研强化模拟题...
  6. 7-194 循环结构 —— 中国古代著名算题。趣味题目:物不知其数。
  7. python历史时间轴可视化_如何使用Python创建历史时间轴
  8. 非常全面的CSDN-markdown编辑器!!!!!!
  9. projece修改工期_工期设定(Project)
  10. layui 使用laydate动态创建多个时间选择框