table of Contents
Jedis
Introduction to Jedis
Java language connection redis service
- Jedis
Programming language and redis
- Java language connection redis service
- Jedis
- SpringData Redis
- Lettuce
- C 、C++ 、C# 、Erlang、Lua 、Objective-C 、Perl 、PHP 、Python 、Ruby 、Scala
- Visually connect redis client
- Redis Desktop Manager
- Redis Client
- Redis Studio
HelloWorld(Jedis版)
- jar package import
- Download link: https://mvnrepository.com/artifact/redis.clients/jedis
- Based on maven
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
Client connects to redis
- Connect redis
Jedis jedis = new Jedis("localhost", 6379);
- Operate redis
jedis.set("name", "itheima"); jedis.get("name");
- Close redis connection
jedis.close();
Example
Use list example
Example of using hash
- API documentation: http://xetorthio.github.io/jedis/
Case: Control of the number of service calls
- Semantic recognition and automatic dialogue in the field of artificial intelligence will be the next important technical call answering service robot system, the Baidu since the inquiry user evaluation value of semantic recognition service, open to free enterprise trial, while training Baidu own models. The rate of trial users’ behavior is now limited, and each user is limited to a maximum of 10 calls per minute
- Case requirements
- ① Set up three users A, B, C
- ② User A is limited to 10 calls per minute, user B is limited to 30 calls per minute, and user C is not limited
- Case: Implementation steps
- 1. Set up business methods
void business(String id,long num){ System.out.println("用户"+id+"发起业务调用,当前第"+num+"次"); }
- 2. Set multi-line classes to simulate user calls
public void run(){ while(true){ jd.service(id); //模拟调用间隔,设定为1.x秒 try{ Random r = new Random(); Thread.sleep(1000+ r.nextInt(200)); }catch (InterruptedException e){ e.printStackTrace();; } } }
- Design redis control scheme
void service(String id){ Jedis jedis = new Jedis("localhost", 6379); String value = jedis.get("compid:" + id); //判定是否具有调用计数控制,利用异常进行控制处理 if(value == null) { //没有控制,创建控制计数器 jedis.setex("compid:" + id, 20, ""+(Long.MAX_VALUE-10)); }else{ //有控制,自增,并调用业务 try{ Long val = jedis.incr("compid:"+id); business(id,10+val-Long.MAX_VALUE); }catch (JedisDataException e){ //调用次数溢出,弹出提示 System.out.println("用户:"+id+"使用次数已达到上限,请稍后再试,或升级VIP会员"); return; }finally{ jedis.close(); } } }
- Design and start the main program
public static void main(String[] args) { MyThread t1 = new MyThread("初级用户"); t1.start(); }
Jedis simple tool development
Package connection parameters
- jedis.properties
jedis.host=localhost jedis.port=6379 jedis.maxTotal=30 jedis.maxIdle=10
Load configuration information
- Static code block to initialize resources
static{ //读取配置文件 获得参数值 ResourceBundle rb = ResourceBundle.getBundle("jedis"); host = rb.getString("jedis.host"); port = Integer.parseInt(rb.getString("jedis.port")); maxTotal = Integer.parseInt(rb.getString("jedis.maxTotal")); maxIdle = Integer.parseInt(rb.getString("jedis.maxIdle")); poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(maxTotal); poolConfig.setMaxIdle(maxIdle); jedisPool = new JedisPool(poolConfig,host,port); }
Get connection
- External access interface, providing jedis connection object, connection is obtained from the connection pool
public static Jedis getJedis(){ Jedis jedis = jedisPool.getResource(); return jedis; }
Example
- Basic version
- Improved version
- Establish redis configuration file
- Create connection pool
- Other file usage
- [Note]: Refer to the Dark Horse Redis tutorial: https://www.bilibili.com/video/BV1AE411j7Wq?t=5