考虑到效率和对数据库的压力,使用缓存或者内存缓存,可以提高反应速度和减轻数据库压力。hibernate中支持的比较多,在hibernate给的文档“提升性能”章节有详细介绍:
hibernate支持缓存类型和介绍:
后面三个还支持集群,比较强大。
现在详细介绍Ehcache使用:
Ehcache所需要的jar包(配合hibernate使用):
加入配置文件:
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
</ehcache>
hibernate配置:
如果Query要缓存要手动设置的:
如果某个类有集合字段,我们也想要集合字段缓存,我们需要对那个字段设置缓存属性:
这种设置对于多级菜单很是有效的!
第一次(没的使用缓存的)效果:
第二次(缓存)效果:
分享到:
相关推荐
hibernate-ehcache-4.1.0.Final.jar 是hibernate4.1使用缓存的jar包
ehcache 二级缓存 配置使用的jar包 配置如下: <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate....
EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和...
NULL 博文链接:https://dreamoftch.iteye.com/blog/1985366
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
Hibernate中默认的二级缓存机制ehcache案例,属性ehcache环境搭建,配置问题。
Hibernate4二级缓存Ehcache案例,可参考博客:http://blog.csdn.net/coco2d_x2014/article/details/52927638
使用Java工程实现了Hibernate二级缓存(Ehcache),其中还有自己封装的一个jar包实现了简单的分页功能
2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。 <!--配置缓存插件 --> <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider 3) 挎贝ehcache.xml文件到类...
hibernate二级缓存 ehcache
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-...
Hibernate缓存机制 细谈Ehcache页面缓存的使用 怎样判断对象是否存在于缓存中
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。我们也可以通过debug调试查看cacheManager对象的实例来判断当前使用了什么缓存。在上一篇中,我们也展示了如何去查看当前使用情况。
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 下图是 Ehcache 在应用程序中的位置: 主要的特性有: 1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:...
Hibernate EhCache 二级缓存配置
hibernate最核心的 就是缓存技术
hibernate二级缓存相关插件(EHcache)