zoukankan      html  css  js  c++  java
  • 使用Java操作Redis(二)

    上篇文章中我们可以看到,通过自己动手编码来操作Redis是相当繁琐的,实际上我们在重复制造轮子。Redis网站上列举出了一些方便操作Redis的常用工具。

    这里写图片描述

    可供Java选择的比较多,这里介绍一下Jedis的使用。
    Jedis 所需Jar包下载地址:
    http://search.maven.org/#artifactdetails%7Credis.clients%7Cjedis%7C2.4.2%7Cjar

    http://search.maven.org/#artifactdetails%7Corg.apache.commons%7Ccommons-pool2%7C2.0%7Cjar

    將commons-pool2-2.0.jar和jedis-2.4.2.jar同时添加到build path中。

    Redis的value同时支持strings, hashes, lists, sets类型,下面代码为使用Jedis对这几种类型的插入和获取操作。

    package com.csii.redis.test;
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import org.junit.Assert;
    import org.junit.Test;
    //注意:为方便测试代码中用到了JUnit
    import redis.clients.jedis.Jedis;
    
    public class RedisTest extends Assert{
        public static Jedis jedis = null;
        static{
            jedis = new Jedis("localhost",6379, 500);
            jedis.connect();
            jedis.auth("123456");
            jedis.flushAll();
        }
        /**
         * strings类型操作
         */
        @Test
        public void testStrings() {
             String status = jedis.set("name", "Rongbo_J");
             assertEquals("OK", status);
             String name = jedis.get("name");
             System.out.println(name);
    
    
        }
        /**
         * hashes类型操作
         */
        @Test
        public void testHashes()
        {
            HashMap<String,String> personInfo = new HashMap<String,String>();
            personInfo.put("name", "Rongbo_J");
            personInfo.put("age", "18");
            personInfo.put("weight", "120");
            String status = jedis.hmset("person", personInfo);
            assertEquals("OK",status);
    
            Map<String, String> person =  jedis.hgetAll("person");
    
            System.out.println(person);
    
        }
        /**
         * lists类型操作
         */
        @Test
        public void testLists()
        {
            //lists为线性结构,通过lpush和lpop添加和移除list中的元素。
            for(int i=0;i<20;i++)
            {
                jedis.lpush("array","i:" + i);
            }
            long len = jedis.llen("array");
    
            System.out.println(len);
    
            for(int i=0;i<len;i++)
            {
                System.out.println(jedis.lpop("array"));
            }
    
            long len2 = jedis.llen("array");
    
            System.out.println(len2);
        }
    
        /**
         * sets类型操作
         */
        @Test
        public void testSets()
        {
            for(int i=0;i < 20;i++)
            {
                jedis.sadd("mySet", "SetElmt"+i);
            }
    
            long len = jedis.scard("mySet");
            System.out.println(len);
    
            Set<String> mySet = jedis.smembers("mySet");
    
            System.out.println(mySet);
    
    
        }
        /**
         * 不使用Jedis操作Redis,运行此测试方法时需要注释掉静态代码块部分
         */
        @Test
        public void testWithoutJedis()
        {
            try {
                Socket sock = new Socket("127.0.0.1",6379);
    
                OutputStream out = sock.getOutputStream();
                InputStream in = sock.getInputStream(); 
                String sendStr = "*3
    $3
    SET
    $4
    name
    $8
    rongbo_j
    ";
                out.write(sendStr.getBytes());
                sendStr = "*2
    $3
    GET
    $5
    name
    ";
                out.write(sendStr.getBytes());
    
                byte[] buffer = new byte[512];
                in.read(buffer);
                System.out.println(new String(buffer));
    
            } catch (Exception e) {
                e.printStackTrace();
            }   
        }
    }
    

    代码中只列举了两三种操作,实际上Jedis对每种类型提供的操作方式非常多,使用起来也非常简单,这里没有全部列举。

  • 相关阅读:
    SQL多表查询:内连接、外连接(左连接、右连接)、全连接、交叉连接
    PL/SQL Developer基本用法
    Oracle中存储过程与函数的区别
    C#反射动态调用dll中的方法
    WebClient模拟发送Post请求
    C#字符串、字节数组和内存流间的相互转换
    C#调用WebService
    VS2008安装SP1补丁后智能提示从中文变为英文的解决办法
    C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
    VS2008创建单元测试
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468936.html
Copyright © 2011-2022 走看看