李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
package com.chinaztt.mes.basic.util;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
/**
 * @Author: Zero
 * @Date: 2022/10/21 10:39
 * @Description:
 */
@Component
public class RedisUtils {
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    /**
     * 递增
     * <p>最终redis中的key为 businessName:prefix</p>
     *
     * @param businessName 业务名
     * @param prefix       前缀
     * @param delta        要增加几(大于0)
     * @return
     */
    public long incr(String businessName, String prefix, long delta) {
        if (delta < 0) {
            throw new RuntimeException("步长必须大于0");
        }
        return redisTemplate.opsForValue().increment(businessName + ":" + prefix, delta);
    }
}