Making sure delete is present

This commit is contained in:
kukulaka
2020-08-31 17:40:16 +01:00
parent 9d6f80149c
commit 8a21d6fd01
8 changed files with 78 additions and 17 deletions

View File

@@ -3,19 +3,11 @@
* and node caching
*/
import { RequestHandler } from 'express';
import redis from 'redis';
import asyncHandler from 'express-async-handler';
import CacheService from '../services/cache';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
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
@@ -29,6 +21,13 @@ const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next)
// 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');
}

View File

@@ -0,0 +1,42 @@
/**
* 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 delCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
// setting the cache
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.del(key);
} else {
const cacheService = new CacheService();
cacheService.delCache(key);
}
return next();
});
export default delCacheMiddleware;

View File

@@ -3,20 +3,11 @@
* and node caching
*/
import { RequestHandler } from 'express';
import redis from 'redis';
import NodeCache from 'node-cache';
import asyncHandler from 'express-async-handler';
import CacheService from '../services/cache';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
const setCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
// setting the cache
@@ -28,6 +19,13 @@ const setCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) =
// 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');
}