对于Redis的学习,除了底层的原理,笔者认为,如何在各个场景中合理的设计使用Redis也是十分的关键的,Redis提供了五种数据类型,可以让我们在更多的场景中做出选择。接下来笔者就从实际案例出发,首先从Redis的分布式锁开始。在日常生活中,我们肯定有接触过抢票(抢商品)的场景,那么这个时候就会存在并发问题,下面的代码主要是在模拟一个抢票的场景。
RedisLockController
@RestController
public class RedisLockController {
private static long count = 20;
private CountDownLatch countDownLatch = new CountDownLatch(5);
@Resource(name="redisLock")
private RedisLock lock;
@RequestMapping(value = "/sale", method = RequestMethod.GET)
public Long sale() throws InterruptedException {
count = 20;
countDownLatch = new CountDownLatch(5);
System.out.println("-------五个窗口开售20张票-------");
new PlusThread().start();
new PlusThread().start();
new PlusThread().start();
new PlusThread().start();
new PlusThread().start();
return count;
}
public class PlusThread extends Thread {
private int amount = 0;
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始售票");
countDownLatch.countDown();
if (countDownLatch.getCount()==0){
System.out.println("----------结果------------");
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
while (count > 0) {
lock.lock();
try {
if (count > 0) {
amount++;
count--;
}
}finally{
lock.unlock();
}
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "售出"+ (amount) + "张票");
}
}
}
RedisConfig
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(lettuceConnectionFactory);
return template;
}
}
RedisLock
@Component
public class RedisLock {
private static final String KEY = "LOCK_KEY";
@Resource(name = "redisTemplate")
private RedisTemplate<String, Object> redisTemplate;
private static ThreadLocal<String> local = new ThreadLocal<>();
public void lock() {
if(tryLock()){
return;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock();
}
public boolean tryLock() {
String uuid = UUID.randomUUID().toString();
RedisCallback<Boolean> callback = (connection) -> connection.set(KEY.getBytes(StandardCharsets.UTF_8), uuid.getBytes(StandardCharsets.UTF_8), Expiration.milliseconds(1000L), RedisStringCommands.SetOption.SET_IF_ABSENT);
boolean re = redisTemplate.execute(callback);
if(re){
local.set(uuid);
return true;
}
return false;
}
public boolean unlock() {
String script = FileUtils.getScript("unlock.lua");
RedisCallback<Boolean> callback = (connection) -> connection.eval(script.getBytes(), ReturnType.BOOLEAN ,1, KEY.getBytes(StandardCharsets.UTF_8), local.get().getBytes(StandardCharsets.UTF_8));
return redisTemplate.execute(callback);
}
}
FileUtils
@Component
public class FileUtils {
public static String getScript(String fileName){
String path = FileUtils.class.getClassLoader().getResource(fileName).getPath();
return readFileByLines(path);
}
public static String readFileByLines(String fileName) {
FileInputStream file = null;
BufferedReader reader = null;
InputStreamReader inputFileReader = null;
String content = "";
String tempString = null;
try {
file = new FileInputStream(fileName);
inputFileReader = new InputStreamReader(file, "utf-8");
reader = new BufferedReader(inputFileReader);
while ((tempString = reader.readLine()) != null) {
content += tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return content;
}
}
unlock.lua
if redis.call("get",KEYS[1]) == ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
运行结果:
那么如果我们在这个抢票系统中,不使用锁,会发生怎样的事情呢?我们将锁相关的代码注释掉,运行看看结果:可以发现,二十张票却出售了23张票
|