关于java:Spring认证中国教育管理中心Spring-Data-Redis框架教程六

4次阅读

共计 4941 个字符,预计需要花费 13 分钟才能阅读完成。

原题目:Spring 认证中国教育管理中心 -Spring Data Redis 框架教程六

13.10.1. 排序查询方法后果
Redis 存储库容许应用各种办法来定义排序程序。Redis 自身在检索散列或汇合时不反对动静排序。因而,Redis 存储库查询方法 Comparator 在将后果返回为 之前结构了利用于后果的 List。让咱们看一下上面的例子:

示例 36. 对查问后果进行排序

interface PersonRepository extends RedisRepository<Person, String> {

List<Person> findByFirstnameOrderByAgeDesc(String firstname);

List<Person> findByFirstname(String firstname, Sort sort);
}
从办法名称派生的动态排序。

应用办法参数进行动静排序。

13.11. 在集群上运行的 Redis 存储库
您能够在集群 Redis 环境中应用 Redis 存储库反对。无关配置详细信息,请参阅“Redis 集群”局部 ConnectionFactory。尽管如此,还必须进行一些额定的配置,因为默认的密钥散布在整个集群及其插槽中流传实体和二级索引。

下表显示了集群上的数据详细信息(基于后面的示例):

钥匙

类型

投币口

节点

人:e2c7dcee-b8cd-4424-883e-736ce564363e

哈希的 id

15171

127.0.0.1:7381

人:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56

哈希的 id

7373

127.0.0.1:7380

人:名字:兰德

指数

1700

127.0.0.1:7379

当所有波及的键都映射到同一个槽时,某些命令(例如 SINTER 和 SUNION)只能在服务器端解决。否则,必须在客户端进行计算。因而,将键空间固定到单个插槽十分有用,这让咱们能够立刻应用 Redis 服务器端计算。下表显示了当您执行此操作时会产生什么(留神插槽列中的更改和节点列中的端口值):

钥匙

类型

投币口

节点

{人}:e2c7dcee-b8cd-4424-883e-736ce564363e

哈希的 id

2399

127.0.0.1:7379

{人}:a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56

哈希的 id

2399

127.0.0.1:7379

{人}:名字:兰德

指数

2399

127.0.0.1:7379

@RedisHash(“{yourkeyspace}”)当您应用 Redis 集群时,通过应用到特定插槽来定义和固定键空间。

13.12.CDI 集成
存储库接口的实例通常由容器创立,因而在应用 Spring Data 时,Spring 是最天然的抉择。Spring 提供了简单的创立 bean 实例的办法。Spring Data Redis 附带一个自定义 CDI 扩大,容许您在 CDI 环境中应用存储库形象。该扩大是 JAR 的一部分,因而要激活它,请将 Spring Data Redis JAR 放入您的类门路中。

而后,您能够通过为 RedisConnectionFactoryand 实现 CDI Producer 来设置根底构造 RedisOperations,如以下示例所示:

class RedisOperationsProducer {

@Produces
RedisConnectionFactory redisConnectionFactory() {

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(new RedisStandaloneConfiguration());
jedisConnectionFactory.afterPropertiesSet();

return jedisConnectionFactory;

}

void disposeRedisConnectionFactory(@Disposes RedisConnectionFactory redisConnectionFactory) throws Exception {

if (redisConnectionFactory instanceof DisposableBean) {((DisposableBean) redisConnectionFactory).destroy();}

}

@Produces
@ApplicationScoped
RedisOperations<byte[], byte[]> redisOperationsProducer(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
template.setConnectionFactory(redisConnectionFactory);
template.afterPropertiesSet();

return template;

}

}
Spring 认证中国教育管理中心 -Spring Data Redis 框架教程六
必要的设置可能会有所不同,具体取决于您的 JavaEE 环境。

Spring Data Redis CDI 扩大抉择所有可用的存储库作为 CDI bean,并在容器申请存储库类型的 bean 时为 Spring Data 存储库创立代理。因而,获取 Spring Data 存储库的实例是申明 @Injected 属性的问题,如以下示例所示:

class RepositoryClient {

@Inject
PersonRepository repository;

public void businessMethod() {

List<Person> people = repository.findAll();

}
}
Redis 存储库须要 RedisKeyValueAdapter 和 RedisKeyValueTemplate 实例。如果未找到提供的 bean,则这些 bean 由 Spring Data CDI 扩大创立和治理。你能够,然而,提供本人的豆子配置的特定属性 RedisKeyValueAdapter 和 RedisKeyValueTemplate。

13.13.Redis 存储库分析
Redis 作为存储自身提供了一个十分狭隘的低级 API,将高级性能(例如二级索引和查问操作)留给用户。

本节提供了存储库形象收回的命令的更具体视图,以便更好地了解潜在的性能影响。

思考以下实体类作为所有操作的终点:

示例 37. 示例实体

@RedisHash(“people”)
public class Person {

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

public class Address {

@GeoIndexed Point location;
}
13.13.1. 插入新的
repository.save(new Person(“rand”, “al’thor”));
HMSET “people:19315449-cda2-4f5c-b696-9cb8018fa1f9” “_class” “Person” “id” “19315449-cda2-4f5c-b696-9cb8018fa1f9” “firstname” “rand” “lastname” “al’thor”
SADD “people” “19315449-cda2-4f5c-b696-9cb8018fa1f9”
SADD “people:firstname:rand” “19315449-cda2-4f5c-b696-9cb8018fa1f9”
SADD “people:19315449-cda2-4f5c-b696-9cb8018fa1f9:idx” “people:firstname:rand”
将展平的条目保留为哈希。

将 <1> 中写入的哈希键增加到同一键空间中实体的辅助索引中。

将 <2> 中写入的哈希键增加到具备属性值的名字的二级索引中。

将 <3> 的索引增加到条目标帮忙器构造集中,以跟踪要在删除 / 更新时清理的索引。

13.13.2. 替换现有
repository.save(new Person(“e82908cf-e7d3-47c2-9eec-b4e0967ad0c9”, “Dragon Reborn”, “al’thor”));
DEL “people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9”
HMSET “people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9” “_class” “Person” “id” “e82908cf-e7d3-47c2-9eec-b4e0967ad0c9” “firstname” “Dragon Reborn” “lastname” “al’thor”
SADD “people” “e82908cf-e7d3-47c2-9eec-b4e0967ad0c9”
SMEMBERS “people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx”
TYPE “people:firstname:rand”
SREM “people:firstname:rand” “e82908cf-e7d3-47c2-9eec-b4e0967ad0c9”
DEL “people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx”
SADD “people:firstname:Dragon Reborn” “e82908cf-e7d3-47c2-9eec-b4e0967ad0c9”
SADD “people:e82908cf-e7d3-47c2-9eec-b4e0967ad0c9:idx” “people:firstname:Dragon Reborn”
删除现有的散列以防止散列键的残余局部可能不再存在。

将展平的条目保留为哈希。

将 <1> 中写入的哈希键增加到同一键空间中实体的辅助索引中。

获取可能须要更新的现有索引构造。

查看索引是否存在以及它是什么类型(文本、天文等)。

从索引中删除可能存在的键。

删除保留索引信息的助手。

将 <2> 中增加的哈希键增加到具备属性值的名字的二级索引中。

将 <6> 的索引增加到条目标帮忙器构造集中,以跟踪要在删除 / 更新时清理的索引。

13.13.3. 保留天文数据
天文索引与基于一般文本的索引遵循雷同的规定,但应用天文构造来存储值。保留应用天文索引属性的实体会产生以下命令:

GEOADD “people:hometown:location” “13.361389” “38.115556” “76900e94-b057-44bc-abcf-8126d51a621b”
SADD “people:76900e94-b057-44bc-abcf-8126d51a621b:idx” “people:hometown:location”
将保留条目标键增加到天文索引。

跟踪索引构造。

13.13.4. 应用简略索引查找
repository.findByFirstname(“egwene”);
SINTER “people:firstname:egwene”
HGETALL “people:d70091b5-0b9a-4c0a-9551-519e61bc9ef3”
HGETALL …
获取蕴含在二级索引中的键。

别离获取 <1> 返回的每个键。

13.13.5. 应用天文索引查找
repository.findByHometownLocationNear(new Point(15, 37), new Distance(200, KILOMETERS));
GEORADIUS “people:hometown:location” “15.0” “37.0” “200.0” “km”
HGETALL “people:76900e94-b057-44bc-abcf-8126d51a621b”
HGETALL …
获取蕴含在二级索引中的键。

别离获取 <1> 返回的每个键。

正文完
 0