!243 解决定时清除缓存时,对于多媒体类型文件,只删除了磁盘缓存文件,导致再次预览时从缓存中获取到了一个不存在文件路径

Merge pull request !243 from zhh/master
pull/251/head
kailing 2023-11-08 01:58:38 +00:00 committed by Gitee
commit 69444a4d72
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 26 additions and 13 deletions

View File

@ -101,6 +101,7 @@ public class CacheServiceJDKImpl implements CacheService {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY); initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY); initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY); initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
initMediaConvertCachePool(CacheService.DEFAULT_MEDIACONVERT_CAPACITY);
} }
@Override @Override

View File

@ -107,6 +107,7 @@ public class CacheServiceRedisImpl implements CacheService {
cleanPdfCache(); cleanPdfCache();
cleanImgCache(); cleanImgCache();
cleanPdfImgCache(); cleanPdfImgCache();
cleanMediaConvertCache();
} }
@Override @Override
@ -135,4 +136,9 @@ public class CacheServiceRedisImpl implements CacheService {
RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY); RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
pdfImg.clear(); pdfImg.clear();
} }
private void cleanMediaConvertCache() {
RMapCache<String, Integer> mediaConvertCache = redissonClient.getMapCache(FILE_PREVIEW_MEDIA_CONVERT_KEY);
mediaConvertCache.clear();
}
} }

View File

@ -104,7 +104,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, String> getPDFCache() { public Map<String, String> getPDFCache() {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
try{ try {
result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes())); result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
@ -116,7 +116,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String getPDFCache(String key) { public String getPDFCache(String key) {
String result = ""; String result = "";
try{ try {
Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes())); Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
@ -129,7 +129,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, List<String>> getImgCache() { public Map<String, List<String>> getImgCache() {
Map<String, List<String>> result = new HashMap<>(); Map<String, List<String>> result = new HashMap<>();
try{ try {
result = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes())); result = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
@ -142,7 +142,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public List<String> getImgCache(String key) { public List<String> getImgCache(String key) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
Map<String, List<String>> map; Map<String, List<String>> map;
try{ try {
map = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes())); map = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
@ -156,7 +156,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
public Integer getPdfImageCache(String key) { public Integer getPdfImageCache(String key) {
Integer result = 0; Integer result = 0;
Map<String, Integer> map; Map<String, Integer> map;
try{ try {
map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes())); map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
@ -180,7 +180,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, String> getMediaConvertCache() { public Map<String, String> getMediaConvertCache() {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
try{ try {
result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_MEDIA_CONVERT_KEY.getBytes())); result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_MEDIA_CONVERT_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
@ -203,7 +203,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String getMediaConvertCache(String key) { public String getMediaConvertCache(String key) {
String result = ""; String result = "";
try{ try {
Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_MEDIA_CONVERT_KEY.getBytes())); Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_MEDIA_CONVERT_KEY.getBytes()));
result = map.get(key); result = map.get(key);
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
@ -218,6 +218,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
cleanPdfCache(); cleanPdfCache();
cleanImgCache(); cleanImgCache();
cleanPdfImgCache(); cleanPdfImgCache();
cleanMediaConvertCache();
} catch (IOException | RocksDBException e) { } catch (IOException | RocksDBException e) {
LOGGER.error("Clean Cache Exception" + e); LOGGER.error("Clean Cache Exception" + e);
} }
@ -236,7 +237,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Integer> getPdfImageCaches() { private Map<String, Integer> getPdfImageCaches() {
Map<String, Integer> map = new HashMap<>(); Map<String, Integer> map = new HashMap<>();
try{ try {
map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes())); map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
} catch (RocksDBException | IOException | ClassNotFoundException e) { } catch (RocksDBException | IOException | ClassNotFoundException e) {
LOGGER.error("Get from RocksDB Exception" + e); LOGGER.error("Get from RocksDB Exception" + e);
@ -245,22 +246,22 @@ public class CacheServiceRocksDBImpl implements CacheService {
} }
private byte[] toByteArray (Object obj) throws IOException { private byte[] toByteArray(Object obj) throws IOException {
byte[] bytes; byte[] bytes;
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj); oos.writeObject(obj);
oos.flush(); oos.flush();
bytes = bos.toByteArray (); bytes = bos.toByteArray();
oos.close(); oos.close();
bos.close(); bos.close();
return bytes; return bytes;
} }
private Object toObject (byte[] bytes) throws IOException, ClassNotFoundException { private Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
Object obj; Object obj;
ByteArrayInputStream bis = new ByteArrayInputStream (bytes); ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream (bis); ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject(); obj = ois.readObject();
ois.close(); ois.close();
bis.close(); bis.close();
@ -281,4 +282,9 @@ public class CacheServiceRocksDBImpl implements CacheService {
Map<String, Integer> initPDFIMGCache = new HashMap<>(); Map<String, Integer> initPDFIMGCache = new HashMap<>();
db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache)); db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
} }
private void cleanMediaConvertCache() throws IOException, RocksDBException {
Map<String, String> initMediaConvertCache = new HashMap<>();
db.put(FILE_PREVIEW_MEDIA_CONVERT_KEY.getBytes(), toByteArray(initMediaConvertCache));
}
} }