在固定超时内缓存单个对象的最佳方法是什么?

时间:2022-06-19 20:46:15

Google Guava has CacheBuilder that allows to create ConcurrentHash with expiring keys that allow to remove entries after the fixed tiemout. However I need to cache only one instance of certain type.

Google Guava具有CacheBuilder,允许使用过期密钥创建ConcurrentHash,允许在固定tiemout之后删除条目。但是我只需要缓存某种类型的一个实例。

What is the best way to cache single object within fixed timeout using Google Guava?

使用Google Guava在固定超时内缓存单个对象的最佳方法是什么?

1 个解决方案

#1


85  

I'd use Guava's Suppliers.memoizeWithExpiration(Supplier delegate, long duration, TimeUnit unit)

我将使用Guava的Suppliers.memoizeWithExpiration(供应商代表,持续时间长,TimeUnit单位)

public class JdkVersionService {

    @Inject
    private JdkVersionWebService jdkVersionWebService;

    // No need to check too often. Once a year will be good :) 
    private final Supplier<JdkVersion> latestJdkVersionCache
            = Suppliers.memoizeWithExpiration(jdkVersionSupplier(), 365, TimeUnit.DAYS);


    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }

    private Supplier<JdkVersion> jdkVersionSupplier() {
        return new Supplier<JdkVersion>() {
            public JdkVersion get() {
                return jdkVersionWebService.checkLatestJdkVersion();
            }
        };
    }
}

Update with JDK 8

Today, I would write this code differently, using JDK 8 method references and constructor injection for cleaner code:

今天,我将以不同的方式编写此代码,使用JDK 8方法引用和构造函数注入来获得更清晰的代码:

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.google.common.base.Suppliers;

@Service
public class JdkVersionService {

    private final Supplier<JdkVersion> latestJdkVersionCache;

    @Inject
    public JdkVersionService(JdkVersionWebService jdkVersionWebService) {
        this.latestJdkVersionCache = Suppliers.memoizeWithExpiration(
                jdkVersionWebService::checkLatestJdkVersion,
                365, TimeUnit.DAYS
        );
    }

    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }
}

#1


85  

I'd use Guava's Suppliers.memoizeWithExpiration(Supplier delegate, long duration, TimeUnit unit)

我将使用Guava的Suppliers.memoizeWithExpiration(供应商代表,持续时间长,TimeUnit单位)

public class JdkVersionService {

    @Inject
    private JdkVersionWebService jdkVersionWebService;

    // No need to check too often. Once a year will be good :) 
    private final Supplier<JdkVersion> latestJdkVersionCache
            = Suppliers.memoizeWithExpiration(jdkVersionSupplier(), 365, TimeUnit.DAYS);


    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }

    private Supplier<JdkVersion> jdkVersionSupplier() {
        return new Supplier<JdkVersion>() {
            public JdkVersion get() {
                return jdkVersionWebService.checkLatestJdkVersion();
            }
        };
    }
}

Update with JDK 8

Today, I would write this code differently, using JDK 8 method references and constructor injection for cleaner code:

今天,我将以不同的方式编写此代码,使用JDK 8方法引用和构造函数注入来获得更清晰的代码:

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.google.common.base.Suppliers;

@Service
public class JdkVersionService {

    private final Supplier<JdkVersion> latestJdkVersionCache;

    @Inject
    public JdkVersionService(JdkVersionWebService jdkVersionWebService) {
        this.latestJdkVersionCache = Suppliers.memoizeWithExpiration(
                jdkVersionWebService::checkLatestJdkVersion,
                365, TimeUnit.DAYS
        );
    }

    public JdkVersion getLatestJdkVersion() {
        return latestJdkVersionCache.get();
    }
}