dawdler-server-plug-jedis
Module introduction
Implements injection of JedisOperator on the dawdler-server side.
1. Add the dependency to the pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-server-plug-jedis</artifactId>
2. Usage
2.1 Using Jedis
Simply annotate a field of type JedisOperator with @JedisInjector.
public class UserServiceImpl implements UserService {
@JedisInjector("myRedis")//myRedis is the name of the configuration file, without the properties suffix
JedisOperator jedisOperator;
public User getUser(String userId) {
jedisOperator.set("userId", userId);//use the jedisOperator object
return null;
}
}
2.2 Using distributed locks
Simply annotate a field of type JedisDistributedLockHolder with @JedisLockInjector.
public class UserServiceImpl implements UserService {
@JedisLockInjector(fileName = "myRedis")//myRedis is the name of the configuration file, without the properties suffix
JedisDistributedLockHolder jedisDistributedLockHolder;
public void synUser(String userId, Business business) {
JedisDistributedLock lock = jedisDistributedLockHolder.createLock("lockKey:"+userId);
try {
lock.lock();
// lock.lock(3000); //wait up to 3 seconds for the lock
// Simulate business processing
doSomething(business);
} finally {
lock.unlock();
}
}
public void synUserNoWait(String userId, Business business) {
JedisDistributedLock lock = jedisDistributedLockHolder.createLock("lockKey:"+userId);
try {
//If the lock cannot be acquired, finish immediately without waiting
if(lock.tryLock()){
// Simulate business processing
doSomething(business);
}
} finally {
lock.unlock();
}
}
}