【问题标题】:How to convert Java Hashmap Hierarchy to its equivalent in Redis Cache?如何将 Java Hashmap Hierarchy 转换为 Redis Cache 中的等价物?
【发布时间】:2020-04-11 17:43:20
【问题描述】:

我有以下代表作业运行的作业类。我在 Job 类中有一个开始和结束时间的列表,因为可以重新运行相同的 Job。

public class Job {

      private final List<Timestamp> jobStartTimes = new SortedList<>();
      private final List<Timestamp> jobEndTimes = new SortedList<>();
      private String jobName;
      private String jobKey;
      private String host;
      ....
      ....
}

我有这张地图用于查询给定 jobkey 的工作。

public class JobMap {

         /**
         * Here value of 'String' key is jobKey
         */
         private final Map<String, Job> jobCache;
}

我还创建了以下哈希映射层次结构,用于在 Map 中存储 (starttime, jobKey) 和 (endtime, jobkey) 条目,以便我可以更快地检索作业记录。这是必需的,因为我的查询是基于时间戳的,例如:返回在 x 和 y 时间戳之间运行的所有作业。

public class YearCache<T> {

        /**
         * Here value of 'Integer' key is month number (0, 11)
         */
        private final Map<Integer, MonthCache> monthCache;
}

public class MonthCache {

        /**
         * Here value of 'Integer' key is week number in a month(0, 4)
         */
        private final Map<Integer, WeekCache> weekCache;
}

public class WeekCache {

        /**
         * Here value of 'Integer' key is day number in a week (0, 6)
         */
        private final Map<Integer, DayCache> dayCache;
}

private class DayCache
{
        /**
         * Here value of 'Integer' key is hour value in a day (0, 23)
         * T is of type String representing jobKey
         */
         private final NavigableMap<Integer, NavigableMap<Timestamp, Set<T>>> hourCache;
}

我想摆脱这个 Java 哈希映射并转移到 Redis 缓存。如何在 Redis 缓存中建模此层次结构?

【问题讨论】:

    标签: java redis memcached


    【解决方案1】:

    这是必需的,因为我的查询是基于时间戳的,例如:返回在 x 和 y 时间戳之间运行的所有作业。

    看看 Redis Time Series Patterns 中的 Sorted Set Time Series Pattern。详情请见here。您可能需要稍微调整示例以满足您的需要。文档摘录:

    +---------------+-------------+
    | Timestamp     | Temperature |
    +---------------+-------------+
    | 1511533205001 | 21          |
    +---------------+-------------+
    | 1511533206001 | 22          |
    +---------------+-------------+
    

    你可以这样查询:

    > ZADD temperature 1511533205001 21
    (integer) 1
    > ZADD temperature 1511533206001 22
    (integer) 1
    > ZRANGEBYSCORE temperature -inf +inf WITHSCORES
    1) "22"
    2) "1511533206001"
    3) "21"
    4) "1511533207001"
    

    这是一个简单的例子。该链接讨论了此方法的扩展以处理特定的极端情况。

    对于您的情况,假设您有 jobStartTimejobEndTime,您可能必须为 jobStartsjobEnds 使用单独的集合,这些集合由作业开始/结束的时间戳键入。那么:

    x 表示作业的开始,y 表示作业的结束

    1. jobStarts集合中查询x之后启动的所有作业
    2. jobsEnds集合中查询在y之前完成但在x之后开始的所有作业
    3. 第 1 步和第 2 步结果中的重复值产生在x 之后开始并在y 之前结束的作业

    在您的代码中:

    public class Job {
    
          private final List<Timestamp> jobStartTimes = new SortedList<>();
          private final List<Timestamp> jobEndTimes = new SortedList<>();
          ....
          private String jobKey;
          ....
    }
    

    您将所有运行存储为作业的一部分。最好将作业与作业分开运行,以便作业详细信息可以单独缓存在名为jobs 的集合中。那么集合jobStartsjobEnds的值可以作为jobKey,你可以使用它从jobs集合中查找详细信息,该集合分别存储了job的详细信息。

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      相关资源
      最近更新 更多