`
xqls
  • 浏览: 27338 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Java-Cache

    博客分类:
  • Java
阅读更多
thanks http://www.blogjava.net/zzzlyr/articles/343234.html
It can also be downloaded from http://ehcache.org/ehcache.xml.
spring 3.0.5 发布后,公司使用Spring MVC +Hibernate 3.5 做项目,其中用到了缓存机制,spring 3.0.5 中ehcache配置方法很简单,其中缓存机制很细颗粒化,可以具体到把每个方式的返回值做缓存,好了不说废话下面开始:
spring 3.0.5 发布后,公司使用Spring MVC +Hibernate 3.5 做项目,其中用到了缓存机制,spring 3.0.5 中ehcache配置方法很简单,其中缓存机制很细颗粒化,可以具体到把每个方式的返回值做缓存,好了不说废话下面开始:

需要JAR包:
第一:spring 3.0.5 其中JAR;
第二:另外需要增量JAR包(cglib-2.2.jar,ehcache-spring-annotations-1.1.2.jar)注意版本;

其中applicationContext.xml 其中配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
  http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="ehCacheManager" />

<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
       <property name="configLocation" value="classpath:ehcache.xml" /> 
   </bean>

加到你的文件中去,上边是头信息自己可以比照下,没有的加进去;


在你的 src 目录下新建ehcache.xml内容如下:


<?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">
<diskStore path="java.io.tmpdir" />
<defaultCache eternal="false"
   maxElementsInMemory="1000"
   overflowToDisk="false"
   diskPersistent="false"
   timeToIdleSeconds="0"
   timeToLiveSeconds="600"
   memoryStoreEvictionPolicy="LRU" />

<cache name="departCache"
   eternal="false"
   maxElementsInMemory="100"
   overflowToDisk="false"
   diskPersistent="false"
   timeToIdleSeconds="0"
   timeToLiveSeconds="300"
   memoryStoreEvictionPolicy="LRU" />

</ehcache>

其中里边详细配置自己可以去上网搜下;

这样基本上配置完成了:

DAO层缓存:例如下边这个方法的返回值需要缓存:

@SuppressWarnings("unchecked")
//spring 3 基于注解ehcache缓存配置;
@Cacheable(cacheName="departCache")
public List<AppDepart> getChildDepart(Integer id) throws Exception {
  return  this.getHibernateTemplate().find("from AppDepart  where state=1 and idParent="+id);
}

@Cacheable(cacheName="departCache") 加上这句话,其中cacheName 对应ehcache.xml  中的<cache name="departCache"

这样这个方法返回值就可以被缓存起来的了,但是怎么样把缓存数据和数据库中的数据实现同步呢?

如果对这个PO做update ,save,delete 可以实现这样策略如下:

@Transactional(propagation = Propagation.REQUIRED)
//设定spring的ecache缓存策略,当编辑机构时候,把缓存全部清除掉,以达到缓存那数据同步;
@TriggersRemove(cacheName="departCache",removeAll=true)
public boolean editDepart(String depno, String depName) {
  boolean flag = false;
  try {
   AppDepart depart = departDao.getAppdepart(depno);
   depart.setDepName(depName);
   departDao.update(depart);
   flag = true;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return flag;
}

好了到此配置完毕,但是更加详细缓存配置策略需要研究(例如:当update数据时候,不全部清掉缓存,就可以达到与数据库同步效果)
以下配置经本人完成测试通过(只限于本版本)。

                            

分享到:
评论

相关推荐

    alisoft-xplatform-asf-cache-2.5.1.jar

    alisoft-xplatform-asf-cache-2.5.1.jar是一个Java实现的库,它为Java开发者提供了与Memcached服务器通信的接口。这个库的特点在于其源码开放,内部注释清晰且全为中文,使得国内开发者能够更轻松地理解和使用。这...

    shiro-cache-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro-cache-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...

    couchbase-java-cache:Couchbase Java SDK 2.0 的 JCache 实现

    couchbase-java-cache 这是针对 Couchbase Java SDK 2.0 的正在进行的 JCache 实现。 请参阅 JSR-107 、 和。 基本操作暂时实现。 部分缺失/缺失 到期由底层 couchbase 存储桶管理,不会通知 不支持 ...

    httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载

    这里提到的四个jar包——httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5,都是Apache HttpClient库的不同组件,用于支持HTTP通信和相关功能。 **httpcore-4.2.4.jar** 是HTTP Core模块...

    jar包-spring-modules-cache.jar

    总之,`spring-modules-cache.jar`中的Spring Modules Cache是Spring框架的一个强大补充,它简化了缓存的管理和使用,帮助开发者构建高性能、易维护的Java应用。无论是在单体应用还是微服务架构中,这个库都能发挥...

    mybatis-enhanced-cache源码和jar包

    而"mybatis-enhanced-cache-master.zip"则可能是源码包,如果你需要深入了解插件的工作原理或者想要对其进行二次开发,可以解压此文件查看源代码。 在实际使用中,你需要按照以下步骤配置和使用这个插件: 1. 添加...

    spring-cache.xsd+spring-encache.xsd

    标题中的"spring-cache.xsd+spring-encache.xsd"提到了两个XML Schema定义文件,它们是Spring框架中用于缓存管理的配置规范。Spring框架是一个广泛应用的Java企业级应用开发框架,它提供了多种功能,包括但不限于...

    aliyun-oss-java-sdk-demo-mvn

    - **缓存策略**:可以根据业务需求设置文件的Cache-Control头,控制浏览器缓存行为。 7. **监控与日志**: 阿里云提供详尽的监控数据,可以查看OSS的使用情况,包括流量、请求量、错误率等。同时,可以通过日志...

    开源项目-go-cache-cache.zip

    开源项目-go-cache-cache.zip是一个基于Go语言的内存缓存库,名为`go-cache`。这个库主要用于在应用程序中实现高效、轻量级的内存数据存储,以提高性能和响应速度。Go-cache的设计灵感可能来自于其他编程语言中的...

    httpclient-cache-4.5.jar

    用于http请求的jar包

    spring-boot-cache.rar

    Spring Boot Cache 是一个用于简化 Spring 应用程序中缓存管理的强大工具。在这个名为 "spring-boot-cache.rar" 的压缩包中,我们很可能是找到了一个基于 Spring Boot 的小型项目,它展示了如何在 Spring Boot 应用...

    spring-cache-4.1.xsd

    spring-cache-4.1.xsd用于spring开发代码提示。用于Java开发。

    java-dns-cache-manipulator::globe_showing_Asia-Australia:一个简单的0依赖线程安全的Java:trade_mark:libtool,可通过编程方式设置dns,而无需访问主机文件,从而使unitintegration test可移植

    Java Dns Cache Manipulator(DCM) 包含子工程: 用编码的方式设置/查看JVM的DNS(实际上设置的是DNS Cache),支持JDK 6+,支持IPv6。 用于设置/查看 运行中JVM进程的DNS Cache。 使用文档进入上面的子工程查看。

    springmodules-cache与springmodules-ehcache的xsd.rar

    &lt;cache:annotation-driven cache-manager="cacheManager"/&gt; &lt;cache:advice ref="cacheAdvice"/&gt; &lt;bean id="cacheAdvice" class="org.springframework.cache.interceptor.CacheAspectSupport"&gt; &lt;!-- 配置...

    史上最全datadalvik-cache 下可删除的文件对应列表

    ### 史上最全datadalvik-cache 下可删除的文件对应列表 在深入解析datadalvik-cache中的可删除文件之前,我们先来了解一下这个目录的基本概念。`datadalvik-cache`是Android系统中的一个特殊目录,它主要用于存放...

    react-native-http-cache2:RN http 缓存管理 原 repo 为 react-native-http-cache 由于不在维护, 对新版本 RN 不在支持, 故新建一个

    对fetch / XMLHttpRequest和ImageView进行本机http缓存控制 的iOS 安卓安装$ npm install react-native-http-cache2 --saveiOS:在您的XCode项目中进行链接从node_modules/react-native-http-cache2/ios文件夹中链接...

    Guava-Cache本地缓存案例代码

    在`guava-cache-demo`项目中,你可以找到以上各种功能的具体实现,通过运行这些示例代码,可以更深入地理解Guava Cache的用法。这个案例是学习和理解Guava Cache操作的一个良好起点,可以帮助开发者在实际项目中有效...

    java连接cache数据库说明,数据库驱动,cache可视化工具

    Java连接Cache数据库主要涉及到的是如何使用Java编程语言与Intersystems Cache数据库进行交互。Intersystems Cache是一款高性能、面向对象的数据库系统,广泛应用于医疗、金融等领域的复杂数据管理。在Java环境中,...

    infinispan-cachestore-jdbc-8.2.3.Final.zip

    《Infinispan缓存存储与Spring集成Java DSL解析》 在现代的分布式系统中,数据缓存扮演着至关重要的角色,它能够显著提升应用程序的性能。Infinispan是JBoss公司推出的一个强大且高度可配置的内存数据网格平台,...

Global site tag (gtag.js) - Google Analytics