Discuss / Java / 使用Guava

使用Guava

Topic source

## 需要的maven依赖

		<dependency>
  			<groupId>com.google.guava</groupId>
  			<artifactId>guava</artifactId>
  			<version>30.1.1-jre</version>
		</dependency>

### GuavaSetting代码

package com.itranswarp.learnjava.templatemethod;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.Cache;

/*
    Guava Cache:https://github.com/google/guava/wiki/CachesExplained
    Cache Pattern: if cached, return; otherwise create, cache and return
*/

public class GuavaSetting extends AbstractSetting{
    private Cache<String, String> cache = CacheBuilder.newBuilder()
                .maximumSize(1000)            
                .build();

    @Override
    protected String lookupCache(String key) {
        return cache.getIfPresent(key);
    }

    @Override
    protected void putIntoCache(String key, String value) {
        cache.put(key, value);   
    }  
}

  • 1

Reply