The Java client that is officially provided by Redis:
Git address: https://github.com/mp911de/lettuce
Advanced Redis Client for Thread-safe Sync, async, and reactive usage. Supports Cluster, Sentinel, pipelining, and Codecs.http://redis.paluch.biz
Introduction
Lettuce is a scalable thread-safe Redis client for synchronous, asynchronous and reactive usage. Multiple threads may share one connection if they avoid blocking and transactional operations such as Blpop and C1>multi/EXEC. Lettuce is built with Netty. Supports Advanced Redis features such as Sentinel, Cluster, pipelining, Auto-reconnect and Redis data models.
This version of lettuce has been tested against Redis and 3.0.
- Lettuce 3.x works with Java 6, 7 and 8, lettuce 4.x requires Java 8
- synchronous, asynchronous and reactive usage
- Redis Sentinel
- Redis Cluster
- SSL and Unix Domain Socket connections
- Streaming API
- CDI and Spring Integration
- Codecs (for Utf8/bit/json etc representation of your data)
- Multiple Command Interfaces
Several common ways to use:
1. Connecting a single machine
PackageCom.lambdaworks.examples;Importcom.lambdaworks.redis.RedisClient;Importcom.lambdaworks.redis.RedisConnection;ImportCom.lambdaworks.redis.RedisURI;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:17*/ Public classConnecttoredis { Public Static voidMain (string[] args) {//Syntax:redis://[email protected]]host[:p Ort][/databasenumber]Redisclient redisclient =NewRedisclient (Redisuri.create ("Redis://[email protected]:6379/0")); Redisconnection<string, string> connection =Redisclient.connect (); System.out.println ("Connected to Redis"); Connection.close (); Redisclient.shutdown (); }}
2. Connecting the cluster
PackageCom.lambdaworks.examples;ImportCom.lambdaworks.redis.RedisURI;Importcom.lambdaworks.redis.cluster.RedisAdvancedClusterConnection;Importcom.lambdaworks.redis.cluster.RedisClusterClient;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:17*/ Public classConnecttorediscluster { Public Static voidMain (string[] args) {//Syntax:redis://[email protected]]host[:p ORT]Redisclusterclient redisclient =NewRedisclusterclient (Redisuri.create ("Redis://[email protected]:7379")); Redisadvancedclusterconnection<string, string> connection =Redisclient.connectcluster (); System.out.println ("Connected to Redis"); Connection.close (); Redisclient.shutdown (); }}
3. Connect Sentinel
PackageCom.lambdaworks.examples;Importcom.lambdaworks.redis.*;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:17*/ Public classConnecttoredisusingredissentinel { Public Static voidMain (string[] args) {//Syntax:redis-sentinel://[email protected]]host[:p ort][,host2[:p Ort2]][/databasenumber] #sentinelMasterIdRedisclient redisclient =Newredisclient (Redisuri.create ("Redis-sentinel://localhost:26379,localhost:26380/0#mymaster")); Redisconnection<string, string> connection =Redisclient.connect (); System.out.println ("Connected to Redis using Redis Sentinel"); Connection.close (); Redisclient.shutdown (); }}
4. Secure connection
PackageCom.lambdaworks.examples;Importcom.lambdaworks.redis.*;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:17*/ Public classConnecttoredisssl { Public Static voidMain (string[] args) {//Syntax:rediss://[email protected]]host[:p Ort][/databasenumber]//adopt the port to the Stunnel port in front of your Redis instanceRedisclient redisclient =NewRedisclient (Redisuri.create ("Rediss://[email protected]:6443/0")); Redisconnection<string, string> connection =Redisclient.connect (); System.out.println ("Connected to Redis using SSL"); Connection.close (); Redisclient.shutdown (); }}
5. Spring Integration
PackageCom.lambdaworks.examples;Importcom.lambdaworks.redis.*;Importorg.springframework.beans.factory.annotation.Autowired;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:31*/ Public classMyspringbean {Privateredisclient redisclient; @Autowired Public voidsetredisclient (redisclient redisclient) { This. redisclient =redisclient; } PublicString Ping () {redisconnection<string, string> connection =Redisclient.connect (); String result=connection.ping (); Connection.close (); returnresult; }}
Use the following code:
PackageCom.lambdaworks.examples;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.lambdaworks.redis.RedisClient;Importcom.lambdaworks.redis.RedisConnection;/** * @author<a href= "Mailto:[email protected]" >mark paluch</a> *@since18.06.15 09:17*/ Public classSpringexample { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext context=NewClasspathxmlapplicationcontext ("Com/lambdaworks/examples/springtest-context.xml"); Redisclient Client= Context.getbean (redisclient.class); Redisconnection<string, string> connection =Client.connect (); System.out.println ("PING:" +connection.ping ()); Connection.close (); Myspringbean Myspringbean= Context.getbean (Myspringbean.class); System.out.println ("PING:" +myspringbean.ping ()); Context.close (); }}
Reference documents:
"1" https://github.com/mp911de/lettuce
"2" http://redis.paluch.biz
lettuce--advanced Redis Client