java - Guava - 缓存一张表并在该缓存上使用 get 方法

标签 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() 创建的缓存。所有三种方法都将使用这一缓存来工作。

我想我可以重用loadingCache2的代码来首先创建缓存:

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 语句将是“SELECT * FROM Profiles”。问题将来自其他方法;考虑到不同的输入要求,我不知道如何将这些方法指向该缓存。以前有人做过类似的事情吗?

编辑:

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

最佳答案

我认为缓存概念存在一些误用。仅当找不到元素时,缓存才会加载,因此通过创建一个以列表为键的缓存,您将取消缓存概念(因为列表是一个对象,除非您传递相同的列表,否则您将永远不会重用该缓存)因此总是从数据库加载值而不使用缓存)。我认为你只需要使用一个缓存,使用 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 替换为与您所做的类似的方法 - 让缓存加载具有数据库中类似字段的配置文件。最后,请注意,像这样从缓存加载会产生别名,因此您需要小心对数据所做的操作。

希望这很清楚并且有帮助。

关于java - Guava - 缓存一张表并在该缓存上使用 get 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50611400/

相关文章:

java - Mongodb:从 mongo 集合中获取特定列值

java - 如何避免上课自用

c++ - 除了memcached,C++还有成熟的分布式缓存解决方案吗?

Angularjs - IE9 缓存行为

java - Java SettableFuture 和 ListenableFuture 的 C# 等价物是什么?

java - 为什么Guava InputSupplier/OutputSupplier没有扩展Supplier接口(interface)?

java - Guava - 如何根据谓词从列表中删除,跟踪删除的内容?

java - 读取 args 并将所有字母转换为大写字母的程序,无需使用 toUpperCase()

ios - 如何使用 Kingfisher(或 AlamofireImage)手动设置缓存中图像的最大期限并清除过期项目?

java - 基于内容估计文本宽度的算法