java - 如何在 Dropwizard 中实现 Guava 缓存?

标签 java spring caching guava dropwizard

我正在尝试使用 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);
    }

这里的想法是,这将使用获取所有配置文件创建一个缓存。该方法使用新的配置文件对象来设置该员工是否受雇的 boolean 值。大小变量意味着该方法将返回指定的数量。当为空时,默认为前 10 名。

我有两个问题: 1.这是我第一次使用缓存,所以我真的不知道我这样做是否正确。 2. 我在文档中找不到有关如何在我的应用程序中实现此功能的任何内容。我该怎么称呼这个?我尝试修改 getAllProfiles 方法以返回它:

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

但这只是返回一个异常,我无法将缓存转换为 java 列表:

Exception occurred: java.lang.ClassCastException: com.google.common.cache.LocalCache$LocalLoadingCache cannot be cast to java.util.List

如果有帮助的话,我的应用程序也在使用 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);
                }
            }
    );

首先,我应该指定传递到 LoadingCache 的键和值,在本例中是一个整数和一个配置文件列表。另外,当我在构建函数中声明新的 CacheLoader 时,我应该保留键和值的布局。最后,当调用 getAll 方法时,我应该使用键 Integer 加载,而不是配置文件对象。

至于调用函数:

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

这用于获取存储在缓存中的某些长度的列表。如果该长度的列表不在缓存中,则 getAll 方法将使用您传递给它的大小变量运行。

@Eugene,谢谢您的帮助。您对加载方法的解释确实帮助我了解了缓存。

关于java - 如何在 Dropwizard 中实现 Guava 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372539/

相关文章:

Javascript - 缓存和 cookie 问题

java - 如何在露天访问 session 工厂(数据源)?

spring - 何时使用@Pointcut & @Before、@After AOP 注解

android - 清除 AdMob 在 Android 中生成的缓存文件是一种好习惯吗?

azure - 在 Service Fabric 集群上发布应用程序后 HttpContext.Session 为空

spring - @ModelAttribute 总是将 Boolean 映射为 false

java - 如何使用 java.nio.Files 创建软符号链接(symbolic link)

java - Couchbase、Java SDK 中更新大量文档的每种方式的优点/缺点是什么?

java - 如何使用 setter 和 getter 方法获取对象的对象数组及其响应

java - jackson 的@JsonIdentityInfo 广度优先