【 Redis中实现锁功能】---RedisTemplate

一、Redis工具类

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 *
 * @author xxx
 * @date 2022/08/23
 */
@Service
public class RedisUtils {
    
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 添加到队列
     *
     * @param key   the key
     * @param value the value
     * @return the Long
     */
    public Long pushToList(String key, String value) {
    
    
        return redisTemplate.opsForList().leftPush(key, value);
    }

    /**
     * 字符串操作:新增或修改
     *
     * @param key   the key
     * @param value the value
     */
    public void putValue(String key, Object value) {
    
    
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 字符串操作:新增或修改
     *
     * @param key   the key
     * @param value the value
     * @param time  有限时间,单位为秒
     */
    public void putValue(String key, Object value, long time) {
    
    
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    /**
     * 字符串操作:取值
     *
     * @param key the key
     * @return the Object
     */
    public Object getValue(String key) {
    
    
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置有效时间
     *
     * @param key  the key
     * @param time the time
     */
    public void expire(String key, long time) {
    
    
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    /**
     * 删除key
     *
     * @param key the key
     */
    public void remove(String key) {
    
    
        redisTemplate.delete(key);
    }

    /**
     * 批量移除
     *
     * @param keys the keys
     */
    public void removeByKeys(String keys) {
    
    
        Set<String> keysSet = redisTemplate.keys(keys);
        if (keysSet != null) {
    
    
            redisTemplate.delete(keysSet);
        }
    }

    /**
     * redis主键自增
     *
     * @param key 主键值
     * @return long 类型主键
     */
    public Long incrId(final String key) {
    
    
        return (Long) redisTemplate.execute((RedisCallback<Long>) connection -> {
    
    
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            byte[] keys = serializer.serialize("sequence:id_" + key);
            //connection.expire(keys, 90000000);
            if (keys != null) {
    
    
                return connection.incr(keys);
            } else {
    
    
                return 0L;
            }
        });
    }

    /**
     * 新增
     * 如果为空就set值,并返回1
     * 如果存在(不为空)不进行操作,并返回0
     *
     * @param lockKey the lockKey
     * @param value   the value
     * @return the Boolean
     */
    public Boolean setIfAbsent(String lockKey, Object value) {
    
    
        return redisTemplate.opsForValue().setIfAbsent(lockKey, value);
    }

    /**
     * 新增
     * 如果为空就set值,并返回1
     * 如果存在(不为空)不进行操作,并返回0
     *
     * @param lockKey the lockKey
     * @param value   the value
     * @param time    the time
     * @return the Boolean
     */
    public Boolean setIfAbsent(String lockKey, Object value, long time) {
    
    
        return redisTemplate.opsForValue().setIfAbsent(lockKey, value, time, TimeUnit.SECONDS);
    }
}

二、实际应用:

Boolean aBoolean = redisUtils.setIfAbsent(key,value,60);
if(aBoolean){
    
    
    业务逻辑.....
}

猜你喜欢

转载自blog.csdn.net/m0_46459413/article/details/128534950