-
Notifications
You must be signed in to change notification settings - Fork 975
Spring Support
Using Lettuce through Spring Data Redis provides familiar Spring abstractions for Redis usage. It integrates well with other Spring components such as Spring Session or Spring Boot. See Spring Data Redis reference documentation for lettuce usage.
Lettuce Standalone/Sentinel/Pub/Sub and Upstream/Replica can be used through RedisClient
. You can provide bean definitions to manage Lettuce resources inside a Spring context. Bean management can take care of resource allocation and clean up through Spring’s bean lifecycle management. Using managed beans gives you the possibility to access lettuce’s RedisClient
/connections from various places inside your code. Your code can also benefit from dependency injection.
Configuring lettuce using Spring’s Java Configuration with @Bean
definitions requires you to provide bean definitions. Notice the specification of destroyMethod
to clean up resources once the application context is shut down.
@Configuration
public class LettuceConfig {
@Bean(destroyMethod = "shutdown")
ClientResources clientResources() {
return DefaultClientResources.create();
}
@Bean(destroyMethod = "shutdown")
RedisClient redisClient(ClientResources clientResources) {
return RedisClient.create(clientResources, RedisURI.create(TestSettings.host(), TestSettings.port()));
}
@Bean(destroyMethod = "close")
StatefulRedisConnection<String, String> connection(RedisClient redisClient) {
return redisClient.connect();
}
}
Use RedisClientFactoryBean
to create a managed instance of RedisClient
using XML-based configuration.
<bean id="redisClient" class="io.lettuce.core.support.RedisClientFactoryBean">
<property name="password" value="mypassword"/>
<!-- Redis URI Format: redis://host[:port]/database -->
<!-- Redis URI: Specify Database as Path -->
<property name="uri" value="redis://localhost/12"/>
<!-- Redis Sentinel URI Format: redis-sentinel://host[:port][,host[:port][,host[:port]]/database#masterId -->
<!-- Redis Sentinel URI: You can specify multiple sentinels. Specify Database as Path, Master Id as Fragment. -->
<property name="uri" value="redis-sentinel://localhost,localhost2,localhost3/1#myMaster"/>
</bean>
Lettuce Redis Cluster support can be used through RedisClusterClient
. You can provide bean definitions to manage Lettuce resources inside a Spring context. Bean management can take care of resource allocation and clean up through Spring’s bean lifecycle management. Using managed beans gives you the possibility to access Lettuce’s RedisClusterClient
/connections from various places inside your code. Your code can also benefit from dependency injection.
Configuring Lettuce using Spring’s Java Configuration with @Bean
definitions requires you to provide bean definitions. Notice the specification of destroyMethod
to clean up resources once the application context is shut down.
@Configuration
public class LettuceConfig {
@Bean(destroyMethod = "shutdown")
ClientResources clientResources() {
return DefaultClientResources.create();
}
@Bean(destroyMethod = "shutdown")
RedisClusterClient redisClusterClient(ClientResources clientResources) {
RedisURI redisURI = RedisURI.create(TestSettings.host(), 7379);
return RedisClusterClient.create(clientResources, redisURI);
}
@Bean(destroyMethod = "close")
StatefulRedisClusterConnection<String, String> clusterConnection(RedisClusterClient clusterClient) {
return clusterClient.connect();
}
}
Use RedisClusterClientFactoryBean
to create a managed instance of RedisClusterClient
using XML-based configuration.
<bean id="redisClient" class="io.lettuce.core.support.RedisClusterClientFactoryBean">
<property name="password" value="mypassword"/>
<!-- Redis URI Format: redis://host[:port]/database -->
<!-- Redis URI: Specify Database as Path -->
<property name="uri" value="redis://localhost/12"/>
</bean>
Lettuce documentation was moved to https://redis.github.io/lettuce/overview/
Intro
Getting started
- Getting started
- Redis URI and connection details
- Basic usage
- Asynchronous API
- Reactive API
- Publish/Subscribe
- Transactions/Multi
- Scripting and Functions
- Redis Command Interfaces
- FAQ
HA and Sharding
Advanced usage
- Configuring Client resources
- Client Options
- Dynamic Command Interfaces
- SSL Connections
- Native Transports
- Unix Domain Sockets
- Streaming API
- Events
- Command Latency Metrics
- Tracing
- Stateful Connections
- Pipelining/Flushing
- Connection Pooling
- Graal Native Image
- Custom commands
Integration and Extension
Internals