Hash Mapping

使用 Redis 中的各种数据结构可以存储数据。Jackson2JsonRedisSerializer 可以转换 JSON 格式的对象。理想情况下,可以通过使用普通键将 JSON 存储为值。你可以通过使用 Redis 哈希对结构化对象进行更精细的映射。Spring Data Redis 根据用例提供将数据映射到哈希的各种策略:

Data can be stored by using various data structures within Redis. Jackson2JsonRedisSerializer can convert objects in JSON format. Ideally, JSON can be stored as a value by using plain keys. You can achieve a more sophisticated mapping of structured objects by using Redis hashes. Spring Data Redis offers various strategies for mapping data to hashes (depending on the use case):

Hash Mappers

哈希映射器是将映射对象转换为 Map<K, V> 及其转换回映射对象的转换器。HashMapper 旨在与 Redis 哈希一起使用。

Hash mappers are converters of map objects to a Map<K, V> and back. HashMapper is intended for using with Redis Hashes.

有多种实现可用:

Multiple implementations are available:

以下示例展示了一种实现哈希映射的方法:

The following example shows one way to implement hash mapping:

public class Person {
  String firstname;
  String lastname;

  // …
}

public class HashMapping {

  @Resource(name = "redisTemplate")
  HashOperations<String, byte[], byte[]> hashOperations;

  HashMapper<Object, byte[], byte[]> mapper = new ObjectHashMapper();

  public void writeHash(String key, Person person) {

    Map<byte[], byte[]> mappedHash = mapper.toHash(person);
    hashOperations.putAll(key, mappedHash);
  }

  public Person loadHash(String key) {

    Map<byte[], byte[]> loadedHash = hashOperations.entries(key);
    return (Person) mapper.fromHash(loadedHash);
  }
}

Jackson2HashMapper

Jackson2HashMapper 通过使用 FasterXML Jackson 为域对象提供 Redis 哈希映射。Jackson2HashMapper 可将顶级属性映射为哈希字段名称,并可选择平铺结构。简单类型映射到简单值。复杂类型(嵌套对象、集合、映射等)表示为嵌套 JSON。

Jackson2HashMapper provides Redis Hash mapping for domain objects by using FasterXML Jackson. Jackson2HashMapper can map top-level properties as Hash field names and, optionally, flatten the structure. Simple types map to simple values. Complex types (nested objects, collections, maps, and so on) are represented as nested JSON.

展开为所有嵌套属性创建单独的哈希条目,并在可能的情况下将复杂类型解析为简单类型。

Flattening creates individual hash entries for all nested properties and resolves complex types into simple types, as far as possible.

考虑以下类及其包含的数据结构:

Consider the following class and the data structure it contains:

public class Person {
  String firstname;
  String lastname;
  Address address;
  Date date;
  LocalDateTime localDateTime;
}

public class Address {
  String city;
  String country;
}

下表显示前一个类中的数据在普通映射中将如何显示:

The following table shows how the data in the preceding class would appear in normal mapping:

Table 1. Normal Mapping
Hash Field Value

firstname

Jon

lastname

Snow

address

{ "city" : "Castle Black", "country" : "The North" }

date

1561543964015

localDateTime

2018-01-02T12:13:14

下表显示前一个类中的数据在平面映射中将如何显示:

The following table shows how the data in the preceding class would appear in flat mapping:

Table 2. Flat Mapping
Hash Field Value

firstname

Jon

lastname

Snow

address.city

Castle Black

address.country

The North

date

1561543964015

localDateTime

2018-01-02T12:13:14

拍平要求所有属性名不得干扰 JSON 路径。在将平铺用于键值对或属性名时,不支持使用圆点或括号。生成 Hash 无法映射回对象。

Flattening requires all property names to not interfere with the JSON path. Using dots or brackets in map keys or as property names is not supported when you use flattening. The resulting hash cannot be mapped back into an Object.

java.util.Datejava.util.Calendar 以毫秒为单位表示。如果 JSR-310 日期/时间类型在类路径上,则会将其序列化成其 toString 形式。

java.util.Date and java.util.Calendar are represented with milliseconds. JSR-310 Date/Time types are serialized to their toString form if jackson-datatype-jsr310 is on the class path.