springboot integration GuavaCache cache

   The main role of the cache is to temporarily store the results of data processing business system in memory, and wait for the next visit to use. For those who frequently need to query the hot data comparison, we used to use caching.

   GuavaCache is produced google cache memory tool. For smaller amounts of data, several dozens of data, but also less need to add cache interface, we recommend using guava Cache Google offers, it is easy to use at the same time and performs well. And thread safe.

   GuavaCache and Map of similar use, put and get to store and retrieve key values, you can set the survival time.

    If tens of millions of pieces of data stored or distributed cache selection redis.

     

pom.xml configuration file:

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

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

 

java code:

/**
 * Copyright (c) 2020, All Rights Reserved.
 *
*/

package com.demo.server.config;

import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.cache.CacheBuilder;


@Configuration
@EnableCaching
public class GuavaCacheConfig {
    
    @Bean
    public the CacheManager CacheManager () { 
        GuavaCacheManager CacheManager = new new GuavaCacheManager (); 
        cacheManager.setCacheBuilder ( 
            . CacheBuilder.newBuilder () 
            expireAfterWrite ( . 10, TimeUnit.SECONDS) // survival time of 10 seconds 
            maximumSize (1000));      // Maximum number of threads 
        return CacheManager; 
    } 

}

 

Guess you like

Origin www.cnblogs.com/chong-zuo3322/p/12319639.html