ehcache 程序
本文介绍如何在Openxava应用程序上快速启用Ehcache,从而提高性能。
查看实体及其图形时,将加载关系。 添加第二级缓存可加快关联元素的检索速度,因为已加载的元素是从缓存而不是数据库中检索的。
最终,该页面解释了分钟项目如何遵守诺言:不写任何内容。
例如,我们将以Lazuly分钟项目展示柜为例。
Openxava-Ehcache集成
在Openxava中,您可以使用Java注释的POJO来描述模型。注释来自标准的JPA2 ORM和特定于Openxava的注释。
但是没有什么可以阻止您添加其他人。 完成添加缓存的操作。 还有一些配置可以启用缓存。
行动清单
- 在源的根目录添加ehcache.xml配置文件
- 修改persistence.xml以包括二级缓存
- 添加缓存注释(与JPA2一起)
备注:
Openxava带有ehcache.jar,因此无需添加依赖项。
详细动作
添加ehcache.xml
在/ persistence处的ehcache.xml文件中
<ehcache>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="300"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="your.domain.object"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
/>
</ehcache>
修改persistence.xml
Persistence.xml文件包含与永久单元有关的信息,例如连接池信息,
类或配置加载。 'persistence.xml'位于/ persistence / META-INF中
我们将附加L2缓存的属性。
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
<property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.generate_statistics" value="true" />
</properties>
添加缓存注释
此处使用的是Hibernate注释,而不是标准注释(实际上,可缓存似乎不起作用)
将缓存注释放置在域对象的类级别。
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
例
懒惰的应用
Lazuly是一个示例数据库,其中包含用于MinuteProject展示目的的会议信息。
Minuteproject生成了一套完整的人工制品,以加快OX应用程序的发布。
可以在Minuteproject 4 Openxava Lazuly展示柜中找到更多信息。 在这一部分,我们重点介绍针对特定缓存生成的伪像。 用于生成的Minuteproject本身基于配置文件,我们在其中定义数据模型以进行反向工程。 在此配置中,有一个扩充部分,您可以在其中添加信息。 处理实体类型中包含的内容类型的信息之一。 有4种可能性(参考数据,主数据,伪静态数据,实时业务数据) 如果使用content-type =“ master-data”或“ reference-data”丰富您的实体,MinuteProject 4 Openxava将生成关联的缓存。
这是针对国家实体在这里完成的。
<entity name="COUNTRY" content-type="reference-data">
这是与缓存有关的伪像
ehcache.xml
<ehcache>
<!--
Sets the path to the directory where cache 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
Subdirectories can be specified below the property e.g. java.io.tmpdir/one
-->
<!--MP-MANAGED-UPDATABLE-BEGINNING-DISABLE @ehcache-main-config-conference@-->
<diskStore path="java.io.tmpdir"/>
<!--
Mandatory Default Cache configuration. These settings will be applied to caches
created programmtically using CacheManager.add(String cacheName)
-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="300"
memoryStoreEvictionPolicy="LRU"
/>
<!-- The unnamed query cache -->
<cache
name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="1000"
eternal="false"
timeToLiveSeconds="300"
overflowToDisk="false"
/>
<!--MP-MANAGED-UPDATABLE-ENDING-->
<!--MP-MANAGED-UPDATABLE-BEGINNING-DISABLE @cache-entity-country-conference@-->
<cache
name="net.sf.mp.demo.conference.domain.admin.Country"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
/>
<!--MP-MANAGED-UPDATABLE-ENDING-->
<!--MP-MANAGED-ADDED-AREA-BEGINNING @custom-cache-definition@-->
<!--MP-MANAGED-ADDED-AREA-ENDING @custom-cache-definition@-->
</ehcache>
Persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- Tomcat + Hypersonic -->
<persistence-unit name="default">
<non-jta-data-source>java:comp/env/jdbc/conferenceDS</non-jta-data-source>
<class>org.openxava.session.GalleryImage</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
<property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<!--MP-MANAGED-ADDED-AREA-BEGINNING @properties@-->
<!--MP-MANAGED-ADDED-AREA-ENDING @properties@-->
</properties>
<!--MP-MANAGED-ADDED-AREA-BEGINNING @persistence-unit@-->
<!--MP-MANAGED-ADDED-AREA-ENDING @persistence-unit@-->
</persistence-unit>
<!--MP-MANAGED-ADDED-AREA-BEGINNING @persistence@-->
<!--MP-MANAGED-ADDED-AREA-ENDING @persistence@-->
</persistence>
类注解
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
//MP-MANAGED-ADDED-AREA-BEGINNING @class-annotation@
//MP-MANAGED-ADDED-AREA-ENDING @class-annotation@
public class Country {
@Hidden @Id @Column(name="id" )
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
...
生成的代码备注
生成的代码在文件扩展名注释中包含标记。
在MP-MANAGED-ADDED-AREA-BEGINNING和MP-MANAGED-ADDED-AREA-ENDING中,您可以放置自定义代码
在MP-MANAGED-UPDATABLE-BEGINNING-DISABLE和MP-MANAGED-UPDATABLE-ENDING中,您可以更改代码。 为了保留您的修改,请将MP-MANAGED-UPDATABLE-BEGINNING-DISABLE更改为MP-MANAGED-UPDATABLE-BEGINNING-ENABLE。
可更新的代码可防止您连续几代丢失定制。
有关可更新代码的更多信息,请参见Minuteproject可更新代码 。
代
- 将以下文件mp-config-LAZULY-OPENXAVA.xml放在/ mywork / config中
- 在提示符下执行mp-model-generation(.sh / cmd)mp-config-LAZULY-OPENXAVA.xml
- / DEV / output / openxava / conference中产生的人工制品
要生成,请使用mp-config-LAZULY-OPENXAVA.xml的更新版本
<!DOCTYPE root>
<generator-config>
<configuration>
<conventions>
<target-convention type="enable-updatable-code-feature" />
</conventions>
<model name="conference" version="1.0" package-root="net.sf.mp.demo">
<data-model>
<driver name="mysql" version="5.1.16" groupId="mysql" artifactId="mysql-connector-java"></driver>
<dataSource>
<driverClassName>org.gjt.mm.mysql.Driver</driverClassName>
<url>jdbc:mysql://127.0.0.1:3306/conference</url>
<username>root</username>
<password>mysql</password>
</dataSource>
<!--
for Oracle and DB2 please set the schema <schema> </schema>
-->
<primaryKeyPolicy oneGlobal="true">
<primaryKeyPolicyPattern name="autoincrementPattern"></primaryKeyPolicyPattern>
</primaryKeyPolicy>
</data-model>
<business-model>
<!--
<generation-condition> <condition type="exclude"
startsWith="DUAL"></condition> </generation-condition>
-->
<business-package default="conference">
<condition type="package" startsWith="STAT" result="statistics"></condition>
<condition type="package" startsWith="COUNTRY" result="admin"></condition>
<condition type="package" startsWith="ROLE" result="admin"></condition>
</business-package>
<enrichment>
<conventions>
<column-naming-convention type="apply-strip-column-name-suffix"
pattern-to-strip="_ID" />
<reference-naming-convention
type="apply-referenced-alias-when-no-ambiguity" is-to-plurialize="true" />
</conventions>
<entity name="COUNTRY" content-type="reference-data">
<semantic-reference>
<sql-path path="NAME" />
</semantic-reference>
</entity>
<entity name="CONFERENCE_MEMBER">
<semantic-reference>
<sql-path path="FIRST_NAME" />
<sql-path path="LAST_NAME" />
</semantic-reference>
<field name="STATUS">
<property tag="checkconstraint" alias="conference_member_status">
<property name="PENDING" value="PENDING" />
<property name="ACTIVE" value="ACTIVE" />
</property>
</field>
<field name="EMAIL">
<stereotype stereotype="EMAIL" />
</field>
</entity>
<entity name="SPEAKER">
<field name="BIO">
<stereotype stereotype="HTML_TEXT" />
</field>
<field name="PHOTO">
<stereotype stereotype="PHOTO" />
</field>
<field name="WEB_SITE_URL">
<stereotype stereotype="WEBURL" />
</field>
</entity>
<entity name="PRESENTATION">
<field name="STATUS">
<property tag="checkconstraint" alias="presentation_status">
<property name="PROPOSAL" value="PROPOSAL" />
<property name="ACTIVE" value="ACTIVE" />
</property>
</field>
</entity>
<entity name="SPONSOR">
<field name="STATUS">
<property tag="checkconstraint" alias="sponsor_status">
<property name="PENDING" value="PENDING" />
<property name="ACTIVE" value="ACTIVE" />
</property>
</field>
<field name="PRIVILEGE_TYPE">
<property tag="checkconstraint" alias="sponsor_privilege">
<property name="GOLDEN" value="Golden" />
<property name="SILVER" value="Silver" />
<property name="BRONZE" value="Bronze" />
</property>
</field>
</entity>
<!-- views -->
<entity name="stat_mb_per_ctry_conf" alias="MEMBER_PER_COUNTRY_AND_CONFERENCE">
<virtual-primary-key isRealPrimaryKey="true">
<property name="virtualPrimaryKey" value="ID" />
</virtual-primary-key>
</entity>
<entity name="stat_mb_by_role" alias="MEMBER_PER_ROLE_COUNTRY_AND_CONFERENCE">
<virtual-primary-key isRealPrimaryKey="true">
<property name="virtualPrimaryKey" value="id" />
</virtual-primary-key>
<field name="stat_mb_per_ctry_conf_ID" linkToTargetEntity="stat_mb_per_ctry_conf"
linkToTargetField="id"></field>
</entity>
</enrichment>
</business-model>
</model>
<targets>
<!-- openxava -->
<target refname="OpenXava" name="OpenXava"
fileName="mp-template-config-openxava-last-features.xml"
outputdir-root="../../DEV/output/openxava/conference"
templatedir-root="../../template/framework/openxava">
</target>
<target refname="JPA2-LIB" fileName="mp-template-config-JPA2-LIB.xml"
templatedir-root="../../template/framework/jpa">
</target>
<target refname="BSLA-LIB" fileName="mp-template-config-bsla-LIB-features.xml"
templatedir-root="../../template/framework/bsla">
</target>
<target refname="CACHE-LIB"
fileName="mp-template-config-CACHE-LIB.xml"
templatedir-root="../../template/framework/cache">
</target>
</targets>
</configuration>
</generator-config>
测试
为确保缓存正常工作:
- 启用Hibernate日志记录。 将以下代码段添加为persistence.xml中的其他属性。
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
- 导航到引用国家/地区的实体(例如地址)
- 当您查看此实体的详细信息时,您会注意到相关实体“国家”的负载很大
- 但是,第二次访问该实体(或引用同一国家/地区实例的另一个实体)的详细信息时,该国家/地区不会从数据库中加载两次。
参考:我们的JCG合作伙伴 将Ehcache添加到Openxava应用程序中 分钟项目博客上的 Florian Adler。
翻译自: https://www.javacodegeeks.com/2012/03/adding-ehcache-to-openxava-application.html
ehcache 程序