原文: https://howtodoinjava.com/hibernate/hibernate-oscache-configuration-example-tutorial/

OSCache 是 OpenSymphony 开发的 Java 框架,可轻松在 Web 应用中缓存内容。 使用 Hiberate ,可以将其配置为充当二级缓存

在我的前一篇文章中,我们了解了为 Hiberate 配置 EhCache 的过程,这是 Hiberate 中的默认二级缓存。 在本文中,我以使用 Hiberate 配置 OSCache 为例。

  1. Sections in this post:
  2. Runtime dependencies
  3. Hibernate configuration
  4. In memory cache example
  5. Physical cache example

运行时依赖项

我已经使用 Maven 来管理项目依赖项,并且pom.xml文件中的必要补充是:

  1. <repositories>
  2. <repository>
  3. <id>repository.jboss.org-public</id>
  4. <name>JBoss.org Maven repository</name>
  5. <url>https://repository.jboss.org/nexus/content/groups/public</url>
  6. </repository>
  7. </repositories>
  8. <!-- OSCache dependencies -->
  9. <dependency>
  10. <groupId>opensymphony</groupId>
  11. <artifactId>oscache</artifactId>
  12. <version>2.4.1</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>javax.jms</groupId>
  16. <artifactId>jms</artifactId>
  17. <version>1.1</version>
  18. </dependency>

如果您不使用 maven,则在项目构建路径中添加相应的 jar 文件

Hiberate 配置

在项目中配置 OSCache 的唯一更改是,必须在 Hiberate 配置文件中创建 hibernate.cfg.xml文件:

  1. <!-- Cache provider class -->
  2. <property name="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</property>

内存中的缓存示例

这是默认实现,如果未配置物理缓存属性,则会得到它。 让我们看一下测试代码示例:

  1. try
  2. {
  3. //Open the hibernate session
  4. Session session = HibernateUtil.getSessionFactory().openSession();
  5. session.beginTransaction();
  6. //fetch the department entity from database first time
  7. DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
  8. System.out.println(department.getName());
  9. //fetch the department entity again; Fetched from first level cache
  10. department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
  11. System.out.println(department.getName());
  12. //Let's close the session
  13. session.getTransaction().commit();
  14. session.close();
  15. //Try to get department in new session
  16. Session anotherSession = HibernateUtil.getSessionFactory().openSession();
  17. anotherSession.beginTransaction();
  18. //Here entity is already in second level cache so no database query will be hit
  19. department = (DepartmentEntity) anotherSession.load(DepartmentEntity.class, new Integer(1));
  20. System.out.println(department.getName());
  21. anotherSession.getTransaction().commit();
  22. anotherSession.close();
  23. }
  24. finally
  25. {
  26. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getEntityFetchCount()); //Prints 1
  27. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()); //Prints 1
  28. HibernateUtil.shutdown();
  29. }
  30. Output in console:
  31. Hibernate: insert into DEPARTMENT (NAME) values (?)
  32. Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=?
  33. Human Resource
  34. Human Resource
  35. Human Resource
  36. 1
  37. 1

您将一次又一次地获得此输出,因为每次关闭 Hiberate 时,都会刷新内存并在下次运行时构建二级缓存。

物理缓存示例

如果您要构建的缓存非常大,则最好在文件系统中构建它(例如 Windows 中的 C 驱动器)。 这将防止 Hiberate 在每次重新启动应用时建立高速缓存。 同样,二级缓存提取规则仍然适用。

要启用物理缓存,请下载oscache.properties文件,并取消注释以下几行:

  1. cache.memory=false
  2. cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
  3. cache.path=c:/temp/cache
  4. cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache

让我们看一下测试代码示例:

  1. try
  2. {
  3. //Open the hibernate session
  4. Session session = HibernateUtil.getSessionFactory().openSession();
  5. session.beginTransaction();
  6. //fetch the department entity from database first time
  7. DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
  8. System.out.println(department.getName());
  9. //fetch the department entity again; Fetched from first level cache
  10. department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
  11. System.out.println(department.getName());
  12. //Let's close the session
  13. session.getTransaction().commit();
  14. session.close();
  15. //Try to get department in new session
  16. Session anotherSession = HibernateUtil.getSessionFactory().openSession();
  17. anotherSession.beginTransaction();
  18. //Here entity is already in second level cache so no database query will be hit
  19. department = (DepartmentEntity) anotherSession.load(DepartmentEntity.class, new Integer(1));
  20. System.out.println(department.getName());
  21. anotherSession.getTransaction().commit();
  22. anotherSession.close();
  23. }
  24. finally
  25. {
  26. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getEntityFetchCount()); //Prints 1
  27. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()); //Prints 1
  28. HibernateUtil.shutdown();
  29. }
  30. Output in console:
  31. Hibernate: insert into DEPARTMENT (NAME) values (?)
  32. Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=?
  33. Human Resource
  34. Human Resource
  35. Human Resource
  36. 1
  37. 1

上面的代码将在“c:/tempcache”位置创建物理缓存。

Hibernate OSCache 配置示例教程 - 图1

下次再次运行示例代码时,将获得以下输出:

  1. Hibernate: insert into DEPARTMENT (NAME) values (?)
  2. Human Resource
  3. Human Resource
  4. Human Resource
  5. 0
  6. 2

将部门实体存储在物理二级缓存中并从那里获取的原因很简单。 因此 Hiberate 将不会再次进入数据库。

要下载该项目的源代码,请遵循给定的链接。

祝您学习愉快!