ehcache.xml配置详解

配置ehcache.xml文件

1. 在Hibernate配置文件中设置:

<!--HibernateSessionFactory-->
<beanid="sessionFactoryclassorg.springframework.orm.hibernate3.LocalSessionfactorybean> propertynamedataSourceref/> mappingResourceslistvalue>com/ououmodelVideos.hbm.xml</>
propertyhibernatePropertiespropspropkeyhibernate.dialectorg.hibernate.dialect.MysqLDialectprophibernate.current_session_context_classthreadhibernate.cglib.use_reflection_optimizerfalsehibernate.query.substitutionstrue'Y,Naddehcachehibernate.cache.provider_classorg.hibernate.cache.EhCacheProviderhibernate.cache.use_query_cache><!--是否使用查询缓存
<!-- hibernate.cache.provider_configuration_file_resource_path>/ehcache.xmlhibernate.show_sql--> <!--<hibernate.transaction.auto_close_session>-->
connection.provider_classorg.hibernate.connection.C3P0ConnectionProviderCreate/ updatethedatabasetablesautomaticallywhentheJVMstartsup
hibernate.hbm2ddl.autoupdateTurnbatchingoffforbettererrormessagesunderPostgresqlhibernate.jdbc.batch_size25hibernate.connection.pool_size10bean>


如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createquery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行

2.首先设置EhCache,建立配置文件ehcache.xml,认的位置在class-path,可以放到你的src目录下:

ehcache

<!-- Setsthepathtothedirectorywherecache.datafilesarecreated.

IfthepathisaJavaSystemPropertyitisreplacedby
itsvalueintherunningVM.
Thefollowingpropertiesaretranslated:
user.home
-Usershomedirectory
user.dirscurrentworkingdirectory java.io.tmpdirDefaulttempfilepathdiskStorepath/>--> /data/ehcacheDefaultCacheconfiguration.Thesewillappliedtocachesprogrammaticallycreatedthrough
theCacheManager.

Thefollowingattributesarerequired:

maxElementsInMemory
- Setsthemaximumnumberofobjectsthatwillbecreatedinmemory
eternal
Setswhetherelementsareeternal.Ifeternal,timeoutsare
ignoredandtheelementisneverexpired.
overflowTodisk
Setswhetherelementscanoverflowtodiskwhentheinmemorycache
hasreachedthemaxInMemorylimit.

Thefollowingattributesareoptional:
timetoIdleSeconds
Setsthetimetoidlefor
anelementbeforeitexpires.
i.e.Themaximumamountoftimebetweenaccessesbeforean
elementexpiresIsonlyused
if
theelementisnoteternal.
Optionalattribute.Avalueof
0
meansthatanElementcanidle
infinity.Thedefaultvalueis.
timetoLiveSeconds
Setsthetimetoliveanelementbeforeitexpires.
i.e.Themaximumtimebetweencreationtimeandwhenanelement
expires.Isonlyused
meansthatandElementcanlive
infinity.
The
.
diskPersistent
WhetherthediskstorepersistsbetweenrestartsoftheVirtual
Machine.
The
false
.
diskExpiryThreadIntervalSeconds
Thenumberofsecondsbetweenrunsofthediskexpirythread.
The
120
seconds.
< defaultCache
maxElementsInMemory
10000"

eternal
false
overflowTodisk
true
timetoIdleSeconds
120
timetoLiveSeconds

diskPersistent

diskExpiryThreadIntervalSeconds
cachenameorg.hibernate.cache.UpdateTimestampsCachemaxElementsInMemory5000"
eternal
overflowTodiskorg.hibernate.cache.StandardQueryCache5eternaluserCache100000timetoIdleSeconds=

"
600 timetoLiveSecondsdiskPersistentcom.ouou.webapp.util.OuouMethodInteceptertimetoLiveSecondsbbcode
overflowTodisk
com.ouou.model.Videoscom.ouou.model.Tags>


以com.ouou.model.Videos为例子 在Videos.hbm.xml中配置: <class table="TEST" lazy="false"> <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。 hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache, 如果不存在与类名匹配的cache名称,则用defaultCache。 如果Videos包含set集合,则需要另行指定其cache 例如Videos包含Tags集合,则需要 添加如下配置到ehcache.xml中 <cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false" timetoIdleSeconds="120" timetoLiveSeconds="120" overflowTodisk="false" /> 另,针对查询缓存的配置如下: <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowTodisk="true"/> <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timetoLiveSeconds="120" overflowTodisk="true"/> 3、 选择缓存策略依据: <cache usage="transactional|read-write|nonstrict-read-write|read-only" (1)/> ehcache不支持transactional,其他三种可以支持。 read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果, 但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。 read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level (序列化事务隔离级别) nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离, 那么比较适合使用非严格读/写缓存策略。 4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。 5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 ( org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。 请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。 需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。 英文参考资料:http://ehcache.sourceforge.net/documentation/#mozTocId258426 博文参考:http://blog.csdn.net/yun15291li/archive/2006/02/21/604095.aspx http://zyl.javaeye.com/blog/68369 其他:http://dev.yesky.com/157/2557157.shtml

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇