Java 如何在Dropwizard中实现Guava缓存?

Java 如何在Dropwizard中实现Guava缓存?,java,spring,caching,guava,dropwizard,Java,Spring,Caching,Guava,Dropwizard,我正在尝试使用guava设置缓存,代码如下: private List<Profile> buildCache() { LoadingCache cache = CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(40) .build(

我正在尝试使用guava设置缓存,代码如下:

private List<Profile> buildCache() {
        LoadingCache cache = CacheBuilder.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .maximumSize(40)
                .build(
                        new CacheLoader<Profile, List<Profile>>() {
                            @Override
                            public List<Profile> load(Profile profile) throws Exception {
                                Profile profile1 = new Profile();
                                Profile.setEmployed(true);
                                return profileDAO.getAllProfiles(Profile1, null);
                            }
                        }
                );
        return (List<Profile>) cache;
    }


public List<Profile> getAllProfiles(Profile profile, Integer size) throws Exception {
        return profileDAO.getAllProfiles(profile, size);
    }

如果有帮助的话,我的应用程序也在使用spring,所以我也一直在对此进行研究。springframework.cache.guava和google.common.cache之间有什么区别吗?还是它只是Spring内置的guava缓存?

好的,我想我已经找到了答案:

private LoadingCache<Integer, List<Profile>> loadingCache = CacheBuilder.newBuilder()
            .refreshAfterWrite(10,TimeUnit.MINUTES)
            .maximumSize(100).build(
            new CacheLoader<Integer, List<Profile>>() {
                @Override
                public List<Profile> load(Integer integer) throws Exception {
                    Profile profile= new Profile();
                    if (integer == null) {
                        integer = 10;
                    }
                    return profileDAO.getAllProfiles(profile, integer);
                }
            }
    );
private LoadingCache LoadingCache=CacheBuilder.newBuilder()
.refreshAfterWrite(10,时间单位:分钟)
.最大尺寸(100)。构建(
新缓存加载程序(){
@凌驾
公共列表加载(整数)引发异常{
配置文件=新配置文件();
if(整数==null){
整数=10;
}
返回profileDAO.getAllProfiles(profile,整数);
}
}
);
首先,我应该指定传递到LoadingCache的键和值,在本例中是一个整数和一个概要文件列表。另外,当我在build函数中声明新的CacheLoader时,我应该保留key和value的布局。最后,当调用getAll方法时,我应该使用key Integer而不是profile对象加载

至于调用函数:

public List<Profile> getAllProfiles(Profile profile, Integer size) throws Exception {
        return loadingCache.get(size);
    }
public List getAllProfiles(配置文件配置文件,整数大小)引发异常{
返回loadingCache.get(大小);
}
这用于获取存储在缓存中的特定长度的列表。如果该长度的列表不在缓存中,则将使用传递给它的size变量运行getAll方法


@尤金,谢谢你的帮助。您对加载方法的解释确实帮助我将缓存置于透视图中。

您的
配置文件是否覆盖了核心对象中的hashCode/equals?是的。我不明白这是怎么开始的,你想从其他配置文件中缓存一些配置文件(
列表
)?您可能只想缓存活动配置文件?这正是我要做的(缓存前10个活动配置文件)。但是,正如我所说的,我真的不知道如何正确地实现它。这样考虑一下——假设您想基于firstname查询一些用户,那么您的缓存是
String,User
load
方法在
String
不在缓存中并且以后缓存时被调用;在第二次呼叫时,将不会调用您的负载。因此,基本上您只需执行
缓存。get(String firstName)
-其余操作将由缓存完成
private LoadingCache<Integer, List<Profile>> loadingCache = CacheBuilder.newBuilder()
            .refreshAfterWrite(10,TimeUnit.MINUTES)
            .maximumSize(100).build(
            new CacheLoader<Integer, List<Profile>>() {
                @Override
                public List<Profile> load(Integer integer) throws Exception {
                    Profile profile= new Profile();
                    if (integer == null) {
                        integer = 10;
                    }
                    return profileDAO.getAllProfiles(profile, integer);
                }
            }
    );
public List<Profile> getAllProfiles(Profile profile, Integer size) throws Exception {
        return loadingCache.get(size);
    }