mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Making sure delete is present
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
42
api/src/middleware/delete-cache.ts
Normal file
42
api/src/middleware/delete-cache.ts
Normal 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;
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user