jedis操作string和jedis操作hash
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(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)