当前位置: 首页 > news >正文

龙华建设局网站uc浏览器网页版入口

龙华建设局网站,uc浏览器网页版入口,网页设计作业一般多少钱,创建网站的四个步骤是在spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式。不过,在实际开发中,我们往往是通过Spring的Cacheable来实现数据的缓存的,所以,本文给大家详细介绍一下Cacheable的用法。 首先,在使用…

spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式。不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以,本文给大家详细介绍一下@Cacheable的用法。

首先,在使用@Cacheable之前,我们要做好准备工作。


第一步:要导入相应的jar包

   <classpathentry kind="lib" path="lib/spring-core-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-cache-1.0.10.jar"/>
    <classpathentry kind="lib" path="lib/spring-context-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-beans-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
    <classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
    <classpathentry kind="lib" path="lib/spring-expression-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/java_memcached-release_2.0.1.jar"/>
    <classpathentry kind="lib" path="lib/spring-aop-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-aspects-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-context-support-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/spring-tx-4.1.4.RELEASE.jar"/>
    <classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/>
    <classpathentry kind="lib" path="lib/ognl-3.0.6.jar"/>
    <classpathentry kind="lib" path="lib/trafficCounter-1.0.2.jar"/>
    <classpathentry kind="lib" path="lib/aspectjweaver-1.8.4.jar"/>
    <classpathentry kind="lib" path="lib/javassist-3.11.0.GA.jar"/>


第二步:xml文件中增加命名空间

<span style="font-size:14px;"><?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:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"></span>

之后添加如下声明:

</pre><pre name="code" class="html"><span style="font-size:14px;"><?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:cache="http://www.springframework.org/schema/cache"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager" /><!-- cacheManager工厂类,指定ehcache.xml的位置 -->   <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  <property name="configLocation"  value="classpath:cache/ehcache.xml"/>  </bean><!-- 声明cacheManager -->  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="cacheManagerFactory"/></bean>
</beans></span>

 

第三步: ehcache.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"updateCheck="false" dynamicConfig="false" monitoring="autodetect">  <diskStore path="java.io.tmpdir" /><!--diskStore path:用来配置磁盘缓存使用的物理路径name:	缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)eternal="false"   元素是否永恒,如果是就永不过期(必须设置)maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置)timeToIdleSeconds="0"   导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期overflowToDisk="false"  当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)diskPersistent="false"  磁盘缓存在VM重新启动时是否保持(默认为false)diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)默认使用"最近使用"策略--><defaultCache  eternal="false"maxElementsInMemory="3000"  timeToIdleSeconds="3600"  timeToLiveSeconds="0"  overflowToDisk="true"  diskPersistent="false"  diskExpiryThreadIntervalSeconds="100"  memoryStoreEvictionPolicy="LRU"/><cache name="propConfigCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="workTimeCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="threeInOneCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="transferCache"  eternal="false"maxElementsInMemory="1000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="threeInOneFavCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="reserveTimeCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="mqServerNameCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/><cache name="schWorkTimeCache"  eternal="false"maxElementsInMemory="3000"  overflowToDisk="true"timeToIdleSeconds="0"  timeToLiveSeconds="1440" memoryStoreEvictionPolicy="LFU"/></ehcache></span>

 

@Cacheable 支持如下几个参数:

  • value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
  • key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
  • condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
//将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key   
@Cacheable(value="andCache",key="#userId + 'findById'")  
public SystemUser findById(String userId) {  SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);        return user ;         
}  
//将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key  
@Cacheable(value="andCache",condition="#userId.length < 32")  
public boolean isReserved(String userId) {  System.out.println("hello andCache"+userId);  return false;  
} </span>

 


@CacheEvict 支持如下几个参数:

  • value:缓存位置名称,不能为空,同上
  • key:缓存的key,默认为空,同上
  • condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
  • allEntries:true表示清除value中的全部缓存,默认为false

allEntries属性详解:

       allEntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率。

   @CacheEvict(value="users", allEntries=true)public void delete(Integer id) {System.out.println("delete user by id: " + id);}

    //清除掉指定key的缓存  @CacheEvict(value="andCache",key="#user.userId + 'findById'")  public void modifyUserRole(SystemUser user) {  System.out.println("hello andCache delete"+user.getUserId());  }  //清除掉全部缓存  @CacheEvict(value="andCache",allEntries=true)  public final void setReservedUsers(String[] reservedUsers) {  System.out.println("hello andCache deleteall");  }</span>  

 

一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值 ,

比如我这里针对用户的操作,使用(userId+方法名称)的方式设定key值 ,当然,你也可以找到更适合自己的方式去设定。


EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
 

Ehcache是一种广泛使用的开源Java分布式缓存。

         主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

         Ehcache最初是由Greg Luck于2003年开始开发。2009年,该项目被Terracotta购买。软件仍然是开源,但一些新的主要功能(例如,快速可重启性之间的一致性的)只能在商业产品中使用,例如Enterprise EHCache and BigMemory。维基媒体Foundationannounced目前使用的就是Ehcache技术。

 


主要的特性有:

  1. 快速
  2. 简单
  3. 多种缓存策略
  4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
  5. 缓存数据会在虚拟机重启的过程中写入磁盘
  6. 可以通过RMI、可插入API等方式进行分布式缓存
  7. 具有缓存和缓存管理器的侦听接口
  8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
  9. 提供Hibernate的缓存实现

 


来源于:

https://blog.csdn.net/qq_17555933/article/details/51320800#

http://www.lbrq.cn/news/2528767.html

相关文章:

  • 西安网页设计师seo网站推广专员招聘
  • 美图秀秀在线修图seo职位要求
  • 那个网站推作者百度首页优化排名
  • 网站建设 资质使用最佳搜索引擎优化工具
  • 描述一下网站建设的基本流程直通车怎么开才有效果
  • 做调味品批发上哪个网站好搜索引擎优化要考虑哪些方面?
  • 淘宝优惠卷网站建设公司是真的假的徐州做网站的公司
  • 网站规划与开发技术专业黄页网站推广服务
  • 网站重新建设的申请dz论坛如何seo
  • 十大免费行情软件网站下载百度搜索榜
  • 电脑如何制作网页教程西安seo培训学校
  • 赤坎手机网站建设公司开源cms建站系统
  • 怎样做网站平台赚钱竞价服务托管公司
  • wordpress意见反馈功能资阳地seo
  • 网站怎么收录百度搜索推广是什么
  • 怎么制作免费网站教程视频百度收录时间
  • 动态网站建设工资优化排名
  • 上行10m做网站服务衡阳seo服务
  • 茶网站建设实训报告深圳快速seo排名优化
  • 专门做广东11选5的网站什么是seo搜索
  • 上海装修公司做网站注册城乡规划师含金量
  • 哈巴狗模式网站开发今日最近的新闻大事10条
  • 临沂做网站企业新东方考研培训机构官网
  • bootstrap开源模板进行优化
  • 一个旅游网站建设百度竞价排名背后的伦理问题
  • 建设网站要花多少钱南宁seo外包服务
  • html网站登录界面模板下载推广引流方法与渠道
  • wordpress查看购物车不管用seo赚钱吗
  • 网站建设需要哪些知识深圳网站制作公司
  • 最优做网站爱链接
  • coze 开源版 coze-studio 配置域名 处理上传问题 教程
  • LLM 模型部署难题的技术突破:从轻量化到分布式推理的全栈解决方案
  • 电商项目_核心业务_分布式事务
  • Excel文件批量加密工具
  • Redis的持久化策略-AOF和RDB(详细图解)
  • 青少年软件编程图形化Scratch等级考试试卷(二级)2025年6月