如何将 Java Hashmap Hierarchy 转换为 Redis 缓存中的等价物?
How to convert Java Hashmap Hierarchy to its equivalent in Redis Cache?
我有以下作业 class 表示作业的运行。我在 Job class 中有一个开始和结束时间列表,因为可以重新运行同一个 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;
....
....
}
我有这个 Map 用于查询给定 jobkey 的作业。
public class JobMap {
/**
* Here value of 'String' key is jobKey
*/
private final Map<String, Job> jobCache;
}
我还创建了以下哈希图层次结构,用于在 Map 中存储 (starttime, jobKey) 和 (endtime, jobkey) 条目,以便我可以更快地检索工作记录。这是必需的,因为我的查询是基于时间戳的,例如:return 所有 运行 在 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 缓存中为这个层次结构建模?
This is needed because my queries are timestamp based, for ex: return all jobs that ran between x and y timestamp.
查看 Redis 时间序列模式中的排序集时间序列模式。可以找到详细信息 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"
这是一个简单的例子。 link 讨论了对这种方法的扩展以处理特定的极端情况。
对于您的情况,假设您有 jobStartTime
和 jobEndTime
,您可能必须为 jobStarts
和 jobEnds
使用单独的集合这份工作 started/ended。那么:
令 x
表示开始,y
表示作业结束
- 从
jobStarts
集合 查询x
之后开始的所有作业
- 从
jobsEnds
set 查询所有在y
之前完成但在x
之后开始的作业
- 重复第 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
的集合中。然后集合 jobStarts
和 jobEnds
的值可以是 jobKey,您可以使用它从单独存储作业详细信息的 jobs
集合中查找详细信息。
我有以下作业 class 表示作业的运行。我在 Job class 中有一个开始和结束时间列表,因为可以重新运行同一个 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;
....
....
}
我有这个 Map 用于查询给定 jobkey 的作业。
public class JobMap {
/**
* Here value of 'String' key is jobKey
*/
private final Map<String, Job> jobCache;
}
我还创建了以下哈希图层次结构,用于在 Map 中存储 (starttime, jobKey) 和 (endtime, jobkey) 条目,以便我可以更快地检索工作记录。这是必需的,因为我的查询是基于时间戳的,例如:return 所有 运行 在 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 缓存中为这个层次结构建模?
This is needed because my queries are timestamp based, for ex: return all jobs that ran between x and y timestamp.
查看 Redis 时间序列模式中的排序集时间序列模式。可以找到详细信息 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"
这是一个简单的例子。 link 讨论了对这种方法的扩展以处理特定的极端情况。
对于您的情况,假设您有 jobStartTime
和 jobEndTime
,您可能必须为 jobStarts
和 jobEnds
使用单独的集合这份工作 started/ended。那么:
令 x
表示开始,y
表示作业结束
- 从
jobStarts
集合 查询 - 从
jobsEnds
set 查询所有在 - 重复第 1 步和第 2 步结果的值会产生在
x
之后开始并在y
之前结束的作业
x
之后开始的所有作业
y
之前完成但在x
之后开始的作业
在您的代码中:
public class Job {
private final List<Timestamp> jobStartTimes = new SortedList<>();
private final List<Timestamp> jobEndTimes = new SortedList<>();
....
private String jobKey;
....
}
您正在将所有运行存储为作业的一部分。最好将作业运行与作业分开,以便可以将作业详细信息单独缓存在名为 jobs
的集合中。然后集合 jobStarts
和 jobEnds
的值可以是 jobKey,您可以使用它从单独存储作业详细信息的 jobs
集合中查找详细信息。