Guava - 缓存表并在一个缓存上使用get方法

时间:2018-05-30 18:35:26

标签: java caching guava google-guava-cache

我之前在我的应用程序中实现了缓存,可以与三个单独的get方法一起使用。这些获取方法为getAllProfiles()getProfilesByID()getProfileByFields()。因此,我的代码如下所示:

private LoadingCache<int[], List<Profile>> loadingCache = CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .maximumSize(100).build(
                    new CacheLoader<int[] ids, List<Profile>>() {
                        @Override
                        public List load(int[] ids) throws Exception {
                            return profileDAO.getProfilesById(ids);
                        }
                    }
            );

    private LoadingCache<Integer, List<Profile>> loadingCache2 = CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .maximumSize(100).build(
                    new CacheLoader<Integer, List<Profile>>() {

                        @Override
                        public List<Profile> load(Integer size) throws Exception {
                            return profileDAO.getAllProfiles(size);
                        }
                    }
            );

    private LoadingCache<Profile, List<Profile>> loadingCache3 = CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .maximumSize(100).build(
                    new CacheLoader<Profile, List<Profile>>() {
                        @Override
                        public List<Profile> load(Profile profile) throws Exception {
                            return profileDAO.getProfileByFields(profile);
                        }
                    }
            );

public ProfileManagerImpl(ProfileDAO profileDAO) {
        this.profileDAO = profileDAO;
    }


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

public List<Profile> getProfilesById(int[] idArray) throws Exception {
        return loadingCache.get(idArray);
    }

public List<Profile> getProfileByFields(Profile profile) throws Exception {
        return loadingCache3.get(profile);
    }

为了简化我的工作,我需要为整个表创建一个在启动时使用getAllProfiles()创建的缓存。然后,所有这三种方法都将使用这一个缓存来处理。

我想我可以在第一时间重用loadCache2的代码来创建缓存:

private LoadingCache<Integer, List<Profile>> loadingCache2 = CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .maximumSize(100).build(
                    new CacheLoader<Integer, List<Profile>>() {

                        @Override
                        public List<Profile> load(Integer size) throws Exception {
                            return profileDAO.getAllProfiles(size);
                        }
                    }
            );

并传入null作为大小,因此DAO上的SQL语句将是&#39; SELECT * FROM Profiles&#39;。问题将来自其他方法;鉴于不同的输入要求,我不知道如何将这些方法指向此缓存。以前有人做过这样的事吗?

编辑:

正如Louis Wasserman所建议的那样,我制作了一个将Object作为通用密钥的单个Cache对象。从那里,服务应该使用if语句来检测输入对象类型,并使用适当的方法来检索缓存的内容,具体取决于使用的方法。

截至目前,它在getAllProfiles上失败,并且出现空指针异常,所以我需要弄明白。

根据以下代码,我看起来是否在正确的轨道上?我使用Cache而不是LoadingCache用于此对象:

public Cache<Object, List<Profile>> cache =
            CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterWrite(5, TimeUnit.MINUTES)
            .build(new CacheLoader<Object, List<Profile>>() {
                @Override
                public List<Profile> load(Object k) throws Exception {
                    if (k instanceof Integer) {
                        return profileDAO.getAllProfiles((Integer) k);
                    }
                    else if (k instanceof int[]) {
                        return profileDAO.getMultipleProfiles((int[]) k);
                    }
                    else if (k instanceof Profile)
                        return profileDAO.getProfileByFields((Profile) k);
                }
            });



public List<Profile> getAllProfiles(Integer size) throws Exception {
        return cache.getIfPresent(size);
    }

1 个答案:

答案 0 :(得分:0)

我认为有些滥用缓存概念。只有在找不到元素时才会加载缓存,因此通过创建缓存,密钥是一个列表,您将使缓存概念无效(因为列表是一个对象,除非您传递相同的内容)列表,您永远不会重复使用值,因此始终从数据库加载而不使用缓存)。我认为你只需要使用一个缓存,使用id作为密钥(如@Ben Manes建议的那样),并用辅助方法包装它(此代码未经过测试,因此请小心)。

/** This is whatever class you intended to put the cache in anyways */
public class CacheWrapper {
    private LoadingCache<Integer, Profile> loadingCache;

    public CacheWrapper(/* whatever arguments the class needs*/) {
        loadingCache = CacheBuilder.newBuilder()
            .refreshAfterWrite(5, TimeUnit.MINUTES)
            .expireAfterAccess(5, TimeUnit.MINUTES)
            .maximumSize(100).build(
                new CacheLoader<Integer, Profile>() {
                    @Override
                    public List<Profile> load(Integer id) throws Exception {
                        return profileDAO.getProfileById(id);
                    }
                }
            );

         // init the cache here
         for (Profile profile : profileDAO.getAllProfiles(null)) {
             loadingCache.put(profile.getId(), profile);
         }    

         /* other construction logic here */
    }

    public List<Profile> getAllProfiles() throws Exception {
        return new ArrayList(loadingCache.asMap().values());
    }

    public List<Profile> getProfilesById(int[] idArray) throws Exception {
        List<Profile> profiles = new ArrayList(idArray.length());

        for(int i = 0; i < idArray.length(); i++) {
            profiles.add(loadingCache.get(idArray[i]));
        }

        return profiles;
    }

    public List<Profile> getProfileByFields(Profile profile) throws Exception {
        List<Profile> profiles = new ArrayList(idArray.length());

        for (Profile cachedProfile : loadingCache.asMap().values()) {
            if (profile.hasEqualFields(cachedProfile)) {
                profiles.add(cachedProfile);
            }
        }

        return profiles;
    }
}

您还可以通过构建以Profile为键的第二个缓存以及匹配的配置文件列表作为值来进一步优化此功能,然后您可以用类似的方法替换getProfileByFields。 - 使缓存加载Profiles具有来自数据库的类似字段。最后,请注意从缓存加载这样会产生别名,因此您需要小心对数据所做的操作。

希望这是明确的,无论如何都有帮助。

相关问题