参考文档: https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/
Redis中文教程: http://www.redis.net.cn/tutorial/3501.html
在某些情况下,不需要加载和重写整个实体,只需在其中设置一个新值即可。 上次活动时间的会话时间戳可能是您只想更改一个属性的场景。 PartialUpdate允许定义对现有对象的设置和删除操作,同时负责更新实体本身的潜在到期时间以及索引结构。
PartialUpdate update = new PartialUpdate("e2c7dcee", Person.class)
.set("firstname", "mat")
.set("address.city", "emond's field")
.del("age");
template.update(update);
update = new PartialUpdate("e2c7dcee", Person.class)
.set("address", new Address("caemlyn", "andor"))
.set("attributes", singletonMap("eye-color", "grey"));
template.update(update);
update = new PartialUpdate("e2c7dcee", Person.class)
.refreshTtl(true);
.set("expiration", 1000);
template.update(update);
1. Set the simple property firstname to mat.
2. Set the simple property address.city to emond’s field without having to pass in the entire object.
This does not work when a custom conversion is registered.
3. Remove the property age.
4. Set complex property address.
5. Set a map/collection of values removes the previously existing map/collection and replaces the values with the given ones.
6. Automatically update the server expiration time when altering Time To Live.
Time To Live
更新复杂对象以及映射/集合结构需要与Redis进一步交互以确定现有值,这意味着可能会发现重写整个实体可能会更快。
Example 19. Sample Repository finder Method
public interface PersonRepository extends CrudRepository {
List findByFirstname(String firstname);
}
请确保在查找器方法中使用的属性设置为索引。
Redis存储库的查询方法仅支持查询具有分页的实体和实体集合。
使用派生查询derived query 方法可能并不总是足以对要执行的查询建模。 RedisCallback提供了对索引结构的实际匹配或甚至自定义添加的更多控制。 它所需要的就是提供一个RedisCallback,它返回一个单独的或一组Iterable set的id值。
Example 20. Sample finder using RedisCallback
String user = //...
List sessionsByUser = template.find(new RedisCallback>() {
public Set doInRedis(RedisConnection connection) throws DataAccessException {
return connection
.sMembers("sessions:securityContext.authentication.principal.username:" + user);
}}, RedisSession.class);
以下概述了Redis支持的关键字以及包含该关键字的方法。
当所有相关密钥映射到同一个插槽时,只能在服务器端处理像SINTER和SUNION这样的一些命令。 否则,计算必须在客户端完成。 因此将密钥空间keyspaces 固定到单个插槽slot,可以立即使用Redis服务器计算。
在使用Redis群集时,通过`@RedisHash(“{yourkeyspace}”)定义和固定密钥空间到特定的插槽。
Instances of the repository interfaces are usually created by a container, which Spring is the most natural choice when working with Spring Data. There’s sophisticated support to easily set up Spring to create bean instances. Spring Data Redis ships with a custom CDI extension that allows using the repository abstraction in CDI environments. The extension is part of the JAR so all you need to do to activate it is dropping the Spring Data Redis JAR into your classpath.
You can now set up the infrastructure by implementing a CDI Producer for the RedisConnectionFactory
and 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 redisOperationsProducer(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
template.afterPropertiesSet();
return template;
}
}
必要的设置可能因您运行的JavaEE环境而异。
class RepositoryClient {
@Inject
PersonRepository repository;
public void businessMethod() {
List people = repository.findAll();
}
}
Redis存储库需要RedisKeyValueAdapter和RedisKeyValueTemplate实例。 如果未找到提供的bean,则这些bean由Spring Data CDI扩展创建和管理。 但是,您可以提供自己的Bean来配置RedisKeyValueAdapter和RedisKeyValueTemplate的特定属性。