Spring缓存实战应用

         缓存的概念大家都耳熟能详,但到底针对什么样的应用场景见识就很少了!刚好本次工作中碰到这样一个应用场景:客户要上传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 缓存详细说明》

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值