zoukankan      html  css  js  c++  java
  • 3.API访问redis数据库、redis集群

    API访问redis数据库
    ----------------------------------
    依赖
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
    

      代码

    import redis.clients.jedis.Jedis;
    
    /**
    * @author star
    */
    public class TestRedis {
        public static void main(String[] args) {
            //Jedis(主机,端口)
            Jedis redis = new Jedis("s101", 6379);
            redis.set("key2","tom1");
            String value = redis.get("key2");
            
            System.out.println(value);
            System.out.println(redis.hgetAll("user_0"));
    
        }
    }
    

      

     
    API访问 redis集群
    -----------------------------
     
    代码
    @Test
    public void testRedisCluster() throws IOException {
        Set<HostAndPort> hosts = new HashSet<HostAndPort>();
        hosts.add(new HostAndPort("192.168.116.101", 7000));
        hosts.add(new HostAndPort("192.168.116.101", 7001));
        hosts.add(new HostAndPort("192.168.116.101", 7002));
        hosts.add(new HostAndPort("192.168.116.101", 7003));
        hosts.add(new HostAndPort("192.168.116.101", 7004));
        hosts.add(new HostAndPort("192.168.116.101", 7005));
        BinaryJedisCluster cluster = new BinaryJedisCluster(hosts);
        byte[] value = cluster.get("key1".getBytes());
        System.out.println(new String(value));
        cluster.close();
    }
     
     
  • 相关阅读:
    CCPC 2020 长春站 部分简略题解
    atcoder arc106 D Powers
    循环节与拓展欧拉定理(广义欧拉降幂)
    最长公共上升子序列 题解
    namomo fish round1 A~C题解
    Codeforces Round #666 (Div. 2) A~E题解
    Educational Codeforces Round 93 Div2 A~E题解
    Codeforces Round #578 Div2 1200 A~E题解
    UVA11997 K Smallest Sums 题解
    LCA模板
  • 原文地址:https://www.cnblogs.com/star521/p/9863275.html
Copyright © 2011-2022 走看看