Redis-操作测试
2021/9/5 19:41:32
本文主要是介绍Redis-操作测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Jedis所需要的jar包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
连接Redis注意事项
禁用Linux的防火墙:Linux(CentOS7)里执行命令 systemctl stop/disable firewalld.service redis.conf中注释掉bind 127.0.0.1 ,然后 protected-mode no
创建测试程序
import org.junit.Test; import redis.clients.jedis.Jedis; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public class JedisDemo1 { public static void main(String[] args) { //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); //测试 String value = jedis.ping(); System.out.println(value); } //操作key @Test public void demo1(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); //添加 jedis.set("name","lucy"); //获取 String name = jedis.get("name"); System.out.println(name); Set<String> keys = jedis.keys("*"); for (String key : keys) { System.out.println(key); } } //操作String @Test public void demo2(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); //设置多个key-value jedis.mset("k1","v1","k2","v2"); List<String> mget = jedis.mget("k1", "k2"); System.out.println(mget); } //操作List @Test public void demo3(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); jedis.lpush("key1","lucy","mary","jack"); List<String> value = jedis.lrange("key1", 0, -1); System.out.println(value); } //操作set @Test public void demo4(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); jedis.sadd("name","lucy","jack"); Set<String> smembers = jedis.smembers("name"); for (String name : smembers) { System.out.println(name); } jedis.srem("name","lucy"); } //操作hash @Test public void demo5(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); jedis.hset("users","age","20"); String hget = jedis.hget("users", "age"); System.out.println(hget); Map<String,String> map = new HashMap<String,String>(); map.put("telphone","13810169999"); map.put("address","donglin"); map.put("email","abc@163.com"); jedis.hmset("hash2",map); List<String> result = jedis.hmget("hash2", "telphone","email"); for (String element : result) { System.out.println(element); } } //操作zset @Test public void demo6(){ //创建Jedis对象 Jedis jedis = new Jedis("192.168.121.160", 6379); jedis.zadd("zset01", 100d, "成都"); jedis.zadd("zset01", 90d, "上海"); jedis.zadd("zset01", 80d, "北京"); jedis.zadd("zset01", 70d, "广州"); Set<String> zrange = jedis.zrange("zset01", 0, -1); for (String e : zrange) { System.out.println(e); } } }
这篇关于Redis-操作测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24Redis资料:新手入门快速指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧