diff --git a/api/src/controllers/activity.ts b/api/src/controllers/activity.ts index 56f4cab663..7715b64d24 100644 --- a/api/src/controllers/activity.ts +++ b/api/src/controllers/activity.ts @@ -5,6 +5,7 @@ import sanitizeQuery from '../middleware/sanitize-query'; import useCollection from '../middleware/use-collection'; import checkCacheMiddleware from '../middleware/check-cache'; import setCacheMiddleware from '../middleware/set-cache'; +import delCacheMiddleware from '../middleware/delete-cache'; import ActivityService from '../services/activity'; import MetaService from '../services/meta'; import { Action } from '../types'; @@ -51,6 +52,7 @@ router.post( '/comment', useCollection('directus_activity'), sanitizeQuery, + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new ActivityService({ accountability: req.accountability }); @@ -74,6 +76,7 @@ router.patch( '/comment/:pk', useCollection('directus_activity'), sanitizeQuery, + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new ActivityService({ accountability: req.accountability }); const primaryKey = await service.update(req.body, req.params.pk); @@ -88,6 +91,7 @@ router.patch( router.delete( '/comment/:pk', useCollection('directus_activity'), + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new ActivityService({ accountability: req.accountability }); await service.delete(req.params.pk); diff --git a/api/src/controllers/collections.ts b/api/src/controllers/collections.ts index cfc92ee28c..764b50573f 100644 --- a/api/src/controllers/collections.ts +++ b/api/src/controllers/collections.ts @@ -4,6 +4,7 @@ import asyncHandler from 'express-async-handler'; import sanitizeQuery from '../middleware/sanitize-query'; import checkCacheMiddleware from '../middleware/check-cache'; import setCacheMiddleware from '../middleware/set-cache'; +import delCacheMiddleware from '../middleware/delete-cache'; import CollectionsService from '../services/collections'; import useCollection from '../middleware/use-collection'; import MetaService from '../services/meta'; @@ -13,6 +14,7 @@ const router = Router(); router.post( '/', useCollection('directus_collections'), + delCacheMiddleware, asyncHandler(async (req, res) => { const collectionsService = new CollectionsService({ accountability: req.accountability }); @@ -58,6 +60,7 @@ router.get( router.patch( '/:collection', useCollection('directus_collections'), + delCacheMiddleware, asyncHandler(async (req, res) => { const collectionsService = new CollectionsService({ accountability: req.accountability }); const collectionKey = req.params.collection.includes(',') @@ -72,6 +75,7 @@ router.patch( router.delete( '/:collection', useCollection('directus_collections'), + delCacheMiddleware, asyncHandler(async (req, res) => { const collectionsService = new CollectionsService({ accountability: req.accountability }); const collectionKey = req.params.collection.includes(',') diff --git a/api/src/controllers/fields.ts b/api/src/controllers/fields.ts index d233e2aba3..7d4c468fb7 100644 --- a/api/src/controllers/fields.ts +++ b/api/src/controllers/fields.ts @@ -5,6 +5,7 @@ import FieldsService from '../services/fields'; import validateCollection from '../middleware/collection-exists'; import checkCacheMiddleware from '../middleware/check-cache'; import setCacheMiddleware from '../middleware/set-cache'; +import delCacheMiddleware from '../middleware/delete-cache'; import { schemaInspector } from '../database'; import { FieldNotFoundException, InvalidPayloadException } from '../exceptions'; import Joi from 'joi'; @@ -85,6 +86,7 @@ router.post( '/:collection', validateCollection, useCollection('directus_fields'), + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new FieldsService({ accountability: req.accountability }); @@ -108,6 +110,7 @@ router.patch( '/:collection', validateCollection, useCollection('directus_fields'), + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new FieldsService({ accountability: req.accountability }); @@ -132,6 +135,7 @@ router.patch( '/:collection/:field', validateCollection, useCollection('directus_fields'), + delCacheMiddleware, // @todo: validate field asyncHandler(async (req, res) => { const service = new FieldsService({ accountability: req.accountability }); @@ -152,6 +156,7 @@ router.delete( '/:collection/:field', validateCollection, useCollection('directus_fields'), + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new FieldsService({ accountability: req.accountability }); diff --git a/api/src/controllers/items.ts b/api/src/controllers/items.ts index 25b16ac539..51b05e8c8d 100644 --- a/api/src/controllers/items.ts +++ b/api/src/controllers/items.ts @@ -4,6 +4,7 @@ import asyncHandler from 'express-async-handler'; import ItemsService from '../services/items'; import checkCacheMiddleware from '../middleware/check-cache'; import setCacheMiddleware from '../middleware/set-cache'; +import delCacheMiddleware from '../middleware/delete-cache'; import sanitizeQuery from '../middleware/sanitize-query'; import collectionExists from '../middleware/collection-exists'; import MetaService from '../services/meta'; @@ -15,6 +16,7 @@ router.post( '/:collection', collectionExists, sanitizeQuery, + delCacheMiddleware, asyncHandler(async (req, res) => { if (req.singleton) { throw new RouteNotFoundException(req.path); @@ -76,6 +78,7 @@ router.patch( '/:collection', collectionExists, sanitizeQuery, + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); @@ -96,6 +99,7 @@ router.patch( '/:collection/:pk', collectionExists, sanitizeQuery, + delCacheMiddleware, asyncHandler(async (req, res) => { if (req.singleton) { throw new RouteNotFoundException(req.path); @@ -114,6 +118,7 @@ router.patch( router.delete( '/:collection/:pk', collectionExists, + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new ItemsService(req.collection, { accountability: req.accountability }); const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk; diff --git a/api/src/controllers/permissions.ts b/api/src/controllers/permissions.ts index e0d51d1835..7de05a0286 100644 --- a/api/src/controllers/permissions.ts +++ b/api/src/controllers/permissions.ts @@ -5,6 +5,7 @@ import sanitizeQuery from '../middleware/sanitize-query'; import PermissionsService from '../services/permissions'; import useCollection from '../middleware/use-collection'; import checkCacheMiddleware from '../middleware/check-cache'; +import delCacheMiddleware from '../middleware/delete-cache'; import setCacheMiddleware from '../middleware/set-cache'; import MetaService from '../services/meta'; import { InvalidCredentialsException } from '../exceptions'; @@ -16,6 +17,7 @@ router.use(useCollection('directus_permissions')); router.post( '/', + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new PermissionsService({ accountability: req.accountability }); const primaryKey = await service.create(req.body); @@ -82,6 +84,7 @@ router.get( router.patch( '/:pk', + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new PermissionsService({ accountability: req.accountability }); const primaryKey = await service.update(req.body, Number(req.params.pk)); @@ -94,6 +97,7 @@ router.patch( router.delete( '/:pk', + delCacheMiddleware, asyncHandler(async (req, res) => { const service = new PermissionsService({ accountability: req.accountability }); await service.delete(Number(req.params.pk)); diff --git a/api/src/middleware/check-cache.ts b/api/src/middleware/check-cache.ts index c641fd8700..19ce08abc8 100644 --- a/api/src/middleware/check-cache.ts +++ b/api/src/middleware/check-cache.ts @@ -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'); } diff --git a/api/src/middleware/delete-cache.ts b/api/src/middleware/delete-cache.ts new file mode 100644 index 0000000000..52b3403429 --- /dev/null +++ b/api/src/middleware/delete-cache.ts @@ -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; diff --git a/api/src/middleware/set-cache.ts b/api/src/middleware/set-cache.ts index 4ab979eccc..9f63508365 100644 --- a/api/src/middleware/set-cache.ts +++ b/api/src/middleware/set-cache.ts @@ -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'); }