Giter Club home page Giter Club logo

redis-kit's Introduction

Redis Distributed Kit

this repo uses local sync lock and redis lock to provide high performance redis tools

distribute_lock

Import Redis Kit in your project

<dependency>
    <groupId>com.github.jsrdxzw</groupId>
    <artifactId>redis-kit-spring-boot-starter</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> 
import com.jsrdxzw.redis.core.EnableRedisKit;

@EnableRedisKit
public class SpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class, args);
    }
}

Use Distributed Lock

by default StringRedisTemplate is used, Of course you can choose other RedisTemplate by yourself.

@Configuration
public class DistributedLockConfiguration{
    @Bean
    public RedisLockFactory redisLockFactory(StringRedisTemplate redisTemplate){
        return new DefaultRedisLockFactory(redisTemplate);
    }
}

use lock in your own business logic code

public class UserService{
    @Autowired
    private RedisLockFactory redisLockFactory;
    public void method() {
        RedisLock RLock = redisLockFactory.getLock("xzw");
        try {
            //RLock.lock();
            RLock.tryLock(30, TimeUnit.SECONDS);
            //RLock.tryLock(30, TimeUnit.SECONDS, 3);
            // your own logic
        } finally{
            RLock.unlock();
        }
    }
}

In the other way, annotations such as @DistributedLock, DistributedTryLock are also provided, please import the spring aop at the first place before using annotations.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;@ComponentScan

@SpringBootApplication(scanBasePackages = {"your.path", "com.jsrdxzw.redis"})
public class Application{
    public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    }
}

example of distributed lock by annotation

// @DistributedLock(lockKey = "your key")
@DistributedTryLock(lockKey = "your key", waitTime = 10)
public void method() {
   //...
}

Redis Cache Example

it will get value from redis and if the key does not exist in redis it will go on next process and put value in redis as cache. by default the expired time is 5 minutes.

@Cache(key="xzw")
public Student methodName() {
}

it will remove redis value based on cache principle -- Cache aside

it is recommended to use @Transactional annotation when modifying cache values

@Transactional(rollbackFor = Throwable.class)
@Put(key="xzw")
public Student methodName() {
}

@Delete is same as @Put

@Transactional(rollbackFor = Throwable.class)
@Delete(key="xzw")
public void methodName() {
}

it will delete value from redis

rate limiter

import org.springframework.beans.factory.annotation.Autowired;

@Autowired
private RateLimit rateLimit;

boolean require = rateLimit.acquire("xzw", 5, 10);

it means we allow 5 requests per seconds, and when the request of per second is greater than 5, it will return false

we have provided two algorithms -- token bucket and rolling window. token bucket algorithm is used by default

# you can change limit algorithm by overriding spring yaml file
redis-kit:
  rate-limit: rollingWindow

redis-kit's People

Contributors

jsrdxzw avatar

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.