mirror of
https://github.com/directus/directus.git
synced 2026-01-24 13:28:15 -05:00
Clear middleware/service (temp)
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
import { BaseException } from './base';
|
||||
|
||||
export class InvalidCacheKeyException extends BaseException {
|
||||
constructor(message: string) {
|
||||
super(message, 400, 'INVALID_CACHE_KEY');
|
||||
}
|
||||
}
|
||||
@@ -1,51 +1,10 @@
|
||||
/**
|
||||
* Caching using redis
|
||||
* and node caching
|
||||
*/
|
||||
import { RequestHandler } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import CacheService from '../services/cache';
|
||||
import { RedisNotFoundException } from '../exceptions';
|
||||
import env from '../env';
|
||||
|
||||
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
|
||||
// make the key of the cache the URL
|
||||
// need to check that this will work for all endpoints
|
||||
// node cache service
|
||||
// have used query as then can decide whather to use cache or not from api call
|
||||
if (env.CACHE_ENABLED !== 'true') return next();
|
||||
|
||||
//key needs to have url, query and permissions
|
||||
|
||||
const key = `${req.url}${req.query}${req.permissions}`;
|
||||
|
||||
// we have two options here. Redis or node cache
|
||||
if (env.CACHE_DRIVER === 'redis') {
|
||||
const redis = require('redis');
|
||||
const redisClient = redis.createClient({
|
||||
enable_offline_queue: false,
|
||||
host: env.CACHE_HOST,
|
||||
port: env.CACHE_PORT,
|
||||
password: env.CACHE_REDIS_PASSWORD,
|
||||
});
|
||||
if (!redisClient) {
|
||||
throw new RedisNotFoundException('Redis client does not exist');
|
||||
}
|
||||
|
||||
redisClient.get(key, (error: string, resultData: string) => {
|
||||
if (error) {
|
||||
throw new RedisNotFoundException('Error retreiving redis cache');
|
||||
}
|
||||
|
||||
if (resultData) {
|
||||
const reponse = JSON.parse(resultData);
|
||||
res.json(reponse);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const cacheService = new CacheService();
|
||||
res.json(cacheService.getCache(key));
|
||||
}
|
||||
if (env.CACHE_ENABLED !== true) return next();
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
@@ -1,55 +1,5 @@
|
||||
/** cache service.
|
||||
* Makes dealing with cache management easier
|
||||
* in case a new cache is used in future
|
||||
* sdtTTL is the amount of time a cache should be
|
||||
* stored, in seconds, before being deleted. Keep
|
||||
* checkPeriod is the amount of seconds for the
|
||||
* autodelete to wait before checking
|
||||
* if cache needs to be deleted.
|
||||
* Have wrapped node cache so we can extend if needed
|
||||
* could put redis cache in here too
|
||||
*/
|
||||
import NodeCache from 'node-cache';
|
||||
import redis from 'redis';
|
||||
import { InvalidCacheKeyException } from '../exceptions';
|
||||
import env from '../env';
|
||||
|
||||
export default class CacheService {
|
||||
apiCache: NodeCache;
|
||||
|
||||
constructor() {
|
||||
// options found at https://github.com/node-cache/node-cache
|
||||
this.apiCache = new NodeCache({
|
||||
stdTTL: env.CACHE_TTL,
|
||||
checkperiod: env.CACHE_CHECK_LIVE,
|
||||
useClones: false,
|
||||
});
|
||||
}
|
||||
// delete the cache with the given key. Casted to string by node-cache
|
||||
// so might as well do it here too for consitancy
|
||||
|
||||
async delCache(keys: string) {
|
||||
if (!keys) {
|
||||
throw new InvalidCacheKeyException('Keys was not provided for cache');
|
||||
}
|
||||
this.apiCache.del(keys);
|
||||
}
|
||||
// attempt to get the cache based on the key, if it is empty then set it
|
||||
|
||||
async getCache(key: string) {
|
||||
const value = this.apiCache.get(key);
|
||||
|
||||
if (value) {
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
}
|
||||
|
||||
async setCache(key: string, data: {}) {
|
||||
this.apiCache.set(key, data);
|
||||
}
|
||||
|
||||
// this flushes all data. important incase cache gets too full.
|
||||
async flushCache() {
|
||||
this.apiCache.flushAll();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user