我是靠谱客的博主 粗犷爆米花,这篇文章主要介绍【Redis基础知识 十三】Jedis的使用,现在分享给大家,希望可以做个参考。

在了解了Redis的基础数据结构以及一些特性和操作指令之后呢,和kafka及ES系列一样,我们不可能用指令去编程和完成业务逻辑,还是需要代码去操作指令的,java也封装了相关的代码,统一叫做Jedis,接下来本篇blog就来了解下Jedis的使用:

  • Jedis简介:简单介绍下Jedis是什么,能干什么
  • Jedis环境搭建:创建相关Java项目,完成第一个Jedis代码编写
  • Jedis操作Redis:Jedis对Redis的一些操作指令
  • Jedis综合案例实现:Jedis实现我们之前提到的综合案例
  • Jedis简易工具类使用:方便后期Jedis和Redis的连接
  • 可视化客户端:可以可视化查看的Redis客户端

接下来就来学习下Jedis的使用,为之后的接口调用打好基础。

Jedis简介

Jedis实际上就是Java语言操作Redis数据的工具,其实我们之前在用JDBC操作Mysql的时候也是一样的,实际上Redis不也是一个非关系型的数据库嘛!
在这里插入图片描述
Jedis有如下的一些优点:轻量,简洁,便于集成和改造;支持连接池;支持pipelining、事务、LUA Scripting、Redis Sentinel、Redis Cluster,但是需要注意,它不支持读写分离,需要自己实现

Jedis环境搭建

首先还是用idea来创建一个Jedis的maven项目,初始化一个Jedis:
在这里插入图片描述
需要maven加载的配置为:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Jedis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>

Jedis操作Redis

使用如下代码操作Redis,指令与Redis原生指令类似:

String类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.junit.Test; import redis.clients.jedis.Jedis; public class JedisTest { @Test public void testJedis(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); //操作Redis jedis.set("name","tml"); System.out.println(jedis.get("name")); //关闭Redis jedis.close(); } }

可以看到值被打印出来了
在这里插入图片描述

List类型

list操作也很类似,和指令一般无二:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Test public void testList(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); //操作Redis jedis.lpush("faith","tml"); jedis.lpush("faith","gcy"); jedis.lpush("faith","love"); List<String> list = jedis.lrange("faith", 0, -1); for (String item:list) { System.out.println(item); } //关闭Redis jedis.close(); }

在这里插入图片描述

Hash类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test public void testHash(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); //操作Redis jedis.hset("Hash","tml","1"); jedis.hset("Hash","gcy","2"); jedis.hset("Hash","love","3"); Map<String,String> map = jedis.hgetAll("Hash"); System.out.println(map); //关闭Redis jedis.close(); }

在这里插入图片描述

Set类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test public void testSet(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); //操作Redis jedis.sadd("set","tml"); jedis.sadd("set","gcy"); jedis.sadd("set","tml"); Set<String> set = jedis.smembers("set"); System.out.println(set); //关闭Redis jedis.close(); }

在这里插入图片描述

SortedSet类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test public void testSortedSet(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); //操作Redis jedis.zadd("sortedSet",100,"china"); jedis.zadd("sortedSet",99,"aa"); jedis.zadd("sortedSet",89,"cc"); Set<Tuple> tset= jedis.zrangeWithScores("sortedSet",0,-1); System.out.println(tset); //关闭Redis jedis.close(); }

在这里插入图片描述

Jedis综合案例实现

还是我们之前看的按次计时服务案例:
在这里插入图片描述
综合分析如下实现步骤:
在这里插入图片描述
实现代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import redis.clients.jedis.Jedis; import redis.clients.jedis.exceptions.JedisException; //业务服务和管理 public class JedisService { private String id; private int num; public JedisService(String id,int num){ this.id=id; this.num=num; } public void servie(){ //连接Redis Jedis jedis=new Jedis("127.0.0.1",6379); String value= jedis.get("compid"+id); try { if(value==null){ jedis.setex("compid"+id,5,Long.MAX_VALUE-num +""); }else { Long val=jedis.incr("compid"+id); business(id,num-(Long.MAX_VALUE-val)); } } catch (JedisException e){ System.out.println("使用已达上限"); return ; }finally { //关闭Redis jedis.close(); } } public void business(String id,Long val){ System.out.println("用户"+id+"执行业务操作第"+val+"次"); } } //线程管理服务 class MyThread extends Thread{ JedisService jedisService; MyThread(String id,int num){ jedisService =new JedisService(id,num); } public void run(){ while (true){ jedisService.servie(); try { Thread.sleep(300L); } catch (InterruptedException e) { e.printStackTrace(); } } } } class main{ public static void main(String[] args) { MyThread myThread=new MyThread("初级用户",10); MyThread myThread1=new MyThread("高级用户",30); myThread.start(); myThread1.start(); } }

在这里插入图片描述

Jedis简易工具类使用【Jedis连接池】

手动管理连接比较麻烦,使用类似JDBC的连接池方法:
在这里插入图片描述
代码实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPools { @Test public static Jedis testJedisPool(){ //获得连接池配置对象,设置配置项 JedisPoolConfig config = new JedisPoolConfig(); // 最大连接数 config.setMaxTotal(30); //最大空闲连接数 config.setMaxIdle(10); //获得连接池 JedisPool jedisPool = new JedisPool(config,"localhost",6379); return jedisPool.getResource(); } }

更高级的使用方式是配置项放到配置文件,并且把连接池设置为静态资源:
在这里插入图片描述
然后读取连接池

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.ResourceBundle; class JedisPools { private static JedisPool jedisPool=null; static { ResourceBundle resourceBundle= ResourceBundle.getBundle("redis"); JedisPoolConfig config = new JedisPoolConfig(); String host=resourceBundle.getString("redis.host"); String port=resourceBundle.getString("redis.port"); String maxTotal=resourceBundle.getString("redis.maxTotal"); String maxIdle=resourceBundle.getString("redis.maxIdle"); config.setMaxTotal(Integer.parseInt(maxTotal)); config.setMaxIdle(Integer.parseInt(maxIdle)); jedisPool = new JedisPool(config,host, Integer.parseInt(port)); } @Test public static Jedis testJedisPool(){ return jedisPool.getResource(); } }

可视化客户端

使用redis Desktop Manager去查看redis的内容:选择官网下载https://redisdesktop.com/pricing,下载完成后可以看到界面如下所示:

在这里插入图片描述
在这里插入图片描述

最后

以上就是粗犷爆米花最近收集整理的关于【Redis基础知识 十三】Jedis的使用的全部内容,更多相关【Redis基础知识内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部