<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
Jedis jedis = new Jedis("localhost");
jedis.set("foo", "bar");
String value = jedis.get("foo");
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
jedis.set("foo", "bar");
String foobar = jedis.get("foo");
jedis.zadd("sose", 0, "car");
jedis.zadd("sose", 0, "bike");
Set<String> sose = jedis.zrange("sose", 0, -1);
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
JedisPool pool = new JedisPool(config, "192.168.245.153",6379);
try (Jedis jedis = pool.getResource()) {
jedis.set("foo", "bar");
String foobar = jedis.get("foo");
jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike");
Set<String> sose = jedis.zrange("sose", 0, -1);
}
pool.close();
Jedis jedis = new Jedis("192.168.245.153",6379);
jedis.append("foo", "bar");
jedis.lpush("s", "1","2","3","4");
Jedis jedis = new Jedis("192.168.245.153",6379);
jedis.hset("hash", "key1", "v1");
jedis.hset("hash", "key2", "v2");
jedis.hset("hash", "key3", "v4");
String hget = jedis.hget("hash", "key3");
System.out.println(hget);
jedis.sadd("set", "1","2","3");
Set<String> smembers = jedis.smembers("set");
System.out.println(smembers.toString());
Map<String,Double> scoreMembers = new HashMap<>();
scoreMembers.put("a", 1d);
scoreMembers.put("c", 3d);
scoreMembers.put("t", 2d);
jedis.zadd("st", scoreMembers);
Long zcard = jedis.zcard("st");
ScanResult<Tuple> zscan = jedis.zscan("st", ScanParams.SCAN_POINTER_START);
List<Tuple> result = zscan.getResult();
Iterator<Tuple> iterator = result.iterator();
while (iterator.hasNext()) {
Tuple next = iterator.next();
String element = next.getElement();
System.out.println(element);
}
Jedis jedis = new Jedis("192.168.245.153",6379);
Person p1 = new Person();
p1.setAge("20");
p1.setName("joy");
p1.setId(1);
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(p1);
byte[] byteArray = bout.toByteArray();
jedis.set("person:1".getBytes(), byteArray);
} catch (IOException e) {
e.printStackTrace();
}
byte[] bs = jedis.get("person:1".getBytes());
ObjectInputStream oin;
try {
ByteArrayInputStream bin = new ByteArrayInputStream(bs);
oin = new ObjectInputStream(bin);
Person p = (Person)oin.readObject();
System.out.println(p.getName());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Jedis jedis = new Jedis("192.168.245.153",6379);
Person p1 = new Person();
p1.setAge("20");
p1.setName("joy");
p1.setId(1);
try {
Map<String, Object> bean2map = BeanUtils.bean2map(p1);
Map<String,String> map = new HashMap<>();
Set<Entry<String, Object>> entrySet = bean2map.entrySet();
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, Object> next = iterator.next();
map.put(next.getKey(), String.valueOf(next.getValue()));
}
jedis.hmset("person", map);
} catch (Exception e) {
e.printStackTrace();
}
String hget = jedis.hget("person", "name");
System.out.println(hget);
jedis.lpush("s", "1","2","3","4");
List<String> sort = jedis.sort("s",new SortingParams().desc());
System.out.println(sort);
jedis.lpush("s", "1","2","3","4");
List<String> sort = jedis.sort("s",new SortingParams().asc());
System.out.println(sort);
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!