The role and usage of incr, incrby, decr, and decrby commands

In redis, incr, incrby, decr, and decrby belong to string data structures, which are atomic increment or decrement operations.

  • incr increments by 1 and returns the incremented result;
  • incrby increments or decrements according to the specified value and returns the incremented or decremented result (incrby increments or decrements depends on the positive or negative of the incoming value);
  • Decrerate decr by 1 and return the decremented result;
  • Decrby performs increment or decrement operation according to the specified value and returns the result after increment or decrement (decrby increment or decrement depends on the positive or negative of the incoming value);

Set incr:key and decr:key to observe usage and execution results

1. Observe the value of incr:key for the first time, and you can see that there is no result

127.0.0.1:6381> GET incr:key

127.0.0.1:6381>

2. Execute the incr command on incr:key , you can see that redis returns the incremented result 1. The incr command will return the incremented result

127.0.0.1:6381> incr incr:key
1
127.0.0.1:6381>

3. Execute the incrby command on incr:key , and you can see that redis returns the incremented result 5. The incrby command will return the incremented result

127.0.0.1:6381> incrby incr:key 4
5
127.0.0.1:6381>

4. Execute the incrby command on incr:key , this time pass in a negative number, you can observe that redis returns the decremented result 2

127.0.0.1:6381> incrby incr:key -3
2
127.0.0.1:6381>

5. Execute the incrby command on the incr:key , and also pass in a negative number. It can be observed that the result of the incr:key becomes a negative number after the decrement

127.0.0.1:6381> incrby incr:key -4
-2
127.0.0.1:6381>

6. The first observation of decr:key, there is no result

127.0.0.1:6381> GET decr:key

127.0.0.1:6381>

7. Execute the decr command on decr:key, you can see that redis returns the decremented result

127.0.0.1:6381> decr decr:key
-1
127.0.0.1:6381>

8. Execute the decrby command on decr:key, you can see that redis returns the decremented result, the value here is 4 instead of -4

127.0.0.1:6381> decrby decr:key 4
-5
127.0.0.1:6381>

9. Execute the decrby command on decr:key, this time enter a negative number and observe the result

127.0.0.1:6381> decrby decr:key -1
-4
127.0.0.1:6381>

 

并发递增和递减

  做一个实验,并发100个线程对同一个key做操作,其中50个执行incr命令,另外50个执行decr命令,观察一下结果

复制代码
/**
 * 并发100个线程,50个线程做decr命令,另外50个线程做incr命令
 *
 * @author tianshu on 16/10/31 上午10:52.
 */
public class MultipleDecrIncr {

    /** decr线程数量 */
    private static final int DECR_THREAD_COUNT = 50;
    /** incr线程数量 */
    private static final int INCR_THREAD_COUNT = 50;

    private static CountDownLatch begin = new CountDownLatch(DECR_THREAD_COUNT + INCR_THREAD_COUNT);

    private static CountDownLatch finish = new CountDownLatch(DECR_THREAD_COUNT + INCR_THREAD_COUNT);

    static final String KEY = "string:decr:incr";


    public static void main(String[] args) throws InterruptedException {

        for(int i = 0; i < DECR_THREAD_COUNT; ++i) {
            new DecrThread().start();
            begin.countDown();
        }

        for(int i = 0; i < INCR_THREAD_COUNT; ++i) {
            new IncrThread().start();
            begin.countDown();
        }

        finish.await();

        JedisConnect jedisConnect = JedisConnect.getJedisConnect();
        Jedis jedis = jedisConnect.getJedis();

        String value = jedis.get(KEY);
        System.out.println(value);

        jedisConnect.releaseJedis(jedis);
    }

    /**
     * decr命令线程
     */
    static class DecrThread extends Thread {

        @Override
        public void run() {

            try {
                begin.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            JedisConnect jedisConnect = JedisConnect.getJedisConnect();
            Jedis jedis = jedisConnect.getJedis();

            jedis.decr(KEY);
            jedisConnect.releaseJedis(jedis);

            finish.countDown();
        }
    }

    /**
     * incr命令线程
     */
    static class IncrThread extends Thread {

        @Override
        public void run() {

            try {
                begin.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            JedisConnect jedisConnect = JedisConnect.getJedisConnect();
            Jedis jedis = jedisConnect.getJedis();

            jedis.incr(KEY);
            jedisConnect.releaseJedis(jedis);

            finish.countDown();
        }

    }

}
复制代码

 

应用场景

  个人觉得这类命令一般会应用到计数器场景

  • 单号生成:根据业务生成key,每当需要单号时可以使用incr获得一个新的序列号。
  • 错误拦截:比如有的网站账号密码输入错误N次之后,会做一些特殊处理;使用incr是实现这种功能的方式之一,可以根据用户的特殊标识表示key,每当账号密码输错时使用incr命令做递增。
  • 非法拦截:某段时间限制同IP请求同一接口次数

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859543&siteId=291194637