Add the option to store cache on disk

This commit is contained in:
Maksim Karasev 2021-09-12 15:10:49 +03:00
parent e83c461c75
commit 12b8e789b1
7 changed files with 65 additions and 10 deletions

View File

@ -72,6 +72,9 @@ To use Redis instead of built-it memory cache:
`REDIS_PASSWORD` - Redis password
Or to store cache on disk :
`CACHE_DIR` - Cache directory
### On bare machine

View File

@ -1,6 +1,7 @@
#PORT=3002 # (optional) port to listen on, by default set to 3001
#IMAGE_DIR=/tmp/images/ # image directory
#DB_DIR=/tmp/db/ # database directory
#CACHE_DIR=/tmp/cache # (optional) cache directory
#REDIS_HOST=localhost # (optional) redis host
#REDIS_PASSWORD=test # (optional) redis password
#PROXY='127.0.0.1/8' # (optional) express formatted reverse proxy settings

View File

@ -17,6 +17,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/better-sqlite3": "^5.4.1",
"@types/cache-manager": "^3.4.0",
"@types/cache-manager-fs-hash": "^0.0.1",
"@types/cache-manager-ioredis": "^2.0.1",
"@types/command-exists": "^1.2.0",
"@types/cookie-parser": "^1.4.2",
@ -50,6 +51,7 @@
"bcryptjs": "^2.4.3",
"better-sqlite3": "^7.1.2",
"cache-manager": "^3.4.0",
"cache-manager-fs-hash": "^1.0.0",
"cache-manager-ioredis": "^2.1.0",
"command-exists": "^1.2.9",
"cookie-parser": "^1.4.5",

View File

@ -2,7 +2,7 @@ import { Router } from 'express';
import fileType from 'file-type';
import logger from '../utils/logger';
import { imageExists } from '../services/imageService';
import { thumbnailCache } from '../utils/cache';
import thumbnailCache from '../utils/cache';
import { getThumbnail } from '../services/thumbnailService';
import { FULLS_AS_THUMBS } from '../utils/config';

View File

@ -1,16 +1,16 @@
import cacheManager from 'cache-manager';
import redisStore from 'cache-manager-ioredis';
import { REDIS_HOST, REDIS_PORT, REDIS_PASSWORD } from './config';
import fsStore from 'cache-manager-fs-hash';
import {
REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, CACHE_DIR,
} from './config';
import logger from './logger';
// eslint-disable-next-line import/prefer-default-export
export const thumbnailCache = typeof REDIS_HOST === 'undefined'
? cacheManager.caching({
store: 'memory',
max: 300,
ttl: 604800,
})
: cacheManager.caching({
// eslint-disable-next-line import/no-mutable-exports
let thumbnailCache: cacheManager.Cache;
if (typeof REDIS_HOST !== 'undefined') {
thumbnailCache = cacheManager.caching({
store: redisStore,
host: REDIS_HOST,
port: REDIS_PORT,
@ -18,5 +18,24 @@ export const thumbnailCache = typeof REDIS_HOST === 'undefined'
db: 0,
ttl: 600,
});
} else if (typeof CACHE_DIR !== 'undefined') {
thumbnailCache = cacheManager.caching({
store: fsStore,
options: {
path: CACHE_DIR, // path for cached files
ttl: 60 * 60 * 24 * 365, // time to life in seconds
subdirs: true, // create subdirectories to reduce the files in a single dir (default: false)
zip: true, // zip files to save diskspace (default: false)
},
});
} else {
thumbnailCache = cacheManager.caching({
store: 'memory',
max: 300,
ttl: 604800,
});
}
logger.info(`Using cache store '${(thumbnailCache.store as any).name}'`);
export default thumbnailCache;

View File

@ -14,6 +14,7 @@ export const BASE_URL: string = process.env.BASE_URL || '';
export const { REDIS_HOST } = process.env;
export const REDIS_PORT = parseInt(process.env.REDIS_PORT || '6379', 10);
export const { REDIS_PASSWORD } = process.env;
export const { CACHE_DIR } = process.env;
export const PROXY = process.env.PROXY || true;
export const { USERNAME } = process.env;
export const { PASSWORD } = process.env;

View File

@ -2547,6 +2547,15 @@ __metadata:
languageName: node
linkType: hard
"@types/cache-manager-fs-hash@npm:^0.0.1":
version: 0.0.1
resolution: "@types/cache-manager-fs-hash@npm:0.0.1"
dependencies:
"@types/cache-manager": "*"
checksum: b99fa73a388a7e8812521a84c8752bffcc488ed80a166052000f2bfa6b1c4c27d6b676fd8280f498925951d59af84c402f5635289cd425e7ce094d597aea7008
languageName: node
linkType: hard
"@types/cache-manager-ioredis@npm:^2.0.1":
version: 2.0.2
resolution: "@types/cache-manager-ioredis@npm:2.0.2"
@ -4788,6 +4797,15 @@ __metadata:
languageName: node
linkType: hard
"cache-manager-fs-hash@npm:^1.0.0":
version: 1.0.0
resolution: "cache-manager-fs-hash@npm:1.0.0"
dependencies:
lockfile: ^1.0.4
checksum: d7a1249e82e4e2af4c84a37578fa023840b1a281cd5da93d635776ffde759aefca82157156274e4e48b08ced0a6f7e4f972c285b65702002e09139b124d7be2f
languageName: node
linkType: hard
"cache-manager-ioredis@npm:^2.1.0":
version: 2.1.0
resolution: "cache-manager-ioredis@npm:2.1.0"
@ -10531,6 +10549,15 @@ fsevents@^1.2.7:
languageName: node
linkType: hard
"lockfile@npm:^1.0.4":
version: 1.0.4
resolution: "lockfile@npm:1.0.4"
dependencies:
signal-exit: ^3.0.2
checksum: 8de35aace8acbe883cbca3cc3959e88904d57c79dccd4afffc64aea8f9cf7b4c63598d08b8add66fbf381f8fb3ce4fd4c518cd231c797c266b6c790eb7b33abc
languageName: node
linkType: hard
"lodash._reinterpolate@npm:^3.0.0":
version: 3.0.0
resolution: "lodash._reinterpolate@npm:3.0.0"
@ -14620,6 +14647,7 @@ resolve@^2.0.0-next.3:
"@types/bcryptjs": ^2.4.2
"@types/better-sqlite3": ^5.4.1
"@types/cache-manager": ^3.4.0
"@types/cache-manager-fs-hash": ^0.0.1
"@types/cache-manager-ioredis": ^2.0.1
"@types/command-exists": ^1.2.0
"@types/cookie-parser": ^1.4.2
@ -14640,6 +14668,7 @@ resolve@^2.0.0-next.3:
bcryptjs: ^2.4.3
better-sqlite3: ^7.1.2
cache-manager: ^3.4.0
cache-manager-fs-hash: ^1.0.0
cache-manager-ioredis: ^2.1.0
command-exists: ^1.2.9
cookie-parser: ^1.4.5