hiyouka / common-redis

:paperclip:redis tools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

common-redis

RedisLock

RedisLock have tow implementation class:
DefauleRedisLock : not support reentrant lock
ReentrantRedisLock : support thread reentrant

Quick Start:

Example:

1.get bean

@Configuration
public class RedisLimitConfig {
  
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Bean
    public RedisLock redisLock(){
        return LockPayLoad.newBuilder()
                .lockType(ReentrantRedisLock.class) //default DefaultRedisLock
                .redisType(RedisType.SINGLE)        //default single
                .sleepTime(100)                     //default 100
                .lockPrefix("lock_")                //default lock_
                .build(jedisConnectionFactory);
    }
    
    @Bean
    public RedisLock redisLock(){
        return LockPayLoad.newBuilder()
                .lockType(ReentrantRedisLock.class) //default DefaultRedisLock
                .redisType(RedisType.SINGLE)        //default single
                .sleepTime(100)                     //default 100
                .lockPrefix("lock_")                //default lock_
                .build(redisTemplate);
    }
}

2.use bean

public class Test{
    @Autowired
    private RedisLock redisLock ;

    public void use() {
        String key = "key";
        String token = UUID.randomUUID().toString();
        try {
            boolean locked = redisLock.tryLock(key, token);
            if (!locked) {
                return;
            }
            //do something
        } finally {
            redisLock.unlock(key,token) ;
        }

    }
    
    public void useBlocking() {
            String key = "key";
            String token = UUID.randomUUID().toString();
            try {
                redisLock.lock(key, token);
                //do something
            } finally {
                redisLock.unlock(key,token) ;
            }
    
        }
    
}

About

:paperclip:redis tools

License:MIT License


Languages

Language:Java 96.5%Language:Lua 3.5%