首页 > 解决方案 > 是否可以查询 Hazelcast 缓存?如果是,该怎么做?

问题描述

我正在尝试使用 hazelcast 实现缓存。

这是我的代码。我的问题是,当 findAllGames() 执行时,我将所有游戏缓存在“gamesCache”中,而当 findGameByTypes() 执行时,我希望它查询“gamesCache”而不是访问数据库并返回结果。

@Cacheable(cacheNames = "gamesCache", key = "#root.methodName")
public List<Game> findAllGames() {
    List<Game>  games = gamesDao.getAllGames(); // dao call
    //some database call
}

public List<Game> findGameByTypes(GameType gameType) {
    List<Game> games = gamesDao.getGamesByType(gameType); // dao call
    //some logic
}

public class Game implements Serializable {
    private long gameId;
    private String gameName;
    private GameType gameType;
}

public class GameType implements Serializable {
   private long gameId;
   private String gameGenre;
   private Boolean status;
}

findAllGames() 总是比 findGamesByTypes() 先命中。

现在缓存的地图是用“findAllGames”作为键和游戏列表作为值生成的。现在有没有办法使用 GameType 属性作为标准来查询地图。

有什么方法可以实现吗?我也愿意接受其他建议。

标签: javaspringcachinghazelcasthazelcast-imap

解决方案


您可以使用 Predicate 或 SqlQuery 查询 Hazelcast Map/Cache。在此处查看详细文档:https ://docs.hazelcast.org/docs/3.11/manual/html-single/index.html#distributed-query


推荐阅读