使用Jedis对Redis操作入门篇

package com.jedis.test;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.*;

/**
 * @Author:Virvil
 * @Description:
 * @Datte:Created in 10:32 2018/4/13
 * Modified By:
 */
public class TestRedis {
    private Jedis jedis;
    @Before
    public void setup() {
        //连接redis服务器,192.168.209.129:6379
        jedis = new Jedis("192.168.209.129", 6379);
    }

    /*
    String 类型的操作
     */
    @Test
    public void testString (){
        //一个键值对的操作
        jedis.set("name","virvil");
        String name = jedis.get("name");
        System.out.println(name);

        //多个键值对的操作
        jedis.mset("age","20","sex","male");
        List<String> list = jedis.mget("name","age","sex");
        System.out.println(list);

        //减一
        jedis.decr("age");

        System.out.println(jedis.get("age"));
    }

    /*
    HashMap类型操作
     */
    @Test
    public void testMap(){

        Map<String,String> map = new HashMap<String,String>();
        map.put("name","virvil");
        map.put("age","20");
        map.put("sex","male");
        //将HashMap类型的实例作为value值存储
        jedis.hmset("user",map);
        //获取值,hmget(String key, String... fields)
        List<String> list = jedis.hmget("user","name","age","sex");
        System.out.println(list);

        String name = jedis.hget("user","name");
        System.out.println(name);
        Set<String> set = jedis.hkeys("user");
        List<String> list2 = jedis.hvals("user");
        System.out.println(set);
        System.out.println(list2);
    }

    /*
    list类型操作,双向链表
     */
    @Test
    public void testList(){
        //lpush 和 lpop 先进后出
        jedis.lpush("list","a");
        jedis.lpush("list","b");
        jedis.lpush("list","c");
        //查询list中的所有数据
        System.out.println(jedis.lrange("list",0,-1));
        //查询第1-2条数据
        System.out.println(jedis.lrange("list",0,1));
        jedis.lpop("list");

        //rpush 和 rpop 先进先出
        jedis.rpush("list","d");
        jedis.rpush("list","e");
        jedis.rpop("list");
        System.out.println(jedis.lrange("list",0,-1));
    }

    /*
    Set类型的操作
     */
    @Test
    public void testSet(){

        //添加
        jedis.sadd("set","a");
        jedis.sadd("set","b");
        jedis.sadd("set","c");
        //删除
        jedis.srem("set","a");
        //查看set中所有的数据
        System.out.println(jedis.smembers("set"));
        //判断b是否是set中的元素
        System.out.println(jedis.sismember("set","b"));
        //获取set中元素的个数
        System.out.println(jedis.scard("set"));
    }

    /*
    zset 类型操作
     */
    @Test
    public void testZset(){
        jedis.zadd("zset",8,"a");
        jedis.zadd("zset",7,"b");
        jedis.zadd("zset",6,"c");

        // 元素个数
        System.out.println(jedis.zcard("zset"));
        // 元素下标
        System.out.println(jedis.zscore("zset", "a"));
        // 删除元素
        System.out.println(jedis.zrem("zset", "a"));
        //score在0-100的总数
        System.out.println(jedis.zcount("zset", 0, 1000));
        //score + 50
        System.out.println(jedis.zincrby("zset", 50, "b"));
        //score在0-100的值
        System.out.println(jedis.zrangeByScore("zset",  0, 1000));
        //score排名
        System.out.println(jedis.zrank("zset", "c"));
        // 整个集合值
        System.out.println(jedis.zrange("zset", 0, -1));
    }
}

阅读更多
换一批

没有更多推荐了,返回首页