什么是缓存击穿:给某一个key设置了过期时间,当key过期的时候,恰好这时间点对这个key有大量的并发请求过来,这些并发的请求可能会瞬间把数据库压垮。
互斥锁代码:
1 private final ReadWriteLock readWriteLock=new ReentrantReadWriteLock(); 2 private final Lock writeLock=readWriteLock.writeLock(); 3 4 @GetMapping("/hi/{name}") 5 public String hi(@PathVariable("name") String name) 6 { 7 System.out.println("name="+name); 8 9 if(redisUtil.get(name)!=null) 10 { 11 return redisUtil.get(name).toString(); 12 } 13 else { 14 writeLock.lock(); 15 try{ 16 Thread.sleep(20*1000); 17 redisUtil.set(name,"My name is "+name+"!",60); 18 return redisUtil.get(name).toString(); 19 } 20 catch (Exception e) 21 { 22 return null; 23 } 24 finally { 25 writeLock.unlock(); 26 } 27 } 28 }
标签:writeLock,面试题,缓存,name,Redis,key,redisUtil From: https://www.cnblogs.com/wuzexin/p/17592343.html