基于Web项目的Spring Cache 缓存配置入门实例(注解驱动)

第一步 引入相关jar包,Cache主要类库位于context jar包下.

这里写代码片

第二步 Spring 配置文件设置,本人项目中配置文件名为 applicationContext.xml.

f118b3bf040c499e8836145f82e44aaa<?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:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd"">//命名空间

    <cache:annotation-driven/>//开启注解

    <bean id="userController" class="com.chw.controller.system.SysUserController"/> // 注入Bean

    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">//cacle缓存管理器
       <property name="caches">
           <set>
               <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                   <property name="name" value="default"/>//默认缓存
               </bean>
               <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                   <property name="id" value="userCache"/>//根据自己需求定义的缓存name相当于Map中的key,value相当于Map中的value
               </bean>
           </set>
       </property>
    </bean>
</beans>

第三部 SysUserController类

package com.chw.controller.system;

import org.springframework.cache.annotation.CacheEvict; 
import org.springframework.cache.annotation.Cacheable; 

public class SysUserController extends BaseController {
   @Cacheable(value="userCache")
   //@CachePut:这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中。
   //@Cacheable:当重复使用相同参数调用方法的时候,方法本身不会被调用执行,即方法本身被略过了,取而代之的是方法的结果直接从缓存中找到并返回了
   public SysUser findById(String userId) {
        System.out.println("查询数据库");
        return sysUserService.findById(userId);
  } 
  @CacheEvict(value="userCache",key="#sysUser.getId()")// 根据id清除userCache缓存
  public int updateUser(SysUser sysUser) {
      return sysUserService.update(sysUser);
  } 

  @CacheEvict(value="userCache",allEntries=true)// 清除全部userCache缓存
  public void deleteAll() { 
        //do something
  }  

}

第四部 main测试类

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.chw.controller.system.SysUserController;
import com.chw.model.system.SysUser;

public class test {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         SysUserController userController = (SysUserController)context.getBean("userController");

         SysUser firstUser = userController.findById("1");//数据库中真实存在的数据
         System.out.println("first query..."+firstUser.toString()); 
         SysUser secondUser = userController.findById("1");
         System.out.println("second query..."+secondUser.toString()); 
    }
}

第五部 运行结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_33949836/article/details/80264236