zoukankan      html  css  js  c++  java
  • lettuce--Advanced Redis client

    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.

    几个常见的使用方法:

    1. 连接单机

    package com.lambdaworks.examples;
    
    import com.lambdaworks.redis.RedisClient;
    import com.lambdaworks.redis.RedisConnection;
    import com.lambdaworks.redis.RedisURI;
    
    /**
     * @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
     * @since 18.06.15 09:17
     */
    public class ConnectToRedis {
    
        public static void main(String[] args) {
            // Syntax: redis://[password@]host[:port][/databaseNumber]
            RedisClient redisClient = new RedisClient(RedisURI.create("redis://password@localhost: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:mpaluch@paluch.biz">Mark Paluch</a>
     * @since 18.06.15 09:17
     */
    public class ConnectToRedisCluster {
    
        public static void main(String[] args) {
            // Syntax: redis://[password@]host[:port]
            RedisClusterClient redisClient = new RedisClusterClient(RedisURI.create("redis://password@localhost: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:mpaluch@paluch.biz">Mark Paluch</a>
     * @since 18.06.15 09:17
     */
    public class ConnectToRedisUsingRedisSentinel {
    
        public static void main(String[] args) {
            // Syntax: redis-sentinel://[password@]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:mpaluch@paluch.biz">Mark Paluch</a>
     * @since 18.06.15 09:17
     */
    public class ConnectToRedisSSL {
    
        public static void main(String[] args) {
            // Syntax: rediss://[password@]host[:port][/databaseNumber]
            // Adopt the port to the stunnel port in front of your Redis instance
            RedisClient redisClient = new RedisClient(RedisURI.create("rediss://password@localhost: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:mpaluch@paluch.biz">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:mpaluch@paluch.biz">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

  • 相关阅读:
    1.淡入淡出效果js原生代码2.缓冲运动
    php 图片加水印插件
    php redis使用 常用方法 | Windows环境下安装Redis | Windows下php安装redis扩展(详解版)
    Windows下安装使用workman简单实例
    极简生成excel方法;excel表导入数据库
    php 生成表单 | url串禁止转义并解决中文字符乱码
    图片上传预览并保存图片 | 多图上传预览并保存图片 | 树状结构(jquery.treeview.js)
    php Rsa签名算法
    php 正则表达式 1.字符串中提取带小数点的数字 2.判断字符串中是否包含关键字 3.统计二维数组中某个字段值出现的次数
    虚拟主机部署tp项目,在.htaccess文件中隐藏index.php
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5089502.html
Copyright © 2011-2022 走看看