Temporarily disable caching

This commit is contained in:
rijkvanzanten
2020-09-08 17:22:34 -04:00
parent 28bfd2d1a4
commit 266c07fde7
13 changed files with 3 additions and 245 deletions

View File

@@ -1,11 +1,7 @@
import express from 'express';
import redis from 'redis';
import asyncHandler from 'express-async-handler';
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';
@@ -16,7 +12,6 @@ router.get(
'/',
useCollection('directus_activity'),
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
const metaService = new MetaService({ accountability: req.accountability });
@@ -29,14 +24,12 @@ router.get(
meta,
});
}),
setCacheMiddleware
);
router.get(
'/:pk',
useCollection('directus_activity'),
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
const record = await service.readByKey(req.params.pk, req.sanitizedQuery);
@@ -45,14 +38,12 @@ router.get(
data: record || null,
});
}),
setCacheMiddleware
);
router.post(
'/comment',
useCollection('directus_activity'),
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
@@ -76,7 +67,6 @@ 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);
@@ -91,7 +81,6 @@ 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);

View File

@@ -1,10 +1,6 @@
import { Router } from 'express';
import redis from 'redis';
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';
@@ -14,7 +10,6 @@ const router = Router();
router.post(
'/',
useCollection('directus_collections'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
@@ -28,7 +23,6 @@ router.post(
router.get(
'/',
useCollection('directus_collections'),
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
const metaService = new MetaService({ accountability: req.accountability });
@@ -37,14 +31,12 @@ router.get(
const meta = await metaService.getMetaForQuery(req.collection, {});
res.json({ data: collections || null, meta });
}),
setCacheMiddleware
);
router.get(
'/:collection',
useCollection('directus_collections'),
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
const collectionKey = req.params.collection.includes(',')
@@ -54,13 +46,11 @@ router.get(
res.json({ data: collection || null });
}),
setCacheMiddleware
);
router.patch(
'/:collection',
useCollection('directus_collections'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
const collectionKey = req.params.collection.includes(',')
@@ -75,7 +65,6 @@ 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(',')

View File

@@ -1,6 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import checkCacheMiddleware from '../middleware/check-cache';
import * as ExtensionsService from '../services/extensions';
import { RouteNotFoundException } from '../exceptions';
@@ -8,7 +7,6 @@ const router = Router();
router.get(
'/:type',
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const typeAllowList = ['interfaces', 'layouts', 'displays', 'modules'];

View File

@@ -2,58 +2,42 @@ import { Router } from 'express';
import asyncHandler from 'express-async-handler';
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, ForbiddenException } from '../exceptions';
import { InvalidPayloadException, ForbiddenException } from '../exceptions';
import Joi from 'joi';
import { Field } from '../types/field';
import useCollection from '../middleware/use-collection';
import { Accountability, types } from '../types';
import { types } from '../types';
const router = Router();
/**
* @TODO
*
* Add accountability / permissions handling to fields
*/
router.get(
'/',
useCollection('directus_fields'),
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
const fields = await service.readAll();
return res.json({ data: fields || null });
}),
setCacheMiddleware
);
router.get(
'/:collection',
validateCollection,
useCollection('directus_fields'),
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
const fields = await service.readAll(req.params.collection);
return res.json({ data: fields || null });
}),
setCacheMiddleware
);
router.get(
'/:collection/:field',
validateCollection,
useCollection('directus_fields'),
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -64,7 +48,6 @@ router.get(
return res.json({ data: field || null });
}),
setCacheMiddleware
);
const newFieldSchema = Joi.object({
@@ -85,7 +68,6 @@ router.post(
'/:collection',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -109,7 +91,6 @@ router.patch(
'/:collection',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -134,7 +115,6 @@ router.patch(
'/:collection/:field',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
// @todo: validate field
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -154,7 +134,6 @@ router.delete(
'/:collection/:field',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
await service.deleteField(req.params.collection, req.params.field);

View File

@@ -2,7 +2,6 @@ import express from 'express';
import asyncHandler from 'express-async-handler';
import sanitizeQuery from '../middleware/sanitize-query';
import useCollection from '../middleware/use-collection';
import FoldersService from '../services/folders';
import MetaService from '../services/meta';

View File

@@ -1,10 +1,6 @@
import express from 'express';
import redis from 'redis';
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';
@@ -16,7 +12,6 @@ router.post(
'/:collection',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
@@ -34,7 +29,6 @@ router.get(
'/:collection',
collectionExists,
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ItemsService(req.collection, { accountability: req.accountability });
const metaService = new MetaService({ accountability: req.accountability });
@@ -50,14 +44,12 @@ router.get(
data: records || null,
});
}),
setCacheMiddleware
);
router.get(
'/:collection/:pk',
collectionExists,
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
@@ -71,14 +63,12 @@ router.get(
data: result || null,
});
}),
setCacheMiddleware
);
router.patch(
'/:collection',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ItemsService(req.collection, { accountability: req.accountability });
@@ -99,7 +89,6 @@ router.patch(
'/:collection/:pk',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
@@ -118,7 +107,6 @@ 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;

View File

@@ -1,12 +1,8 @@
import express from 'express';
import redis from 'redis';
import asyncHandler from 'express-async-handler';
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';
import env from '../env';
@@ -17,7 +13,6 @@ 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);
@@ -30,7 +25,6 @@ router.post(
router.get(
'/',
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
const metaService = new MetaService({ accountability: req.accountability });
@@ -40,13 +34,11 @@ router.get(
return res.json({ data: item || null, meta });
}),
setCacheMiddleware
);
router.get(
'/me',
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
if (!req.accountability?.user || !req.accountability?.role) {
throw new InvalidCredentialsException();
@@ -71,19 +63,16 @@ router.get(
router.get(
'/:pk',
sanitizeQuery,
checkCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
const record = await service.readByKey(Number(req.params.pk), req.sanitizedQuery);
return res.json({ data: record || null });
}),
setCacheMiddleware
);
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));
@@ -96,7 +85,6 @@ router.patch(
router.delete(
'/:pk',
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
await service.delete(Number(req.params.pk));

View File

@@ -29,13 +29,7 @@ const defaults: Record<string, any> = {
CORS_ENABLED: false,
CACHE_ENABLED: true,
CACHE_DRIVER: 'redis',
CACHE_HOST: '127.0.0.1',
CACHE_PORT: '6379',
CACHE_REDIS_PASSWORD: null,
CACHE_TTL: 300,
CACHE_CHECK_LIVE: 300,
CACHE_ENABLED: false,
OAUTH_PROVIDERS: '',

View File

@@ -3,7 +3,6 @@ export * from './collection-not-found';
export * from './field-not-found';
export * from './forbidden';
export * from './hit-rate-limit';
export * from './invalid-cache-key';
export * from './invalid-credentials';
export * from './invalid-otp';
export * from './invalid-payload';

View File

@@ -1,40 +0,0 @@
/**
* 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) => {
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

@@ -1,42 +0,0 @@
/**
* 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 setCacheMiddleware: 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.setex(key, env.CACHE_TTL, JSON.stringify(res.json));
} else {
const cacheService = new CacheService();
cacheService.setCache(key, JSON.stringify(res.json));
}
return next();
});
export default setCacheMiddleware;

View File

@@ -1,83 +0,0 @@
import redis from 'redis';
import {
RateLimiterRedis,
RateLimiterMemory,
IRateLimiterStoreOptions,
IRateLimiterOptions,
} from 'rate-limiter-flexible';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
// options for the rate limiter are set below. Opts can be found
// at https://github.com/animir/node-rate-limiter-flexible/wiki/Options
let rateLimiterConfig = new RateLimiterMemory(getRateLimiterConfig());
// need to pick redis or memory
if (env.RATE_LIMIT_TYPE === 'redis') {
rateLimiterConfig = new RateLimiterRedis(getRateLimiterRedisConfig());
}
export default rateLimiterConfig;
function getRateLimiterConfig(): IRateLimiterOptions {
const config: any = {};
config.keyPrefix = 'rlflx';
for (const [key, value] of Object.entries(env)) {
if (key === 'CONSUMED_POINTS_LIMIT') {
config.points = value;
continue;
}
if (key === 'CONSUMED_RESET_DURATION') {
config.duration = value;
continue;
}
}
return config;
}
function getRateLimiterRedisConfig(): IRateLimiterStoreOptions {
const redisConfig: any = {};
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.REDIS_HOST,
port: env.REDIS_PORT,
password: env.REDIS_PASSWORD,
});
if (!redisClient) {
throw new RedisNotFoundException('Redis client does not exist');
}
redisConfig.keyPrefix = 'rlflx';
redisConfig.storeClient = redisClient;
for (const [key, value] of Object.entries(env)) {
if (key === 'CONSUMED_POINTS_LIMIT') {
redisConfig.points = value;
continue;
}
if (key === 'CONSUMED_RESET_DURATION') {
redisConfig.duration = value;
continue;
}
if (key === 'EXEC_EVENLY') {
redisConfig.execEvenly = value;
continue;
}
if (key === 'BLOCK_POINT_DURATION') {
redisConfig.blockDuration = value;
continue;
}
if (key === 'INMEMORY_BLOCK_CONSUMED') {
redisConfig.inmemoryBlockOnConsumed = value;
continue;
}
if (key === 'INMEMEMORY_BLOCK_DURATION') {
redisConfig.inmemoryBlockDuration = value;
continue;
}
}
return redisConfig;
}