How do I implement Guava Caching in Dropwizard?

旧巷老猫 提交于 2019-12-13 01:39:12

问题


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

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.

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

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

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

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 google.common.cache, or is it just Spring's inbuilt guava cache?


回答1:


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

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.

As for calling the function:

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

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, Thank you for your help. Your explanation on the load method really helped put the cache into perspective for me.



来源:https://stackoverflow.com/questions/50372539/how-do-i-implement-guava-caching-in-dropwizard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!