Skip to content

Commit

Permalink
New: Max cache size and max cache old
Browse files Browse the repository at this point in the history
  • Loading branch information
ollm committed Oct 9, 2023
1 parent c3ee1e2 commit fe679d4
Show file tree
Hide file tree
Showing 22 changed files with 227 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Search and filter in library/browsing [`8393903`](https://github.com/ollm/OpenComic/commit/8393903117981bea2b8a79e2e50b77d02334aa05)
- Tracking at the end of the chapter/volume setting option [`39f1954`](https://github.com/ollm/OpenComic/commit/39f19546b2fd363d321f9c423e706c3dc773aa4f)
- View size and remove cache and temporary files from settings [`8114336`](https://github.com/ollm/OpenComic/commit/8114336ef2c70748e3b1d87618903ef34cb58f0a)
- Max cache size and max cache old

##### 🐛 Bug Fixes

Expand Down
5 changes: 5 additions & 0 deletions languages/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Idioma",
"settings": "Configuració",
"theme": "Tema",
"days": "Dies",
"valueCannotBeEmpty": "El valor no pot estar buit",
"nightMode": "Mode nit"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "Emmagatzematge",
"cache": "Memòria cau",
"cacheDescription": "Les miniatures i l'índex dels fitxers comprimits s'emmagatzemen aquí, no es recomana netejar-la tret que sigui molt gran o hi hagi algun error relacionat amb ella",
"cacheMaxSize": "Tamany màxim de la memòria cau",
"cacheMaxSizeDescription": "S'eliminaran fitxers de la memòria cau quan superi aquesta mida, prioritzant els fitxers més antics sense ús",
"cacheMaxOld": "Temps màxim de la memòria cau",
"cacheMaxOldDescription": "S'eliminaran fitxers de la memòria cau quan portin més d'aquest temps sense fer-se servir",
"clearCache": "Netejar memòria cau",
"temporaryFiles": "Arxius temporals",
"temporaryFilesDescription": "Aquí es descomprimeixen els fitxers comprimits a mesura que es van necessitant, això s'elimina automàticament en tancar OpenComic",
Expand Down
5 changes: 5 additions & 0 deletions languages/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Jazyk",
"settings": "",
"theme": "Vzhled",
"days": "",
"valueCannotBeEmpty": "Hodnota nesmí být prázdná",
"nightMode": "Noční režim"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Sprache",
"settings": "",
"theme": "Thema",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "Nachtmodus"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
7 changes: 6 additions & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Language",
"settings": "Settings",
"theme": "Theme",
"days": "Days",
"valueCannotBeEmpty": "Value cannot be empty",
"nightMode": "Night mode"
},
Expand Down Expand Up @@ -180,9 +181,13 @@
"main": "Storage",
"cache": "Cache",
"cacheDescription": "The thumbnails and index of the compressed files are stored here, it is not recommended to clean it unless it is very large or there is an error related to it",
"cacheMaxSize": "Maximum cache size",
"cacheMaxSizeDescription": "Files will be removed from the cache when it exceeds this size, prioritizing older unused files",
"cacheMaxOld": "Maximum cache time",
"cacheMaxOldDescription": "Files will be removed from the cache when they have not been used for more than this time",
"clearCache": "Clear cache",
"temporaryFiles": "Temporary files",
"temporaryFilesDescription": "Here the compressed files are decompressed as they are needed, this is automatically deleted when closing OpenComic",
"temporaryFilesDescription": "Here the compressed files are decompressed as they are needed, this is automatically removed when closing OpenComic",
"removeTemporaryFiles": "Remove temporary files"
}
},
Expand Down
5 changes: 5 additions & 0 deletions languages/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Idioma",
"settings": "Ajustes",
"theme": "Tema",
"days": "Días",
"valueCannotBeEmpty": "El valor no puede estar vació",
"nightMode": "Modo noche"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "Almacenamiento",
"cache": "Caché",
"cacheDescription": "Las miniaturas y el índice de los archivos comprimidos se almacenan aquí, no se recomienda limpiarla a no ser que sea muy grande o haya algún error relacionado con ella",
"cacheMaxSize": "Tamaño máximo de la caché",
"cacheMaxSizeDescription": "Se eliminarán archivos de la caché cuando supere este tamaño, priorizando los archivos más antiguos sin uso",
"cacheMaxOld": "Tiempo máximo de la caché",
"cacheMaxOldDescription": "Se eliminarán archivos de la caché cuando lleven más de este tiempo sin usarse",
"clearCache": "Limpiar caché",
"temporaryFiles": "Archivos temporales",
"temporaryFilesDescription": "Aquí se descomprimen los archivos comprimidos a medida que se van necesitando, esto se elimina automáticamente al cerrar OpenComic",
Expand Down
5 changes: 5 additions & 0 deletions languages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Langues",
"settings": "",
"theme": "Theme",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "Mode nuit"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Nyelv",
"settings": "",
"theme": "Téma",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "Sötét téma"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Lingua",
"settings": "",
"theme": "Tema",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "Modalità notte"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "言語",
"settings": "",
"theme": "テーマ",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "ナイトモード"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Linguagem",
"settings": "",
"theme": "Tema",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "Modo Noturno"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Язык",
"settings": "",
"theme": "Тема",
"days": "",
"valueCannotBeEmpty": "Значение не может быть пустым",
"nightMode": "Ночной режим"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/vi.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "Ngôn ngữ",
"settings": "",
"theme": "Chủ đề",
"days": "",
"valueCannotBeEmpty": "Giá trị không được để trống",
"nightMode": "Chế độ tối"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/zh-hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "语言",
"settings": "",
"theme": "主题",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "夜间模式"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
5 changes: 5 additions & 0 deletions languages/zh-hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"language": "語言",
"settings": "",
"theme": "圖形介面",
"days": "",
"valueCannotBeEmpty": "",
"nightMode": "夜間模式"
},
Expand Down Expand Up @@ -180,6 +181,10 @@
"main": "",
"cache": "",
"cacheDescription": "",
"cacheMaxSize": "",
"cacheMaxSizeDescription": "",
"cacheMaxOld": "",
"cacheMaxOldDescription": "",
"clearCache": "",
"temporaryFiles": "",
"temporaryFilesDescription": "",
Expand Down
78 changes: 78 additions & 0 deletions scripts/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,83 @@ async function deleteInCache(path)
return;
}

function deleteInCacheSha(sha, returnFileSize = false)
{
let cachePath = p.join(cacheFolder, sha+'.jpg');

if(data[sha])
delete data[sha];

let fileSize = 0;

if(fs.existsSync(cachePath))
{
if(returnFileSize)
fileSize = fs.statSync(cachePath).size;

fs.unlinkSync(cachePath);
}

return fileSize;
}

function purge()
{
let _time = time();

let cacheMaxSize = config.cacheMaxSize * 1000 * 1000;
let cacheMaxOld = config.cacheMaxOld * 60 * 60 * 24;

let dataArray = [];

// Remove not usage files
for(let sha in data)
{
if(_time - data[sha].lastAccess > cacheMaxOld)
{
deleteInCacheSha(sha);
}
else
{
dataArray.push({
sha: sha,
lastAccess: data[sha].lastAccess,
});
}
}

// Remove if exede cache max size
let cacheSize = fileManager.dirSizeSync(cache.folder);

if(cacheSize > cacheMaxSize)
{
let cacheMaxSizeMargin = cacheMaxSize * 0.8; // Remove 20% if cache exceeds maximum size to avoid running this every time

dataArray.sort(function(a, b) {

if(a.lastAccess === b.lastAccess)
return 0;

return a.lastAccess > b.lastAccess ? 1 : -1;

});

for(let i = 0, len = dataArray.length; i < len; i++)
{
let size = deleteInCacheSha(dataArray[i].sha, true);

cacheSize -= size;

if(cacheSize < cacheMaxSizeMargin)
break;
}
}

storage.set('cache', data);

return;
}

module.exports = {
folder: cacheFolder,
returnThumbnailsImages: returnThumbnailsImages,
Expand All @@ -224,4 +301,5 @@ module.exports = {
processingTheImageQueue: function(){return processingTheImageQueue},
stopQueue: stopQueue,
resumeQueue: resumeQueue,
purge: purge,
};
20 changes: 20 additions & 0 deletions scripts/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,25 @@ async function dirSize(dir)
return size;
}

function dirSizeSync(dir)
{
let files = fs.readdirSync(dir, {withFileTypes: true});
let size = 0;

for(let i = 0, len = files.length; i < len; i++)
{
let file = files[i];
let path = p.join(dir, file.name);

if(file.isDirectory())
size += dirSizeSync(path);
else if(file.isFile())
size += fs.statSync(path).size;
}

return size;
}

var prevDevicePixelRatio = window.devicePixelRatio;

window.addEventListener('resize', function() {
Expand Down Expand Up @@ -1987,4 +2006,5 @@ module.exports = {
lastCompressedFile: lastCompressedFile,
containsCompressed: containsCompressed,
dirSize: dirSize,
dirSizeSync: dirSizeSync,
}

0 comments on commit fe679d4

Please sign in to comment.