Skip to content

redis加错未采用原子操作 #1

@li-shaoke

Description

@li-shaoke
boolean isSuccess = redisTemplate.opsForValue().setIfAbsent(businessKey, uniqueValue);
        if (!isSuccess) {
            throw new Exception("You can't do it,because another has get the lock =-=");
        }
        redisTemplate.expire(businessKey, annotation.lockTime(), TimeUnit.SECONDS);

这块redis操作,不是原子的,如果在设置过期时间前,突然宕机了,则加锁后的key将长时间存在,
应该用
Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit);

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions