企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Hibernate OSCache 配置示例教程 > 原文: [https://howtodoinjava.com/hibernate/hibernate-oscache-configuration-example-tutorial/](https://howtodoinjava.com/hibernate/hibernate-oscache-configuration-example-tutorial/) [**OSCache**](//howtodoinjava.com/category/frameworks/oscache/ "oscache tutorials") 是 OpenSymphony 开发的 Java 框架,可轻松在 Web 应用中缓存内容。 使用 Hiberate ,可以将其配置为充当[**二级缓存**](//howtodoinjava.com/hibernate/how-hibernate-second-level-cache-works/ "How hibernate second level cache works?") 。 在我的前一篇文章中,我们了解了[**为 Hiberate**](//howtodoinjava.com/hibernate/hibernate-ehcache-configuration-tutorial/ "Hibernate EhCache configuration tutorial") 配置 EhCache 的过程,这是 Hiberate 中的默认二级缓存。 在本文中,我以使用 [**Hiberate**](//howtodoinjava.com/hibernate-tutorials/ "hibernate tutorials") 配置 OSCache 为例。 ```java Sections in this post: Runtime dependencies Hibernate configuration In memory cache example Physical cache example ``` ## 运行时依赖项 我已经使用 [**Maven**](//howtodoinjava.com/maven/ "maven tutorials") 来管理项目依赖项,并且`pom.xml`文件中的必要补充是: ```java <repositories> <repository> <id>repository.jboss.org-public</id> <name>JBoss.org Maven repository</name> <url>https://repository.jboss.org/nexus/content/groups/public</url> </repository> </repositories> <!-- OSCache dependencies --> <dependency> <groupId>opensymphony</groupId> <artifactId>oscache</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> <version>1.1</version> </dependency> ``` 如果您不使用 maven,则**在项目构建路径中添加相应的 jar 文件**。 ## Hiberate 配置 在项目中配置 OSCache 的唯一更改是,必须在 Hiberate 配置文件中创建 `hibernate.cfg.xml`文件: ```java <!-- Cache provider class --> <property name="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</property> ``` ## 内存中的缓存示例 这是默认实现,如果未配置物理缓存属性,则会得到它。 让我们看一下测试代码示例: ```java try { //Open the hibernate session Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //fetch the department entity from database first time DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //fetch the department entity again; Fetched from first level cache department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //Let's close the session session.getTransaction().commit(); session.close(); //Try to get department in new session Session anotherSession = HibernateUtil.getSessionFactory().openSession(); anotherSession.beginTransaction(); //Here entity is already in second level cache so no database query will be hit department = (DepartmentEntity) anotherSession.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); anotherSession.getTransaction().commit(); anotherSession.close(); } finally { System.out.println(HibernateUtil.getSessionFactory().getStatistics().getEntityFetchCount()); //Prints 1 System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()); //Prints 1 HibernateUtil.shutdown(); } Output in console: Hibernate: insert into DEPARTMENT (NAME) values (?) Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource Human Resource Human Resource 1 1 ``` 您将一次又一次地获得此输出,因为每次关闭 Hiberate 时,都会刷新内存并在下次运行时构建二级缓存。 ## 物理缓存示例 如果您要构建的缓存非常大,则最好在文件系统中构建它(例如 Windows 中的 C 驱动器)。 这将防止 Hiberate 在每次重新启动应用时建立高速缓存。 同样,[二级缓存提取规则](//howtodoinjava.com/hibernate/how-hibernate-second-level-cache-works/ "How hibernate second level cache works?")仍然适用。 要启用物理缓存,请下载[**`oscache.properties`**](https://svn.apache.org/repos/asf/db/ojb/trunk/src/config/oscache.properties "download oscache.properties")文件,并取消注释以下几行: ```java cache.memory=false cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener cache.path=c:/temp/cache cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache ``` 让我们看一下测试代码示例: ```java try { //Open the hibernate session Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //fetch the department entity from database first time DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //fetch the department entity again; Fetched from first level cache department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); //Let's close the session session.getTransaction().commit(); session.close(); //Try to get department in new session Session anotherSession = HibernateUtil.getSessionFactory().openSession(); anotherSession.beginTransaction(); //Here entity is already in second level cache so no database query will be hit department = (DepartmentEntity) anotherSession.load(DepartmentEntity.class, new Integer(1)); System.out.println(department.getName()); anotherSession.getTransaction().commit(); anotherSession.close(); } finally { System.out.println(HibernateUtil.getSessionFactory().getStatistics().getEntityFetchCount()); //Prints 1 System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()); //Prints 1 HibernateUtil.shutdown(); } Output in console: Hibernate: insert into DEPARTMENT (NAME) values (?) Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=? Human Resource Human Resource Human Resource 1 1 ``` 上面的代码将在“`c:/tempcache`”位置创建物理缓存。 ![physical oscache example](https://img.kancloud.cn/7d/d8/7dd8051511b6859aefc3515cb79f3fa8_773x186.png "physical oscache example") 下次再次运行示例代码时,将获得以下输出: ```java Hibernate: insert into DEPARTMENT (NAME) values (?) Human Resource Human Resource Human Resource 0 2 ``` 将部门实体存储在物理二级缓存中并从那里获取的原因很简单。 因此 Hiberate 将不会再次进入数据库。 要下载该项目的源代码,请遵循给定的链接。 **祝您学习愉快!**