zoukankan      html  css  js  c++  java
  • springmvc缓存--验证通过

    --验证通过

    --参考 prescription-web-weinan

    ehcache jar坐标

    <dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.9.4</version>
    </dependency>
    <dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.2</version>
    </dependency>

    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-4.2.xsd

    --参考地址

    https://blog.csdn.net/tonytfjing/article/details/39251507

    前几篇文章已经搭建了一个基本的springmvc demo,现在我们来完善下。
    相信大家写程序的时候都接触过缓存的概念,也都知道,数据量大的时候缓存对于提高效率是很显著的。而缓存一般包括前台静态资源缓存和后台查询出来的数据缓存,这里介绍的是后者。最后感谢以下两篇文章,本文是在他们的基础上完成这个demo的。
    http://my.oschina.net/duoduo3369/blog/173924
    http://blog.csdn.net/jadyer/article/details/12257865
    开始奉上代码。

    1.在springmvc的配置文件中加入缓存配置,代码如下:
    <!-- 缓存配置(两种) -->
    <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
    <!--
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
    <set>
    <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>

    --------------------------------------------------------------------------------------------------------------------------------------

    https://blog.csdn.net/maoyeqiu/article/details/45119539

    解决方法:在service或者dao上添加@Service或@Repository标签,让其扫描这些类中方法上的@Cacheable标签。

    </set>
    </property>
    </bean>
    -->
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>

    (注意不要忘记引入对应的命名空间)

    2.在配置路径下(这里是默认的src下)建立ehcache.xml文件,并配置程序的相关cache策略,代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache dynamicConfig="false" monitoring="off" updateCheck="false"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">

    <!-- 定义缓存策略
    eternal="false" // 元素是否永恒,如果是就永不过期(必须设置)
    maxEntriesLocalHeap="1000" // 堆内存中最大缓存对象数,0没有限制(必须设置)
    overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)
    diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false)
    timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0
    timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期
    memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU
    -->
    <defaultCache eternal="false" maxEntriesLocalHeap="0" timeToIdleSeconds="300" timeToLiveSeconds="300"/>
    <cache name="myCache" maxEntriesLocalHeap="1000" />

    </ehcache>

    3.既然是ehcache,肯定要引入ehcache的jar:ehcache-2.8.3,至于还需要什么jar,运行后就会发现。

    4.运行后报错,nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
    是因为缺少aopaliance-1.0 jar包,加入即可。

    5.后台service代码(我的注解是加在service的方法上的):

    //将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的key
    //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值
    @Cacheable(value="myCache", key="#id")
    public String getUsernameById(int id){
    System.out.println("调用了测试缓存的方法");
    System.out.println("数据库中查到此用户号[" + id + "]对应的用户名为[" + userMapper.getUsernameById(id) + "]");
    return userMapper.getUsernameById(id);
    }

    注意:springmvc有关缓存的注解主要是@Cacheable、@CachePut、@CacheEvict。关于这三个的详细使用可参考:http://my.oschina.net/duoduo3369/blog/173924

    6.第一次访问前台页面:

    console后台有相关日志,日志如下:


    第二次执行,日志如下:


    程序没有执行我加了缓存注解的方法,后台没有日志,但是前台返回了数据,说明是从缓存里读取的数据,即缓存配置成功。

  • 相关阅读:
    可以使用多少列创建索引?
    如何显示前 50 行?
    简单描述 MySQL 中,索引,主键,唯一索引,联合索引的区别,对数据库的性能有什么影响-从读写两方面?
    实践中如何优化 MySQL ?
    列的字符串类型可以是什么?
    MySQL 里记录货币用什么字段类型好 ?
    什么是通用 SQL 函数?
    对于关系型数据库而言,索引是相当重要的概念?
    为表中得字段选择合适得数据类型?
    SQL 注入漏洞产生的原因?如何防止?
  • 原文地址:https://www.cnblogs.com/zhoading/p/15010365.html
Copyright © 2011-2022 走看看