How to prepare Redis features software
Redis is a high -performance NOSQL database that is usually used as a memory cache. However, it is very useful as the basic data store solution. In this article, we will see how Redis features programming features on the example of the spring application. In many cases of use, the objects stored in Redis may be valid only for a certain period of time. This is especially useful for the continuation of short -lived organisms in Redis without having to remove them manually when they reach the end of their lives. We’ll consider how to make time to live (TTL) for the application. TTL here is just an example of Redis feature. Other properties can also be prepared in this way.
application
Let’s consider the spring application that stores Cardinfoentity in Redis. The entity contains sensitive information that can be stored for only 5 minutes. Here is what Cardinvini seems to be:
@Getter
@Setter
@ToString(exclude = "cardDetails")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@RedisHash
public class CardInfoEntity {
@Id
private String id;
private String cardDetails;
private String firstName;
private String lastName;
}
One needs to set TTL so that the objects are automatically deleted. This can be achieved in three ways:
- Use Timeline Explanation of @redishash (for example
@RedisHash(timeToLive = 5*60)
)) - Using a TIMETOLIVE explanatory comment on a digital feature or method
- Use KEYSPaceconfiguration.keyspacettings
The first two options have their faults. In the first case, the value is coded. There is no flexibility to change value without rebuilding and republishing the entire application. In the second case, we have to provide a field that is not related to the logic of work.
The third option does not have these problems. With this approach we can use property in application.yml
To set TTL and if necessary, the other Redis properties. It is also placed in a separate composition file and does not interfere with the field of work.
We need to carry out the formation of keys and introduce custom keys that contain Redis settings that we want to prepare.
@Configuration
@RequiredArgsConstructor
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class RedisConfiguration {
private final RedisKeysProperties properties;
@Bean
public RedisMappingContext keyValueMappingContext() {
return new RedisMappingContext(
new MappingConfiguration(new IndexConfiguration(), new CustomKeyspaceConfiguration()));
}
public class CustomKeyspaceConfiguration extends KeyspaceConfiguration {
@Override
protected Iterable initialConfiguration() {
return Collections.singleton(customKeyspaceSettings(CardInfoEntity.class, CacheName.CARD_INFO));
}
private KeyspaceSettings customKeyspaceSettings(Class type, String keyspace) {
final KeyspaceSettings keyspaceSettings = new KeyspaceSettings(type, keyspace);
keyspaceSettings.setTimeToLive(properties.getCardInfo().getTimeToLive().toSeconds());
return keyspaceSettings;
}
}
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class CacheName {
public static final String CARD_INFO = "cardInfo";
}
}
To make Redis delete the entities with one TTL, it should add enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP
To Enablesrepositories Explanation. The Cachename category has been presented to use the constants as the names of entities and to reflect that there can be multiple entities that can be formed differently if necessary.
conclusion
Redis has good integration with SPRING, which provides several composition options. The approach stipulated in this article is not the most obvious and person who does not have much experience working with spring. In many cases only use the explanatory comments at the top of the entities sufficient. However, if you have a more complicated application with many different entities with different Redis properties, the option to provide keys may be the best because of its clear structure and the above features (using properties and maintaining configuration outside work objects).
To view the full example application where AOP was used as shown in this article, read my other article about creating a sensitive data service with SPRING and Redis.
The source code for the full version of this service is available on GitHub.