dawdler-client-plug-jedis
Module introduction
Implements injection of JedisOperator on the web side.
1. Add the dependency to the pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-client-plug-jedis</artifactId>
2. Usage
2.1 Using Jedis
Simply annotate a field of type JedisOperator with @JedisInjector.
@Controller
public class UserController{
@JedisInjector("myRedis")//myRedis is the name of the configuration file, without the properties suffix
JedisOperator jedisOperator;
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
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.
@Controller
public class UserController implements UserService {
@JedisLockInjector(fileName = "myRedis")//myRedis is the name of the configuration file, without the properties suffix
JedisDistributedLockHolder jedisDistributedLockHolder;
@RequestMapping(value = "/synUser", method = RequestMethod.POST)
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();
}
}
@RequestMapping(value = "/synUserNoWait", method = RequestMethod.POST)
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();
}
}
}