<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??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 將不會再次進入數據庫。 要下載該項目的源代碼,請遵循給定的鏈接。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看