Commit 8eea4adc by xmh

更改 RLocalCacheMap 使用方式

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