如何在Dropwizard中实现Guava缓存?

编程入门 行业动态 更新时间:2024-10-10 02:23:02
本文介绍了如何在Dropwizard中实现Guava缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用以下代码使用番石榴来设置缓存:

I'm trying to setup a cache using guava, with the following code:

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); }

此处的想法是,这将使用获取所有配置文件创建缓存。用于该方法的方法使用新的配置文件对象设置是否雇用该雇员的布尔值。 size变量表示该方法将返回任何指示的值。如果为null,则默认为前10个。

The idea here is that this will create a cache using get all profile. The method for that uses a new profile object to set a boolean on whether that employee is employed or not. The size variable means that the method will return however many indicated. When null, it defaults to top 10.

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

I have two issues: 1. This is the first time I have ever used a cache, so I really do not know if I am doing this correctly. 2. I cannot find anything in the documentation on how to implement this within my app. How am I supposed to call this? I tried modifying the getAllProfiles method to return it:

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

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

But that simply returns an exception that I cannot cast the cache into a java list:

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

如果有帮助,我的应用程序也使用spring,因此我也一直在对此进行研究。 springframework.cache.guava和googlemon.cache之间有什么区别,还是仅仅是Spring内置的番石榴缓存?

If its any help, my app is also using spring, so I've also been doing research into that. Is there any difference between springframework.cache.guava and googlemon.cache, or is it just Spring's inbuilt guava cache?

推荐答案

好吧,我想我已经弄清楚了:

Ok, I think I managed to figure it out:

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的键和值,在本例中为Integer和配置文件列表。另外,当我在构建函数中声明新的CacheLoader时,我应该保留键和值的布局。最后,当调用getAll方法时,我应该已经使用键Integer而不是配置文件对象加载了。

First, I should have specified the key and value being passed into LoadingCache, in this case, an Integer and a List of Profile. Also, when I declared the new CacheLoader in the build function, I should have kept that layout of key and value. Finlly, when calling the getAll method, I should have loaded using the key Integer, not a profile object.

与调用函数一样:

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

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

This serves to get lists of certain legnths that are stored in the cache. If the list of that length is not in the cache, the getAll method will run, using the size varaible you pass to it.

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

@Eugene, Thank you for your help. Your explanation on the load method really helped put the cache into perspective for me.

更多推荐

如何在Dropwizard中实现Guava缓存?

本文发布于:2023-11-30 21:51:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   如何在   Dropwizard   Guava

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!