Spring boot cache usage

1 first introduced configurations:

<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

  

 

2 cache and then began to use 

  Add comments on startup class: @EnableCaching

 

3 cache method you want to use the above plus @Cacheable (value = "Cache name", key = "# p1")

    Remarks key = "# p1", it represents the only sign to do with the cache parameters p1

 

 

 

 

 

 

About 2 cache ehche and redis

 

1 If you simply need a cache, do not need to share multi-section, does not require long-lasting change is required, then the use of ehcahe like Java and embedded cache is appropriate.

If multiple nodes share 2, data format, lasting change requirements are relatively high. You can choose a stand-alone tool cache redis like.

 

redis: Configure and develop cache is redis redis (Note, redis do cache @Cacheable which must specify the cache name, ehcache because the configuration file specifies the default cache, so you can not specify)

 

Note 1: You can specify an expiration time, but this is the number of milliseconds

Note 2: You can specify a prefix unified cache, but this @Cacheable inside the specified cache name is invalid, does not recommend doing so, the default will be used @Cacheable the value :: key format, which specifies the configuration of a unified prefix is ​​the prefix :: key format, the cache may cause confusion

 

spring:
  cache:
    type: redis

  redis:
    host: 127.0.0.1
    port: 9736
    database: 0
    password: rds123456

Note: The need to introduce the jar package redis

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

  

 

 

ehcache configuration:

  cache:
    ehcache:
      config: classpath:ehcache.xml

  

ehcache profile corresponding to the following clasPath hand.

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 
    <diskStore path="java.io.tmpdir"/>
 
  <!--defaultCache:echcache的默认缓存策略  -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <cache name="keyStore" 
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="86400"
            timeToLiveSeconds="86400"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

  

 

 

Note: The need to introduce ehcache jar package

<dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/cxygg/p/12160842.html