8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存

时间:2024-01-18 12:51:20

      8.5.3 使用@CacheEvict清除缓存

        被@CacheEvict注解修饰的方法可用于清除缓存,使用@CacheEvict注解时可指定如下属性:

          ⊙ value : 必须属性。用于指定该方法用于清除哪个缓存区的数据。

          ⊙ allEntries : 该属性指定是否清空整个缓存区。

          ⊙ beforeInvocation : 该属性指定是否在执行方法之前清除缓存。默认是在方法成功完成之后才清除缓存。

          ⊙ condition : 该属性指定一个SpEL表达式,只有当该表达式为true时才清除缓存。

          ⊙ key : 通过SpEL表达式显示指定缓存的key。多个参数组合的key 用#name + #age + ...

          Class : UserServiceImpl

package edu.pri.lime._8_5_3.cacheevict.service.impl;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import edu.pri.lime._8_5_3.cacheevict.bean.User;
import edu.pri.lime._8_5_3.cacheevict.service.UserService; @Service
@Cacheable(value="users")
public class UserServiceImpl implements UserService{ @Override
public User getUserByNameAndAge(String name, int age) {
System.out.println("----------正在执行getUserByNameAndAge()查询方法----------");
return new User(name,age);
} @Override
public User getAnotherUser(String name, int age) {
System.out.println("----------正在执行getAnotherUser()查询方法----------");
return new User("Oracle",22);
} @Override
@CacheEvict(value="users")
public void evictUser(String name, int age) {
System.out.println("--正在清空" + name + "," + age + "对应的缓存--");
} @Override
@CacheEvict(value="users",allEntries=true)
public void evictAll() {
System.out.println("--正在情况整个缓存--");
} }

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描Spring的组件 -->
<context:component-scan base-package="edu.pri.lime._8_5_3.cacheevict.service.impl"/> <!-- 启用Spring缓存 -->
<cache:annotation-driven cache-manager="cacheManager" /> <!-- 使用simpleCacheManager缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<!-- 配置缓存区 -->
<property name="caches">
<set>
<!-- 使用ConcurrentMapCacheFactoryBean工程Bean生产缓存区 -->
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!-- 定义缓存区名称 -->
<property name="name" value="users" />
</bean>
</set>
</property>
</bean>
</beans>

          Class : SpringTest

package edu.pri.lime._8_5_3.cacheevict;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._8_5_3.cacheevict.bean.User;
import edu.pri.lime._8_5_3.cacheevict.service.UserService; public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_5_3_cacheevict.xml");
UserService userService = ctx.getBean("userService",UserService.class);
User userA = userService.getUserByNameAndAge("lime", 24);
User userB = userService.getAnotherUser("lime", 24);
System.out.println(userA == userB);
User userC = userService.getUserByNameAndAge("Oracle", 24);
User userD = userService.getAnotherUser("Oracle", 24);
System.out.println(userD == userC); userService.evictUser("lime", 24); User userE = userService.getUserByNameAndAge("lime", 24);
User userF = userService.getAnotherUser("Oracle", 24); userService.evictAll(); User userG = userService.getUserByNameAndAge("lime", 24);
User userH = userService.getAnotherUser("Oracle", 24); }
}

          Console :

----------正在执行getUserByNameAndAge()查询方法----------
true
----------正在执行getUserByNameAndAge()查询方法----------
true
--正在清空lime,24对应的缓存--
----------正在执行getUserByNameAndAge()查询方法----------
--正在情况整个缓存--
----------正在执行getUserByNameAndAge()查询方法----------
----------正在执行getAnotherUser()查询方法----------

啦啦啦