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


      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()查詢方法----------

啦啦啦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM