1.创建一个maven工程redis-cache
2.pom文件的依赖
<!-- junit5运行所需jar包 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
3. spring-redis-cache.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd ">
<context:component-scan base-package="com.szcatic"/>
<!-- 启用缓存注解功能 -->
<cache:annotation-driven cache-manager="redisCacheManager"/>
<!-- 对缓存管理器的统一管理 -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
factory-method="create" c:connection-factory-ref="jedisConnectionFactory" />
<!-- 配置Cluster -->
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="3"></property>
<!-- 节点配置 -->
<property name="clusterNodes">
<set>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1 "></constructor-arg>
<constructor-arg name="port" value="7000"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1 "></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1 "></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="100" />
<property name="maxTotal" value="600" />
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<!-- redis 访问的模版 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<!-- 添加如下序列化配置解决key乱码问题以及令keys()方法生效 -->
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
</bean>
</beans>
4.UserDao查询数据接口
package com.szcatic.dao;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
/**
* 注解@Cacheable声明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。
* 如果这个值能够找到,就会返回存储的值,
* 否则的话,这个方法就会被调用,返回值会放在缓存之中。
* @param userId
* @return
*/
@Cacheable(value = "userCache", key = "1000")
public String getUserName(String userName) {
System.out.println("查询用户名");
return userName;
}
/**
* 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,
* 和 @Cacheable 不同的是,它每次都会触发真实方法的调用
* @param userName
*/
@CachePut(value = "userCache", key = "1000")
public String updateUserName(String userName) {
System.out.println("更新用户名并添加到缓存");
return userName;
}
/**
* 是否清空所有缓存内容,缺省为 false,
* 如果指定为 true,则方法调用后将立即清空所有缓存
*/
@CacheEvict(value="userCache", allEntries=true)
public void deleteUserName() {
System.out.println("删除数据并清空缓存");
}
}
5.测试类
package com.szcatic.test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.szcatic.dao.UserDao;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring-redis-cache.xml")
public class CacheTest {
@Autowired
private UserDao userDao;
@Test
void testGetUserName() {
String userName = userDao.getUserName("zhangsan");
System.out.println(userName);
}
@Test
void testUpdateUserName() {
String userName = userDao.updateUserName("lisi");
System.out.println(userName);
}
@Test
void testDeleteUserName() {
userDao.deleteUserName();
}
@Test
void testAll() {
testDeleteUserName();
testGetUserName();
testUpdateUserName();
testGetUserName();
testDeleteUserName();
testGetUserName();
}
}
6.testAll测试方法运行结果:
删除数据并清空缓存
查询用户名
zhangsan
更新用户名并添加到缓存
lisi
lisi
删除数据并清空缓存
查询用户名
zhangsan