lettuce--Advanced Redis client

來源:互聯網
上載者:User

標籤:

redis官方提供的java client:

git地址: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 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

幾個常見的使用方法:

1. 串連單機

package com.lambdaworks.examples;import com.lambdaworks.redis.RedisClient;import com.lambdaworks.redis.RedisConnection;import com.lambdaworks.redis.RedisURI;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:17 */public class ConnectToRedis {    public static void main(String[] args) {        // Syntax: redis://[[email protected]]host[:port][/databaseNumber]        RedisClient redisClient = new RedisClient(RedisURI.create("redis://[email protected]:6379/0"));        RedisConnection<String, String> connection = redisClient.connect();        System.out.println("Connected to Redis");        connection.close();        redisClient.shutdown();    }}

2. 串連叢集

package com.lambdaworks.examples;import com.lambdaworks.redis.RedisURI;import com.lambdaworks.redis.cluster.RedisAdvancedClusterConnection;import com.lambdaworks.redis.cluster.RedisClusterClient;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:17 */public class ConnectToRedisCluster {    public static void main(String[] args) {        // Syntax: redis://[[email protected]]host[:port]        RedisClusterClient redisClient = new RedisClusterClient(RedisURI.create("redis://[email protected]:7379"));        RedisAdvancedClusterConnection<String, String> connection = redisClient.connectCluster();        System.out.println("Connected to Redis");        connection.close();        redisClient.shutdown();    }}

3. 串連sentinel

package com.lambdaworks.examples;import com.lambdaworks.redis.*;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:17 */public class ConnectToRedisUsingRedisSentinel {    public static void main(String[] args) {        // Syntax: redis-sentinel://[[email protected]]host[:port][,host2[:port2]][/databaseNumber]#sentinelMasterId        RedisClient redisClient = new RedisClient(                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.安全的串連

package com.lambdaworks.examples;import com.lambdaworks.redis.*;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:17 */public class ConnectToRedisSSL {    public static void main(String[] args) {        // Syntax: rediss://[[email protected]]host[:port][/databaseNumber]        // Adopt the port to the stunnel port in front of your Redis instance        RedisClient redisClient = new RedisClient(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整合

package com.lambdaworks.examples;import com.lambdaworks.redis.*;import org.springframework.beans.factory.annotation.Autowired;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:31 */public class MySpringBean {    private RedisClient redisClient;    @Autowired    public void setRedisClient(RedisClient redisClient) {        this.redisClient = redisClient;    }    public String ping() {        RedisConnection<String, String> connection = redisClient.connect();        String result = connection.ping();        connection.close();        return result;    }}

使用代碼如下:

package com.lambdaworks.examples;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lambdaworks.redis.RedisClient;import com.lambdaworks.redis.RedisConnection;/** * @author <a href="mailto:[email protected]">Mark Paluch</a> * @since 18.06.15 09:17 */public class SpringExample {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                "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();    }}

參考文獻:

【1】https://github.com/mp911de/lettuce

【2】http://redis.paluch.biz

 

lettuce--Advanced Redis client

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.