jedis介绍

约定不等于承诺〃 2022-07-11 08:25 255阅读 0赞

  jedis是redis的java版本的客户端实现。下面演示jedis的相关操作:
  
  首先在eclipse新建动态web工程:
  
  image\_1b6tlp75ltr41kcd1vurkheah89.png-41.6kB
  
  将jedis所需的jar包导入到工程中:
  
  image\_1b6toosmh8iuv9h107gach19e6m.png-8.7kB
  
  编写Java代码测试连通性:

  1. import redis.clients.jedis.Jedis;
  2. public class TestPing {
  3. public static void main(String[] args) {
  4.   //192.168.202.131是我虚拟机的IP地址,6379是redis服务的端口号,请自行配置
  5. Jedis jedis = new Jedis("192.168.202.131", 6379);
  6. System.out.println(jedis.ping());
  7. }
  8. }

  运行程序,会报错:
  Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
  
  我们需要更改redis的配置文件,将bind 127.0.0.1注释掉,使任何IP都可以访问redis服务:
  
  image\_1b6tp9atqk1c13l21at9pi964o13.png-108.2kB
  
  重启redis服务,再运行程序,发现继续报错:
  
  Exception in thread “main” redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
  
  大意是redis在保护模式下,拒绝在没有绑定IP地址或者没有授权密码的情况下进行访问。我们需要继续更改redis配置文件,将protected-mode yes改为protected-mode no:
  
  image\_1b6ttt2ro11oenofng81ccn1clv1g.png-97.5kB
  
  重启redis服务,重新运行程序,会输出“PONG”,证明连接成功:
  
  image\_1b6ttupvq8hdeu0v2k1enl1ev81t.png-14.8kB
  
  下面根据数据类型来测试jedis的API:

  1. //String类型
  2.   @Test
  3. public void testString() {
  4. Jedis jedis = new Jedis("192.168.202.131", 6379);
  5. //清空数据库
  6. jedis.flushDB();
  7. String k1 = "k1";
  8. String v1 = "v1";
  9. jedis.set("k1", v1);
  10. System.out.println(jedis.get(k1));
  11. //是否存在
  12. System.out.println(jedis.exists(k1));
  13. //类型
  14. System.out.println(jedis.type(k1));
  15. //拼接
  16. jedis.append(k1, "v1v1v1");
  17. System.out.println(jedis.get(k1));
  18. //设置多个数据
  19. jedis.mset("k2","v2","k3","3");
  20. System.out.println(jedis.keys("*"));
  21. //删除
  22. jedis.del("k2");
  23. System.out.println(jedis.keys("*"));
  24. //自增
  25. jedis.incr("k3");
  26. System.out.println(jedis.get("k3"));
  27. }

  输出:
  
  image\_1b6u762sg11b31bn9i9t17621gff9.png-19.7kB   

  1. //List类型
  2. @Test
  3. public void testList(){
  4. Jedis jedis = new Jedis("192.168.202.131", 6379);
  5. //清空数据库
  6. jedis.flushDB();
  7. //从列表头部插入
  8. jedis.lpush("l1", "a","b","c");
  9. //从列表尾部插入
  10. jedis.rpush("l1", "d","e");
  11. //返回列表中所有值
  12. System.out.println(jedis.lrange("l1", 0, -1));
  13. //返回列表长度
  14. System.out.println(jedis.llen("l1"));
  15. //从列表头部移除一个元素并返回
  16. System.out.println(jedis.lpop("l1"));
  17. }

  输出:
  
  image\_1b6u87tfdc1g1lvhptjcv1l9lm.png-13.7kB   

  1. //Hash类型
  2. @Test
  3. public void testHash(){
  4. Jedis jedis = new Jedis("192.168.202.131", 6379);
  5. //清空数据库
  6. jedis.flushDB();
  7. Map<String,String> map = new HashMap<>();
  8. map.put("name", "Alan");
  9. map.put("age", "22");
  10. jedis.hmset("user",map);
  11. jedis.hset("user", "gender", "male");
  12. //获取单个数据
  13. System.out.println(jedis.hget("user", "age"));
  14. //获取多个数据
  15. System.out.println(jedis.hmget("user", "name","gender"));
  16. //获取所有数据
  17. System.out.println(jedis.hgetAll("user"));
  18. //获取所有的键
  19. System.out.println(jedis.hkeys("user"));
  20. //获取所有的值
  21. System.out.println(jedis.hvals("user"));
  22. }

  输出:
  
  image\_1b6u89aa51egu1dakmtqtuebhd13.png-22.5kB   

  1. // Set类型
  2. @Test
  3. public void testSet() {
  4. Jedis jedis = new Jedis("192.168.202.131", 6379);
  5. // 清空数据库
  6. jedis.flushDB();
  7. //添加一个或多个数据
  8. jedis.sadd("s1", "a","b","c");
  9. jedis.sadd("s1", "d");
  10. //返回集合中所有元素
  11. System.out.println(jedis.smembers("s1"));
  12. //成员个数
  13. System.out.println(jedis.scard("s1"));
  14. //求交集,并集,差集
  15. jedis.sadd("s2", "c","d","e","f");
  16. System.out.println(jedis.sinter("s1","s2"));
  17. System.out.println(jedis.sunion("s1","s2"));
  18. System.out.println(jedis.sdiff("s1","s2"));
  19. //判断是否是成员
  20. System.out.println(jedis.sismember("s1", "c"));
  21. //移除一个或者多个元素
  22. jedis.srem("s1", "b","c");
  23. System.out.println(jedis.smembers("s1"));
  24. }

  输出:

  image\_1b6u8sf9u1hk2dc21g3o1m5l5p71g.png-12.8kB   

  1. // Zset类型
  2. @Test
  3. public void testZset() {
  4. Jedis jedis = new Jedis("192.168.202.131", 6379);
  5. // 清空数据库
  6. jedis.flushDB();
  7. //向有序集合中添加多个成员
  8. Map<Double,String> map = new HashMap<>();
  9. map.put(60.0, "Alan");
  10. map.put(70.0, "Jack");
  11. map.put(75.0, "Tom");
  12. jedis.zadd("rank", map);
  13. //返回所有成员
  14. System.out.println(jedis.zrange("rank", 0, -1));
  15. //带有分数地返回所有成员,字符串由ASCII码表示
  16. System.out.println(jedis.zrangeWithScores("rank", 0, -1));
  17. //增加成员分数
  18. jedis.zincrby("rank", 0.5, "Alan");
  19. System.out.println(jedis.zscore("rank", "Alan"));
  20. //返回分数在60到70之间的成员
  21. System.out.println(jedis.zrangeByScore("rank", 60, 70));
  22. }

  输出:
  
  image\_1b6u9nmnpk56b6dp26hj611en1t.png-29.1kB   

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Jedis

    redis 应用场景 缓存(数据查询、短连接、新闻内容、商品内容等等) 聊天室的在线好友列表 任务队列。(秒杀、抢购、12306等等) 应用排行榜

    相关 jedis使用

    一、Redis Client介绍 1.1、简介 Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用。 Jedis源

    相关 Jedis 初探

    Jedis是java操作redis的jar包 先说一下如何连接redis 比如在linux操作系统上的话,进入目录,输入 "./redis cli"即可登录。 如果设置了

    相关 Jedis

    参考资料 Redis 单点模式和集群模式代码测试及问题记录:https://blog.csdn.net/boonya/article/details/49466003 Je

    相关 Jedis连接

    Jedis连接 到场api中的jedis。我们能够发现,jedis类提供了4个构造方法。都可用于连接: ![20141009172808406][] ![Cente