Redisが高くて、キャッシュが貫通しています.
2962 ワード
次のコードは、1 W個人が同時にアクセスする場合、redisからallUserを取る場合、userListが空の場合、1 W個人はif判断文に入り、データベースを照会し、データベースの圧力に耐えられない
package com.tb.service;
import com.tb.dao.TUserMapper;
import com.tb.pojo.TUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class TuserServicelmpl implements TuserService {
@Resource
private TUserMapper tUserMapper;
@Resource
private RedisTemplate redisTemplate;
@Override
public List userlist() {
// , :
//
List userList = ( List) redisTemplate.opsForValue().get("allUser");
if (userList==null){
//
userList = tUserMapper.userlist();
redisTemplate.opsForValue().set("allUser",userList);
}
return userList;
}
}
ソリューション
Synchronizedキーワード
デュアル検出ロックの解決
package com.tb.service;
import com.tb.dao.TUserMapper;
import com.tb.pojo.TUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class TuserServicelmpl implements TuserService {
@Resource
private TUserMapper tUserMapper;
@Resource
private RedisTemplate redisTemplate;
@Override
public List userlist() {
// , :
//
List userList = ( List) redisTemplate.opsForValue().get("allUser");
//
if (userList==null){
synchronized(this){
//
userList = ( List) redisTemplate.opsForValue().get("allUser");
if (userList==null){
// ,
userList = tUserMapper.userlist();
redisTemplate.opsForValue().set("allUser",userList);
}
}
}
return userList;
}
}
テストコード @GetMapping("/Tuser")
public List querlist(){
//
Runnable runnable = new Runnable() {
@Override
public void run() {
tuserService.userlist();
}
};
//
ExecutorService executorService = Executors.newFixedThreadPool(25);
for(int i = 0 ; i<10000;i++){
executorService.submit(runnable);
}
return tuserService.userlist();
}
package com.tb.service;
import com.tb.dao.TUserMapper;
import com.tb.pojo.TUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class TuserServicelmpl implements TuserService {
@Resource
private TUserMapper tUserMapper;
@Resource
private RedisTemplate redisTemplate;
@Override
public List userlist() {
// , :
//
List userList = ( List) redisTemplate.opsForValue().get("allUser");
//
if (userList==null){
synchronized(this){
//
userList = ( List) redisTemplate.opsForValue().get("allUser");
if (userList==null){
// ,
userList = tUserMapper.userlist();
redisTemplate.opsForValue().set("allUser",userList);
}
}
}
return userList;
}
}
@GetMapping("/Tuser")
public List querlist(){
//
Runnable runnable = new Runnable() {
@Override
public void run() {
tuserService.userlist();
}
};
//
ExecutorService executorService = Executors.newFixedThreadPool(25);
for(int i = 0 ; i<10000;i++){
executorService.submit(runnable);
}
return tuserService.userlist();
}