spring-boot-starter-data-redis 翻译官方文档 8.4 - 8.6

参考文档: https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/

Redis中文教程: http://www.redis.net.cn/tutorial/3501.html

8.4. Secondary Indexes

二级索引用于启用基于本机Redis结构的查找操作。 值在每次保存时写入相应的索引,并在对象被删除或过期时被删除。

8.4.1. Simple Property Index
鉴于示例Person实体,我们可以通过使用@Indexed注释属性来为firstname创建索引。

Example 13. Annotation driven indexing

@RedisHash("persons")
public class Person {

  @Id String id;
  @Indexed String firstname;
  String lastname;
  Address address;
}

索引是为实际属性值构建的。 保存两个Persons,例如。 “rand”和“aviendha”将会设置如下的索引。

SADD persons:firstname:rand e2c7dcee-b8cd-4424-883e-736ce564363e
SADD persons:firstname:aviendha a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56

在嵌套元素上也可以有索引。 假设地址具有用@Indexed注释的城市属性。 在那种情况下,一旦person.address.city不为空,我们就为每个城市设置了Sets。

SADD persons:address.city:tear e2c7dcee-b8cd-4424-883e-736ce564363e

此外,编程设置允许在map keys和list属性上定义索引。


Same as with keyspaces it is possible to configure indexes without the need of annotating the actual domain type.

keyspaces相同,可以配置索引而不需要在实际的域类型上使用注解。

Example 14. Index Setup via @EnableRedisRepositories

@Configuration
@EnableRedisRepositories(indexConfiguration = MyIndexConfiguration.class)
public class ApplicationConfig {

  //... RedisConnectionFactory and RedisTemplate Bean definitions omitted

  public static class MyIndexConfiguration extends IndexConfiguration {

    @Override
    protected Iterable<IndexDefinition> initialConfiguration() {
      return Collections.singleton(new SimpleIndexDefinition("persons", "firstname"));
    }
  }
}

Example 15. Programmatic Index setup

@Configuration
@EnableRedisRepositories
public class ApplicationConfig {

  //... RedisConnectionFactory and RedisTemplate Bean definitions omitted

  @Bean
  public RedisMappingContext keyValueMappingContext() {
    return new RedisMappingContext(
      new MappingConfiguration(
        new KeyspaceConfiguration(), new MyIndexConfiguration()));
  }

  public static class MyIndexConfiguration extends IndexConfiguration {

    @Override
    protected Iterable<IndexDefinition> initialConfiguration() {
      return Collections.singleton(new SimpleIndexDefinition("persons", "firstname"));
    }
  }
}
8.4.2. Geospatial Index
假设 Address 类型包含一个类型为Point的location属性,该位置保存特定地址的地理坐标。 通过使用@GeoIndexed注解属性,将使用Redis GEO命令添加这些值。


在上面的例子中,使用GEOADD和对象id作为成员的名字来存储lon / lat值。 查找方法允许使用CirclePoint, Distance组合来查询这些值。

不能将near/within与其他标准组合在一起。

8.5. Time To Live

存储在Redis中的对象只能在一段时间内有效。这对于在Redis中保留短暂的对象特别有用,而不必在达到其寿命时手动删除它们。 以秒为单位的到期时间可以通过@RedisHash(timeToLive = ...)以及通过KeyspaceSettings进行设置(请参阅 Keyspaces)。

可以通过在 numeric属性或方法上使用@TimeToLive注释来设置更灵活的到期时间。但是,不要在同一个类中的方法和属性上应用@TimeToLive。

Example 16. Expirations

public class TimeToLiveOnProperty {

  @Id
  private String id;

  @TimeToLive
  private Long expiration;
}

public class TimeToLiveOnMethod {

  @Id
  private String id;

  @TimeToLive
  public long getTimeToLive() {
  	return new Random().nextLong();
  }
}
使用@TimeToLive显式注释属性将从Redis回读实际的TTL或PTTL值。 -1表示该对象没有过期关联。

repository的实现确保了通过RedisMessageListenerContainer订阅Redis keyspace notifications

当到期被设置为正值时,执行相应的EXPIRE命令。除了保留原始文件外,仿真副本被存储在Redis中并设置为在原始文件保留5分钟后到期。这样做的目的在于,开启Repository支持,通过Springs ApplicationEventPublisher发布RedisKeyExpiredEvent持有的过期值密钥过期甚至原始值已经消失。所有连接的应用程序将使用Spring Data Redis repository接收到RedisKeyExpiredEvent

默认情况下,初始化应用程序时,key expiry listener是被禁用的。可以在@EnableRedisRepositories或RedisKeyValueAdapter中调整为启用模式,以启动应用程序的listener,或者在第一次插入具有TTL的实体时自动启动listener。可用的值请参阅EnableKeyspaceEvents。

RedisKeyExpiredEvent将保存实际过期的域对象以及密钥的副本。

延迟或禁用到期事件侦听器启动会影响RedisKeyExpiredEvent发布。 被禁用的事件侦听器不会发布到期事件。 由于延迟侦听器初始化,延迟启动可能导致事件丢失。

keyspace通知消息侦听器将在Redis中更改notify-keyspace-events设置(如果尚未设置这些设置)。 现有的设置不会被覆盖,所以留给用户去正确的设置这些,当现有的设置不为空时。 请注意,在AWS ElastiCache上禁用了CONFIG,启用监听器将导致错误。

Redis Pub / Sub消息不是持久的。 如果在应用程序关闭期间某个键过期,则不会处理到期事件,这可能会导致secondary indexes引用已过期的对象。

8.6. Persisting References

使用@Reference标记属性允许存储简单的键引用,而不是将值复制到Hash本身。 在从Redis加载时,引用会自动解析并映射回对象。

Example 17. Sample Property Reference

_class = org.example.Person
id = e2c7dcee-b8cd-4424-883e-736ce564363e
firstname = rand
lastname = al’thor
mother = persons:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56 (1)
(1)这个引用存储了被引用对象的整个键(keyspace:id)。

在保存引用对象时,引用对象不会保留更改。 请确保分开保存对引用对象的更改,因为只有引用将被存储。 在引用类型的属性上设置的索引不会被解析。











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值