this repo uses local sync lock and redis lock to provide high performance redis tools
<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);
}
}
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);
}
}
// @DistributedLock(lockKey = "your key")
@DistributedTryLock(lockKey = "your key", waitTime = 10)
public void method() {
//...
}
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
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