Giter Site home page Giter Site logo

redlock-spring-boot's Introduction

redlock-spring-boot

Build Status codecov

Distributed redis lock implemented and integrated with spring boot.

Usage

Maven dependency

To use redlock-spring-boot-starter, you import dependency into your POM.xml:

<dependency>
    <groupId>com.lomagicode.spring.boot</groupId>
    <artifactId>redlock-spring-boot-starter</artifactId>
    <version>${spring-boot-redlock.version}</version>
</dependency>

Use in code

Redlock-spring-boot is very easy-to-use, it exposed a @RedisLock annotation which you can just add to your method that you want to be executed in a redis lock. For example:

@Component
public class Example {
    @RedisLock(lockKey="your-lock-key", expire=10, timeUnit=TimeUnit.SECONDS)
    public void doTask() {
        try {
            // Do some task...
        } catch (Exception e) {
            // Handle exception.
        }
    } 
}

With above code, when you call Example#doTask(), it will try to aquire and hold the lock before really executing the method. if it failed to aquire the lock, the method wouldn't be executed which was as expected.

What should be noticed

Because @RedisLock is implemented with spring aop, the Example class should must be a spring containered bean.

Exception in code wrapped with @RedisLock annotation MUST be caught and handled.

That's all, enjoy it!

redlock-spring-boot's People

Contributors

hatakawas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

redlock-spring-boot's Issues

可以锁动态值吗?

看例子中注解中的key是常量,如果想根据方法入参来锁的话能否实现呢,如锁商品id

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.