<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot ehcache 2 示例 > 原文: [https://howtodoinjava.com/spring-boot/ehcache2-config-example/](https://howtodoinjava.com/spring-boot/ehcache2-config-example/) 了解如何使用 **ehcache 2.x** 在 [spring boot](https://howtodoinjava.com/spring-boot-tutorials/) 應用程序中配置緩存。 學習使用基于**注解的緩存配置**以及使用[`CacheManager`](https://docs.spring.io/autorepo/docs/spring-framework/4.1.8.RELEASE/javadoc-api/org/springframework/cache/CacheManager.html)手動更新緩存。 ## 1\. Maven 依賴 在此示例中,我們使用的是 Spring 運行版本`1.5.13.RELEASE`。 較早的 spring boot 版本支持`net.sf.ehcache`軟件包下的`ehcache 2.x`。 我們需要以下依賴項來添加緩存功能。 * `spring-boot-starter-cache` * **ehcache**(`net.sf.ehcache`) * **緩存 API**(`javax.cache`) `pom.xml` ```java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.company</groupId> <artifactId>SpringBootEhcache</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootEhcache</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` ## 2\. `ehcache.xml` Spring Boot 會自動檢測`classpath`中是否存在`ehcache.xml`并對其進行配置。 在這里,我們提供緩存名稱,到期時間等。 在[ ehcache 文檔](https://www.ehcache.org/documentation/2.8/configuration/configuration.html#dynamically-changing-cache-configuration)中找到屬性的完整列表。 `ehcache.xml` ```java <ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="2000" eternal="true" overflowToDisk="false" timeToLiveSeconds="1200" /> <cache name="employeeCache" maxElementsInMemory="2000" eternal="false" overflowToDisk="false" timeToLiveSeconds="10000" /> </ehcache> ``` ## 3\. `@EnableCaching` 它啟用 Spring 的注解驅動的緩存管理功能,并在調用`@Cacheable`注解方法時啟用對攔截器的支持。 `CacheConfig.java` ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { } ``` ## 4\. `@Cacheable`注解 要緩存從方法調用返回的數據,我們可以在方法上使用[`@Cacheable`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html)注解。 使用其屬性`cacheNames`和`key`來指代緩存和緩存條目的鍵屬性。 `EmployeeManager.java` ```java import java.util.HashMap; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class EmployeeManager { static HashMap<Long, Employee> db = new HashMap<>(); static { db.put(1L, new Employee(1L, "Alex", "Gussin")); db.put(2L, new Employee(2L, "Brian", "Schultz")); } @Cacheable(cacheNames="employeeCache", key="#id") public Employee getEmployeeById(Long id) { System.out.println("Getting employee from DB"); return db.get(id); } } ``` ## 5\. `CacheManager` API 有時,在使用注解似乎不是完美解決方案的情況下,我們可能需要使用緩存。 在這種情況下,我們可以使用`org.springframework.cache.CacheManager`和`org.springframework.cache.Cache`抽象來訪問并利用 ehcache 添加和訪問緩存條目。 要使用`CacheManager`,我們必須首先自動裝配到 spring 組件中,并使用它的`getCache(name)`方法來按名稱獲取緩存實例。 訪問緩存后,我們可以使用其`get()`和`put()`方法來添加和訪問緩存條目。 ```java //1 @Autowired private CacheManager cacheManager; //2 Cache cache = cacheManager.getCache("myCache"); cache.put(3L, "Hello"); String value = cache.get(3L).get(); ``` ## 6\. Spring boot ehcache 2 示例 – 演示 我正在創建一個模型類`Employee`,其實例將被緩存。 `Employee.java` ```java import java.io.Serializable; public class Employee implements Serializable { private static final long serialVersionUID = 5517244812959569947L; private Long id; private String firstName; private String lastName; public Employee() { super(); } public Employee(Long id, String firstName, String lastName) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; } //Getters and setters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; } } ``` 現在在演示應用程序類中,我正在使用`@Cacheable`注解測試基于注解的緩存訪問,并使用`CacheManager`進行手動緩存訪問。 `Application.java` ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { @Autowired private CacheManager cacheManager; @Autowired private EmployeeManager employeeManager; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { //This will hit the database System.out.println(employeeManager.getEmployeeById(1L)); //This will hit the cache - verify the message in console output System.out.println(employeeManager.getEmployeeById(1L)); //Access cache instance by name Cache cache = cacheManager.getCache("employeeCache"); //Add entry to cache cache.put(3L, "Hello"); //Get entry from cache System.out.println(cache.get(3L).get()); }; } } ``` 程序輸出。 `Console` ```java Getting employee from DB Employee [id=1, firstName=Alex, lastName=Gussin] Employee [id=1, firstName=Alex, lastName=Gussin] //Fetched from cache Hello ``` 請在評論中使用 ehcache 將有關此 **spring boot** 緩存示例的問題交給我。 學習愉快! [下載源碼](https://howtodoinjava.com/wp-content/downloads/SpringBootCacheDemo-ehcache2.zip)
                  <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>

                              哎呀哎呀视频在线观看