最近没什么事,研究了下分布式缓存Ehcache.
一、下载
从terracotta.org上下载terracotta-3.7.5-installer.jar,然后使用java -jar命令来安装它。
在安装目录的bin目录terracotta-3.7.5\bin下运行start-tc-server.bat文件来启动terracotta
二、客户端:
需要的JAR包:
在terracotta安装目录中找到terracotta-3.7.5\ehcache\lib\下的文件:
ehcache-core-2.6.6.jar(建议不要使用与terracotta不匹配的ehcache版本,如ehche-core-2.7.0,否则可能客户端代码出现异常)
ehcache-terracotta-2.6.6.jar
在terracotta安装目录中找到common\terracotta-toolkit-1.6-runtime-5.5.0.jar
然后加上日志包:slf4j-api、slf4j-log4j、log4j
配置文件:
log4j.properties
ehcache.xml
<cache name="terracottaCacheTest"
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="1800"
overflowToDisk="false">
<terracotta/>
</cache>
测试类:
publicclass TerracottaExample
{
CacheManager cacheManager = new CacheManager();
public TerracottaExample()
{
Cache cache = cacheManager.getCache("terracottaCacheTest");
int cacheSize = cache.getKeys().size();
cache.put(new Element("" + cacheSize, cacheSize));
for (Object key : cache.getKeys())
{
System.out.println("Key:" + key+",value:"+cache.get(key).getObjectValue());
}
cacheManager.shutdown();
}
publicstaticvoid main(String[] args) throws Exception
{
new TerracottaExample();
}
}
三、在Hibernate中使用分布式二级缓存
加入hibernate4.2.0的Jar包
加入mysql-connector-java-5.1.24-bin.jar
添加Hibernate.cfg.xml文件:如下
<session-factory>
<!-- SQL dialect -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="hbm2ddl.auto">update</property>
<property name="javax.persistence.validation.mode">none</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
<property name="jdbc.batch_size">50</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- Names the annotated entity class -->
<mapping class="bean.Student"/>
</session-factory>
创建Bean
@Entity
@Table(name="student")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE,region="terracottaCacheTest")//这里的区域仍然使用terracottaCacheTest,当hibernate操作数据时,可以查看此缓存区域中是否有加入了缓存中。
publicclass Student implements Serializable
{
privatestaticfinallongserialVersionUID = -2482463021057396397L;
privateintid;
private String name;
//省略get、set方法
}
测试类
Configuration cfg=new Configuration().configure();//加载配置文件hibernate.cfg.xml
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();//ServiceRegistryBuilder通过配置文件的参数来建立相应的ServiceRegistry!
SessionFactory sessionFactory =cfg.buildSessionFactory(serviceRegistry);//建立SessionFactory
Session session=sessionFactory.getCurrentSession();
Transaction transaction=session.beginTransaction();
try{
Student student=(Student)session.get(Student.class, 1);
System.out.println(student.getName());
transaction.commit();
}catch (Exception e) {
transaction.rollback();
}// finally{//事务在提交或回滚的时候会检测Session是否关闭,如果没有则
// if(session!=null&&session.isOpen())
// {
// System.out.println("关闭Session……");
// session.close();
// }
// }
sessionFactory.close();
然后运行TerracottaExample这个类,这会发现通过hibernate使用分布式ehcache已经生效!
下面是打印缓存中的内容
Key:bean.Student#1,value:org.hibernate.cache.ehcache.internal.strategy.AbstractReadWriteEhcacheAccessStrategy$Item@a03a12
国内首套免费的《大数据技术(Hadoop)视频教程》:
相关推荐
Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个 gzip 缓存 servlet 过滤器,支持 REST 和 SOAP api...
软件仍然是开源,但一些新的主要功能(例如,快速可重启性之间的一致性的)只能在商业产品中使用,例如Enterprise EHCache and BigMemory。,维基媒体Foundationannounced目前使用的就是Ehcache技术。
软件仍然是开源,但一些新的主要功能(例如,快速可重启性之间的一致性的)只能在商业产品中使用,例如Enterprise EHCache and BigMemory。,维基媒体Foundationannounced目前使用的就是Ehcache技术。
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 下图是 Ehcache 在应用程序中的位置: 主要的特性有: 1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:...
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。主要的特性有:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题5. 缓存...
EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度。
ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案,是hibernate中默认的CacheProvider。
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。Ehcache是一种广泛使用的开 源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,...
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 主要的特性有: 1. 快速. 2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心...
Ehcache用户指南。版本号 V1.5 Hibernate的默认二级缓存的实现者,支持分布式缓存。
为您提供Ehcache Java缓存框架下载,EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。主要的特性有:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和...
为您提供EhcacheJava缓存框架下载,EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。主要的特性有:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和...
ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案。同时ehcache作为开放源代码项目,采用限制比较宽松的...
EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。Ehcache是一种广泛使用的开 源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,...
系统特点: 1、使用ehcache 缓存存储用户登录信息,同时可以结合 terracotta 进行分布式部署,可以避免传统分布式部署中因session 复制带来的性能问题。 2、系统中菜单配置可以使用右键菜单管理,这个实现当时花了...
4、eHcache 以及参数1
EhCache EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 主要的特性有: 1. 快速. 2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心容量...
<p>EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。</p><p>主要的特性有: 1. 快速. 2. 简单. 3. 多种缓存策略 4. 缓存数据有两级:内存和磁盘,因此无需担心容量...
APDPlat快速体验 APDPlat入门指南 APDPlat专题文章 APDPlat是Application Product Development Platform(应用级产品开发平台...27)、分布式搜索和实时分析使用elasticsearch 标签:APDPlat Web框架
Ehcache是一种广泛使用的Java分布式缓存器,具有快速、精干等特点,是Hibernate中默认CacheProvider。它提供内存存储和磁盘存储两种方案,因此无需担心容量问题。Ehcache可以单独使用,一般在第三方库中被用到的比较...