首页 > 其他分享 >jedis操作string和jedis操作hash

jedis操作string和jedis操作hash

时间:2023-02-14 09:22:50浏览次数:28  
标签:hash string jedis user 操作 Jedis

Jedis操作string

Jedls操作各种redis中的数据结构
  (1)字符串类型string
    set
    get
(2)哈希类型hash : map格式
    hset
    hget
(3)列表类型list : linkedlist格式。支持重复元秦
    lpush / rpush
    lpop / rpop
(4)集合类型set:不允许重复元秦
    sadd
(5)有序集合类型sortedset :不允许重复元素,且元素有顺序
    zadd

案例

/**
* string 数据结构操作
*/
@Test
public void test2(){
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);//如果使用空参构造,默认值 “localhost”,6379端口额
//2.操作
//存储
jedis.set("username","zhangsan");
//获取
String username = jedis.get("username");
System.out.println(username);

//可以使用setex()方法存储可以指定过期时间的key value
jedis.setex("activecode",20,"hehe");//将activecode: hehe键值对存入redis,并且20秒后自动删除该键值对

//3.关闭连接
jedis.close();
}

 jedis操作hash

/**
     * hash 数据结构操作
     */
    @Test
    public void test3(){
        //1.获取连接
        Jedis jedis = new Jedis("localhost", 6379);//如果使用空参构造,默认值 “localhost”,6379端口额
        //2.操作
        //存储
        jedis.hset("user","name","lisi");
        jedis.hset("user","age","26");
        jedis.hset("user","gender","male");

        //获取hash
        String name = jedis.hget("user", "name");
        System.out.println(name);

        //获取hash的所有map中的数据
        Map<String, String> user = jedis.hgetAll("user");

        //keyset
        Set<String> keySet = user.keySet();
        for (String key : keySet){
            //获取value
            String value = user.get(key);
            System.out.println(key+":"+value);
        }

        //3.关闭连接
        jedis.close();
    }

 

标签:hash,string,jedis,user,操作,Jedis
From: https://www.cnblogs.com/xuche/p/17117033.html

相关文章