Commit 8eea4adc by xmh

更改 RLocalCacheMap 使用方式

排除图片访问时的鉴权
1 parent 91096350
...@@ -32,7 +32,7 @@ public class WebConfig implements WebMvcConfigurer { ...@@ -32,7 +32,7 @@ public class WebConfig implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/users/login", "/reid/upload", "/**/websocket/**", "/users/logout"); registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns("/users/login", "/reid/upload", "/**/websocket/**", "/users/logout","pics/image/**");
} }
@Bean @Bean
......
...@@ -34,6 +34,8 @@ import java.util.stream.Collectors; ...@@ -34,6 +34,8 @@ import java.util.stream.Collectors;
@Component @Component
@Slf4j @Slf4j
public class StorageUtils { public class StorageUtils {
private static RLocalCachedMap<Long, Storage> storageMap = null;
private static RLocalCachedMap<Long, Pack> packMap = null;
@Resource @Resource
private PackService packService; private PackService packService;
@Resource @Resource
...@@ -42,13 +44,11 @@ public class StorageUtils { ...@@ -42,13 +44,11 @@ public class StorageUtils {
private PicService picService; private PicService picService;
@Resource @Resource
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Resource @Resource
private RedissonService redissonService; private RedissonService redissonService;
@Resource @Resource
private RedissonClient redissonClient; private RedissonClient redissonClient;
public PicVo getPic(Long picId) { public PicVo getPic(Long picId) {
String redisKey = Constants.getRedisKeyPic(picId); String redisKey = Constants.getRedisKeyPic(picId);
RBucket<PicVo> picInfoBucket = redissonClient.getBucket(redisKey); RBucket<PicVo> picInfoBucket = redissonClient.getBucket(redisKey);
...@@ -74,21 +74,20 @@ public class StorageUtils { ...@@ -74,21 +74,20 @@ public class StorageUtils {
// 获取并缓存 image // 获取并缓存 image
Long packId = picVo.getPackId(); Long packId = picVo.getPackId();
String picImageKey = Constants.getRedisKeyPicImage(picId); // String picImageKey = Constants.getRedisKeyPicImage(picId);
RBucket<byte[]> picImageBucket = redissonClient.getBucket(picImageKey); // RBucket<byte[]> picImageBucket = redissonClient.getBucket(picImageKey);
byte[] bytes = picImageBucket.get(); // byte[] bytes = picImageBucket.get();
if (bytes == null) { // if (bytes == null) {
try { try {
bytes = getFile(packId, picVo.getName()); byte[] bytes = getFile(packId, picVo.getName());
picVo.setImage(bytes);
} catch (Exception e) { } catch (Exception e) {
log.info("", e); log.info("", e);
return null; return null;
} }
picImageBucket.set(bytes); // picImageBucket.set(bytes);
} // }
picImageBucket.expire(4, TimeUnit.HOURS); // picImageBucket.expire(4, TimeUnit.HOURS);
picVo.setImage(bytes);
return picVo; return picVo;
} }
...@@ -159,7 +158,9 @@ public class StorageUtils { ...@@ -159,7 +158,9 @@ public class StorageUtils {
public Storage getStorage(Long storageId) { public Storage getStorage(Long storageId) {
RLocalCachedMap<Long, Storage> storageMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_STORAGE_MAP, LocalCachedMapOptions.defaults()); if (storageMap == null) {
storageMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_STORAGE_MAP, LocalCachedMapOptions.defaults());
}
if (!storageMap.isExists()) { if (!storageMap.isExists()) {
refreshStorage(); refreshStorage();
} }
...@@ -176,7 +177,9 @@ public class StorageUtils { ...@@ -176,7 +177,9 @@ public class StorageUtils {
} }
public Pack getPack(Long packId) { public Pack getPack(Long packId) {
RLocalCachedMap<Long, Pack> packMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_PACK_MAP, LocalCachedMapOptions.defaults()); if (packMap == null) {
packMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_PACK_MAP, LocalCachedMapOptions.defaults());
}
if (!packMap.isExists()) { if (!packMap.isExists()) {
refreshPack(); refreshPack();
} }
...@@ -193,7 +196,6 @@ public class StorageUtils { ...@@ -193,7 +196,6 @@ public class StorageUtils {
public synchronized void refreshStorage() { public synchronized void refreshStorage() {
redissonService.lockAndRun("lock:" + Constants.REDIS_KEY_STORAGE_MAP, 10L, 8L, () -> { redissonService.lockAndRun("lock:" + Constants.REDIS_KEY_STORAGE_MAP, 10L, 8L, () -> {
RLocalCachedMap<Long, Storage> storageMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_STORAGE_MAP, LocalCachedMapOptions.defaults());
List<Storage> storages = storageService.selectByExample(new StorageExample()); List<Storage> storages = storageService.selectByExample(new StorageExample());
Map<Long, Storage> map = storages.stream().collect(Collectors.toMap(Storage::getId, x -> x, (a, b) -> a)); Map<Long, Storage> map = storages.stream().collect(Collectors.toMap(Storage::getId, x -> x, (a, b) -> a));
storageMap.putAll(map); storageMap.putAll(map);
...@@ -202,7 +204,6 @@ public class StorageUtils { ...@@ -202,7 +204,6 @@ public class StorageUtils {
public synchronized void refreshPack() { public synchronized void refreshPack() {
redissonService.lockAndRun("lock:" + Constants.REDIS_KEY_PACK_MAP, 10L, 8L, () -> { redissonService.lockAndRun("lock:" + Constants.REDIS_KEY_PACK_MAP, 10L, 8L, () -> {
RLocalCachedMap<Long, Pack> packMap = redissonClient.getLocalCachedMap(Constants.REDIS_KEY_PACK_MAP, LocalCachedMapOptions.defaults());
List<Pack> packs = packService.selectByExample(new PackExample()); List<Pack> packs = packService.selectByExample(new PackExample());
Map<Long, Pack> map = packs.stream().collect(Collectors.toMap(Pack::getId, x -> x, (a, b) -> a)); Map<Long, Pack> map = packs.stream().collect(Collectors.toMap(Pack::getId, x -> x, (a, b) -> a));
packMap.putAll(map); packMap.putAll(map);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!