Redis分布式锁的代码陷阱

首先来看一段代码:建立锁
package com.cc.api.impl.m.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**

  • @author

  • @date

  • @desc redis分布式可重入锁
    */
    @Component
    @Slf4j
    public class RedisLock {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private static StringRedisTemplate redisTemplateString;

    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
    this.redisTemplate = redisTemplate;
    }

    public RedisTemplate getRedisTemplate() {
    return this.redisTemplate;
    }
    @Autowired
    public void setRedisTemplateString(StringRedisTemplate redisTemplateString) {
    RedisLock.redisTemplateString = redisTemplateString;
    }

    public static StringRedisTemplate getRedisTemplateString() {
    return redisTemplateString;
    }

    /**

    • 获取锁
    • @param lockName 锁名称
    • @param releaseTime 超时时间(单位:时间)
    • @return key 解锁标识
      */
      public boolean getLock(String lockName,String threadId,Date releaseTim

你可能感兴趣的:(redis,分布式,数据库)