缓存的概念大家都耳熟能详,但到底针对什么样的应用场景见识就很少了!刚好本次工作中碰到这样一个应用场景:客户要上传Excel文件,首先要把Excel文件内容读出放到页面列表中进行显示,确认无误后再保存入数据库。
实现分析:把数据从Excel读出后放入List中显示到页面,由于页面只是显示并没有进行修改功能。因此,再次点击保存进入数据库的数据,如果从页面上获取list,或者是再次从Excel中获取,就做了重复工作,这时,我们考虑如果点击上传Excel的时候,就把list数据保存到缓存中,再次点击保存的时候就可以直接从缓存中获得数据了!
Spring从3.1版本开始,引入了对Cache的支持,使用方法和原理均类似于Spring对事务的支持。缓存的中心思想是,我们把方法的参数和返回值以键值对的方式放入到缓存中,下次再次调用这个方法时,直接从缓存中获取,从而提高执行效率。
下面根据实战介绍一下Spring缓存注解实现过程:
1. maven提供常用的jar包即可,无序额外jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> 2. 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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="com.rollenholt.spring.cache"/> <context:annotation-config/> <cache:annotation-driven/> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <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="name" value="ExcelCache"/> </bean> </set> </property> </bean> </beans> 3. Service添加注解
@Cacheable(value="ExcelCache",key="#cacheVersion") @Override public List<CreditInfoBean> uploadReceiverCreditInfoExcel(InputStream in, MultipartFile file, Long companyId, Long userId,String cacheVersion) throws Exception { List<List<Object>> listob = ExcelUtils.getBankListByExcel(in,file.getOriginalFilename()); List<CreditInfoBean> creditInfoList=new ArrayList<CreditInfoBean>(); for (int i = 1; i < listob.size(); i++) { List<Object> ob = listob.get(i); CreditInfoBean creditInfoBean = new CreditInfoBean(); creditInfoBean.setCompanyName(String.valueOf(ob.get(0))); creditInfoList.add(creditInfoBean); } return creditInfoList; } 说明:key值是我在HTML添加的缓存版本编码,每次点击上传的时候自动加1,这样,即可以及时更新原有缓存,也可以保证在不更换excel文件的情况下始终使用前一个缓存。
4. 缓存清理
@CacheEvict(value="ExcelCache",allEntries=true) @Override public void reload() { } 我们不可能让缓存永远存在内存当中,这样系统没多久就死掉了呀!所以我们必须在合适的位置清理掉缓存。
总结:
本次应用只是用到了常用的两个缓存功能!详细的基础请解读下面文章:
《注释驱动的Spring Cache缓存介绍》
《Spring 缓存详细说明》