Jedis简单入门

Jedis是用于操作redis中存储的数据,为什么要用Jedis?如果你熟悉redis的命令的话Jedis中的方法和Redis的命令如出一辙,当然操作redis中的数据还有SpringData Redis、Lettuce。

jar包的下载地址:

https://mvnrepository.com/artifact/redis.clients/jedis

mvn的坐标导入

<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.9.0</version>
</dependency>

入门案例:

 @Test
    public void test1(){
        Jedis jedis = new Jedis("localhost",6379);
//        jedis.set("name","兰交余文乐");
        String name=jedis.get("name");
        System.out.println(name);
        jedis.close();
    }

案例:

人工智能领域的语义识别与自动对话将是未来服务业机器人应答呼叫体系中的重要技术,百度自研用户评
价语义识别服务,免费开放给企业试用,同时训练百度自己的模型。现对试用用户的使用行为进行限速,
限制每个用户每分钟最多发起10次调用
要求:①A、B、C三个用户 ②A用户 限制 10次每分钟,B用户30次每分钟,C用户不限制次数
  public static void main(String[] args) {
        ClientServer c1=new ClientServer("UserA",10);
        ClientServer c2=new ClientServer("UserB",20);
        ClientServer c3=new ClientServer("UserC",Long.MAX_VALUE);
        MyThread t1=new MyThread(c1);
        MyThread t2=new MyThread(c2);
        MyThread t3=new MyThread(c3);
        t1.setName("UserA");
        t2.setName("UserB");
        t3.setName("UserC");
        t1.start();
        t2.start();
        t3.start();
    }
class  ClientServer{
    String userName;
    long count;
    ClientServer( String userName,long count){
        this.userName=userName;
        this.count=count;
    }
    public void  callMethod(String userName,long count){
        Jedis jedis=JedisPoolUtils.getInstance();
        String value= jedis.get(userName);
        String maxVal=String.valueOf(Long.MAX_VALUE-count);
       try{
            if (value==null){
                jedis.setex(userName,30,maxVal);
            }else {

                    jedis.incr(userName);
                    long c=Long.parseLong(jedis.get(userName))-Long.parseLong(maxVal);
                    bussiness(userName,c);
            }
    }catch (JedisDataException e){
        System.out.println(userName+"体验次数已用完,请升级成会员");
    }finally {
        jedis.close();
    }
    }
    public  void  bussiness(String userName,long c){
        System.out.println(userName+"业务操作执行第"+c+"次");

    }
}
class MyThread extends  Thread{
    private ClientServer c;
    private ReentrantLock lock=new ReentrantLock();
    MyThread(ClientServer c){
        this.c=c;
    }
    @Override
    public void run() {
        while (true){
            lock.lock();
            c.callMethod(c.userName,c.count);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
        }
    }
}
public class JedisPoolUtils {
    static String host;
    static String port;
    static  String maxTotal;
    static String maxIdle;
    static JedisPoolConfig poolconfig;
    static JedisPool jedisPool;
    static {
        ResourceBundle resourceBundle=ResourceBundle.getBundle("jedis");
        host=resourceBundle.getString("jedis.host");
        port=resourceBundle.getString("jedis.port");
        maxTotal=resourceBundle.getString("jedis.maxTotal");
        maxIdle=resourceBundle.getString("jedis.maxIdle");
        poolconfig=new JedisPoolConfig();
        poolconfig.setMaxTotal(Integer.parseInt(maxTotal));
        poolconfig.setMaxIdle(Integer.parseInt(maxIdle));
        jedisPool=new JedisPool(poolconfig,host,Integer.parseInt(port));
    }
    public  static  Jedis getInstance(){
        Jedis jedis=jedisPool.getResource();
        return jedis;
    }

}
jedis.host=localhost
jedis.port=6379
jedis.maxTotal=30
jedis.maxIdle=10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值