@Test void updateUsername() { IntStream.range(0,1000).parallel() .forEach( i -> { userService.updateUsername("123","xiaoming"+i); }); }
@Override public UserInfo updateUsername(String id, String username) { boolean res = false; UserInfo info = null; RLock lock = redission.getLock("lock_"+id); try { lock.lock(10, TimeUnit.SECONDS); System.out.println("got lock"); }catch (Exception e){ log.warn("获取redis锁失败！ {}",id); } finally { try{ lock.unlock(); }finally { System.out.println("unlock"); } } return info; }
UserServiceImpl implements UserService { @Override public UserInfo updateUsername(String id, String username) { boolean res = false; UserInfo info = null; RLock lock = redission.getLock("lock_"+id); try { lock.lock(10, TimeUnit.SECONDS); System.out.println("got lock"); }catch (Exception e){ log.warn("获取redis锁失败！ {}",id); } finally { try{ lock.unlock(); }finally { System.out.println("unlock"); } } return info; } }
UserServiceImpl implements UserService { @Override public UserInfo updateUsername(String id, String username) { boolean res = false; UserInfo info = null; RLock lock = redission.getLock("lock_"+id); try { lock.lock(10, TimeUnit.SECONDS); System.out.println("got lock"); }catch (Exception e){ log.warn("获取redis锁失败！ {}",id); } finally { try{ lock.unlock(); }finally { System.out.println("unlock"); } } return info; } @Autowired  UserServiceImpl(RedisTemplate<String, UserInfo> redisTemplate); }
UserServiceImpl implements UserService { @Override public UserInfo updateUsername(String id, String username) { boolean res = false; UserInfo info = null; RLock lock = redission.getLock("lock_"+id); try { lock.lock(10, TimeUnit.SECONDS); System.out.println("got lock"); }catch (Exception e){ log.warn("获取redis锁失败！ {}",id); } finally { try{ lock.unlock(); }finally { System.out.println("unlock"); } } return info; } @Autowired  UserServiceImpl(RedisTemplate<String, UserInfo> redisTemplate); @Override @Cacheable("getUserInfo") UserInfo getUserInfo(String key); @Override @Cacheable("userinfo") UserInfo addUser(String username, String password); @Override UserInfo updateUsername(String id, String username); }
UserServiceImpl implements UserService { @Override public UserInfo updateUsername(String id, String username) { boolean res = false; UserInfo info = null; RLock lock = redission.getLock("lock_"+id); try { lock.lock(10, TimeUnit.SECONDS); System.out.println("got lock"); }catch (Exception e){ log.warn("获取redis锁失败！ {}",id); } finally { try{ lock.unlock(); }finally { System.out.println("unlock"); } } return info; } @Autowired  UserServiceImpl(RedisTemplate<String, UserInfo> redisTemplate); @Override @Cacheable("getUserInfo") UserInfo getUserInfo(String key); @Override @Cacheable("userinfo") UserInfo addUser(String username, String password); @Override UserInfo updateUsername(String id, String username);  }