Redis --五种简单的数据类型(java代码操控)

Jedis

使用java 的api来操作redis的技术
1.单实例连接
导入pom文件

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.0.1</version>
  </dependency>

最基础的redis连接

public class TestJedis {
    public static void main(String[] args) {
        //redis连接
        Jedis jedis = new Jedis("192.168.159.130",6379);
        //设置key-value值
        jedis.set("username", "chenbin");
        //获取对应的值
        String username = jedis.get("username");
        System.out.println(username);
        //关闭
        jedis.close();
    }
}

使用连接池封装连接对象:

public class TestRedisPool {

    public static void main(String[] args) {
        //1.设置连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2.设置连接池参数
        config.setMaxTotal(30);
        //3.最大空闲
        config.setMaxIdle(10);
        //4.获取连接池对象
        JedisPool jedisPool = new JedisPool("192.168.159.130",6379);

        Jedis jedis = null;//客户端对象
        try{
            jedis = jedisPool.getResource();
            //设置参数e
            jedis.set("name","chenbin");
            String name = jedis.get("name");
            System.out.println("name="+name);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(jedis!=null){
                jedis.close();
            }
            //虚拟机关闭的时候,释放资源
            if(jedisPool!=null){
                jedisPool.close();
            }
        }
    }
}

String操作类使用:

package com.bruceliu.demo2;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.concurrent.TimeUnit;

/**
 * @Author 晨边#CB
 * @Date:created in  2019/9/26 23:57
 * @Version V1.0
 * 使用jedis完成redis的string操作
 **/

public class TestString {

    Jedis jedis = null;//客户端对象
    JedisPool jedisPool = null;

    @Test
    public void testString(){
        System.out.println("新增数据:");
        System.out.println(jedis.set("name","chenbin"));
        System.out.println(jedis.set("name1","rrlv"));
        System.out.println(jedis.set("name2","chenhong"));

        System.out.println("删除name2");
        System.out.println(jedis.del("name2"));
        System.out.println("修改name的值:"+jedis.set("name1","chenbinhaha"));

        System.out.println("在name2的后面追加值:"+jedis.append("name2","haha"));
        System.out.println(jedis.get("name2"));

        System.out.println("新增多个值:"+jedis.mset("key1","111","key2","222","key3","333"));
        System.out.println("同时获取多个值"+jedis.mget("key1","key2","key3"));

        System.out.println("删除多个键值对:"+jedis.del(new String[]{"key1","key2"}));
        System.out.println("获取多个键值对:"+jedis.mget("key1","key2","key3"));
        jedis.flushDB();
        System.out.println("=====新增键值对防止覆盖原先值===========");
        System.out.println(jedis.setnx("key1","value1"));
        System.out.println(jedis.setnx("key2","value2"));
        System.out.println(jedis.setnx("key1","value1-new"));//无法覆盖之前的值

        System.out.println("==========新增键值对并设置有效时间");
        System.out.println(jedis.setex("key3",2,"value3"));
        System.out.println(jedis.get("key3"));
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3"));

        System.out.println("=======获取原值,更新为新值=============");
        System.out.println(jedis.getSet("key2","key2GetSet"));
        System.out.println(jedis.get("key2"));

        System.out.println("获取key2的值的字串:"+jedis.getrange("key2",0,2));
    }




    @Before
    public void init(){
        //1.设置连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2.设置连接池参数
        config.setMaxTotal(30);
        //3.最大空闲
        config.setMaxIdle(10);
        //4.获取连接池对象
        jedisPool = new JedisPool("192.168.159.130",6379);

        try{
            jedis = jedisPool.getResource();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @After
    public void closeResources(){
        if(jedis!=null){
            jedis.close();
        }
        if(jedisPool!=null){
            jedisPool.close();
        }
    }

}

List操作使用

public class TestList {
    Jedis jedis = null;//客户端对象
    JedisPool jedisPool = null;


    @Test
    public void testList(){
        jedis.flushDB();
        System.out.println("===========添加一个List================");
        jedis.lpush("collections","bb","cc","dd","ee","aa","bb","cc");

        System.out.println("获取collections中的元素:"+jedis.lrange("collections",0,-1));
        System.out.println("======================================");
        //删除列表指定的值,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
        System.out.println("删除指定元素个数:"+jedis.lrem("collections",2,"HashMap"));

        System.out.println("删除下标0-3区间之外的元素:"+jedis.ltrim("collections",0,3));

        System.out.println("collections列表出栈(左侧):"+jedis.lpop("collections"));
        System.out.println("collections列表出栈(左侧):"+jedis.lpop("collections"));
        System.out.println("collections列表出栈(左侧):"+jedis.lpop("collections"));

        System.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collection","gg"));

        System.out.println("collections列表出栈(右侧):"+jedis.rpop("collections"));
        System.out.println("collections内容:"+jedis.lrange("collections",0,-1));

        System.out.println("修改collections指定下标内容:"+jedis.lset("collections",1,"ff"));

        System.out.println("collections的长度:"+jedis.llen("collections"));
        System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections",2));

        System.out.println("====================================");
        jedis.lpush("sorted","11","33","44","22","66");
        System.out.println("排序之前:"+jedis.lrange("sorted",0,-1));
        System.out.println(jedis.sort("sorted"));  //升序排列

        System.out.println("排序之前不变:"+jedis.lrange("sorted",0,-1));




    }




    @Before
    public void init(){
        //1.设置连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2.设置连接池参数
        config.setMaxTotal(30);
        //3.最大空闲
        config.setMaxIdle(10);
        //4.获取连接池对象
        jedisPool = new JedisPool("192.168.159.130",6379);

        try{
            jedis = jedisPool.getResource();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @After
    public void closeResources(){
        if(jedis!=null){
            jedis.close();
        }
        if(jedisPool!=null){
            jedisPool.close();
        }
    }
}

set的操作与使用:

@Test
    public void TestSet(){

        jedis.flushDB();
        System.out.println("=============向集合中添加元素(不重复)==================");
        System.out.println(jedis.sadd("eleSet","e1","e2","e3","e4","e5","e6","e7"));

        System.out.println(jedis.sadd("eleSet","e6"));
        System.out.println(jedis.sadd("eleSet","e6"));

        System.out.println("查看set中所有的元素:"+jedis.smembers("eleSet"));

        System.out.println("删除一个e4的元素:"+jedis.srem("eleSet","e4"));
        System.out.println("eleSet中所有元素为:"+jedis.smembers("eleSet"));

        System.out.println("随机的删除eleSet元素中的一个"+jedis.spop("eleSet"));
        System.out.println("随机的删除eleSet元素中的一个"+jedis.spop("eleSet"));

        System.out.println("eleSet中的所有元素为:"+jedis.smembers("eleSet"));

        System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet"));

        System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet","e3"));
        System.out.println("e2是否在eleSet中:"+jedis.sismember("eleSet","e2"));
        System.out.println("e6是否在eleSet中:"+jedis.sismember("eleSet","e6"));
        System.out.println("=================================================");

        System.out.println("将eleSet中的删除e1并存入eleSet1中:"+jedis.smove("eleSet","eleSet1","e1"));


        System.out.println(jedis.sadd("element1","e1","e2","e4","e5","e7"));
        System.out.println(jedis.sadd("element2","e0","e1","e3","e4"));

        System.out.println("==================集合运算========================");
        System.out.println("element1和element2的并集:"+jedis.sinter("element1","element2"));
        System.out.println("element1和element2的交集:"+jedis.sunion("element1","element2"));
        System.out.println("element1和element2的差集:"+jedis.sdiff("element1","element2"));

        jedis.sinterstore("element3","element1","element2");//求交集并将交集保存到element3
        System.out.println("element3中的元素:"+jedis.smembers("element3"));


    }

Hash的操作:

@Test
    public void testHash (){
        jedis.flushDB();
        Map<String,String> map = new HashMap<String, String>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");

        //添加名称为hash(key)的hash元素
        jedis.hmset("hash",map);
        //向名称为hash的hash中添加key5-value5
        jedis.hset("hash","key5","value5");

        System.out.println("散列hash所有的键值对:"+jedis.hgetAll("hash"));//return Map<String ,String>

        System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));//return set<String>
        System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));//return List<String>

        System.out.println("将key6保存的值加上一个整,如果key6不存在则添加key6:"+jedis.hincrBy("hash","key6",6));
        System.out.println("散列hash所有的键值对为:"+jedis.hgetAll("hash"));

        System.out.println("将key6保存的值加上一个整,如果key6不存在则添加key6:"+jedis.hincrBy("hash","key6",3));
        System.out.println("散列hash所有的键值对为:"+jedis.hgetAll("hash"));

        System.out.println("删除一个或多个键值对:"+jedis.hdel("hash","key2"));

        System.out.println("散列hash的所有键值对:"+jedis.hgetAll("hash"));

        System.out.println("散列hash的大小:"+jedis.hlen("hash");
        System.out.println("判断hash是否存在key2:"+jedis.exists("hash","key2"));

        System.out.println("获取hash中的值:"+jedis.hmget("hash","key3"));
        System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));




    }

SortedSet操作:

 @Test
    public void testSortedSet(){
        jedis.flushDB();
        jedis.lpush("collections","ArrayList","Vector","Stack","HashMap","WeakHashMap","LinkedHashMap");

        System.out.println("collections中的内容:"+jedis.lrange("collections",0,-1));

        SortingParams sortingParams = new SortingParams();
        System.out.println(jedis.sort("collections",sortingParams.alpha()));

        System.out.println("collections内容:"+jedis.lrange("collections",0,-1));

        jedis.lpush("sortedList","3","2","7","1","6","0");
        System.out.println("排序前:"+jedis.lrange("sortedList",0,-1));

        System.out.println("升序:"+jedis.sort("sortedList",sortingParams.asc()));
        System.out.println("降序:"+jedis.sort("sortedList",sortingParams.desc()));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值