1. 概述
Redis【入门】就这一篇!
https://www.jianshu.com/p/56999f2b8e3b
redis 在windows 上启动等常用操作
https://blog.csdn.net/luuvyjune/article/details/81016295
基于token的登录管理(多设备登录、单设备登录)
https://blog.csdn.net/xxssyyyyssxx/article/details/83743739
2. Redis配置文件
redis.properties
redis.host=127.0.0.1
redis.port=6379
#redis.password=123456
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000
spring-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 引入配置文件 -->
<bean id="redisPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redis.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<!-- <property name="password" value="${redis.password}" /> -->
<property name="timeout" value="${redis.timeout}"></property>
<property name="database" value="2"/>
</bean>
<!-- 复杂对象处理 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!--存储string时key需要的序列化配置-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<!--存储Map时key需要的序列化配置 (set hash zset list) -->
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--存储Map时value需要的序列化配置-->
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
<!-- 字符串处理 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<!--存储Map时key需要的序列化配置 (set hash zset list) -->
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--存储Map时value需要的序列化配置-->
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<property name="stringSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
<bean id="redisUtil" class="com.zhiruan.util.redis.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
<bean id="stringRedisUtil" class="com.zhiruan.util.redis.StringRedisUtil">
<property name="stringRedisTemplate" ref="stringRedisTemplate" />
</bean>
</beans>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml,classpath:spring-redis.xml</param-value>
</context-param>
3.springboot结合redis
IDEA搭建SpringBoot+Mybatis+Redis
SpringBoot+MySQL+MyBatis+druid+redis整合 实现功能:初始化商品列表从数据库读取加载至redis中
如何spingboot中简单的使用redis缓存存取和查询数据
Docker 安装 Redis + Spring Boot 整合 Redis
Spring Boot 整合 Redis 实现缓存操作
springboot整合redis缓存的增删改查
SpringBoot学习——springboot整合Redis实现数据缓存
4. 如何spingboot中简单的使用redis缓存存取和查询数据
1.在pom文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在application.yml文件中添加redis的配置
3.在service层中注入redisTemplate
没加缓存时候的代码:
/**
* 根据ID查询实体
* @param id
* @return
*/
public Article findById(String id) {
return articleDao.findById(id).get();
}
加了缓存之后的代码:
/**
* 根据ID查询实体
* @param id
* @return
*/
public Article findById(String id) {
//先从缓存中查询当前对象
Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
//如果没有取到,就从数据库从查询,然后存入缓存中
if(article == null){
article = articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_"+id,article);
}
return article;
}
第一次查询在控制台会打印出相应的sql语句,第二次查询就不会打印了,这就说明第二次没有访问数据库而是访问的缓存
4.在修改和删除的方法中要添加上删除redis中数据的方法
/**
* 修改
* @param article
*/
public void update(Article article) {
redisTemplate.delete("article_"+ article.getId());
articleDao.save(article);
}
/**
* 删除
* @param id
*/
public void deleteById(String id) {
redisTemplate.delete("article_"+ id);
articleDao.deleteById(id);
}
5. SpringBoot整合Redis并使用Spring缓存注解进行缓存操作
https://blog.csdn.net/justry_deng/article/details/89285317
SpringBoot使用Spring缓存注解
https://blog.csdn.net/justry_deng/article/details/89283664
springboot+redis实现缓存数据