Skip to content

Commit

Permalink
fix: use redis cache if redis env var is set
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed May 3, 2024
1 parent 079e8d7 commit ad539ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
7 changes: 6 additions & 1 deletion api/src/graphql/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getChildLogger } from '../../logger';
import "express-async-errors";
import { errorHandler } from '../../middleware/errors';
import * as Sentry from "@sentry/node";
import { initRedis } from '../../redis';
const logger = getChildLogger({ msgPrefix: 'SERVER' });

function loggerPlugin(): ApolloServerPlugin<Context> {
Expand Down Expand Up @@ -55,6 +56,10 @@ export const startServer = async (
await apolloIndex.start();
await apolloDrive.start();

if (process.env.REDIS_TLS_URL) {
await initRedis()
}

router.use(
'/drives',
cors<cors.CorsRequest>(),
Expand All @@ -81,7 +86,7 @@ export const startServer = async (
if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}

const httpServer = createHttpServer(app);
return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
Expand Down
18 changes: 3 additions & 15 deletions api/src/modules/document/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ import {
Listener,
ListenerFilter,
actions,
DocumentDriveState,
DocumentDriveAction
} from 'document-model-libs/document-drive';
import RedisCache from 'document-drive/cache/redis';
import MemoryCache from 'document-drive/cache/memory';
import { RedisClientType, createClient } from 'redis';

import { init } from './listenerManager';
import { getChildLogger } from '../../logger';
import DocumentDriveError from '../../errors/DocumentDriveError';
import { getRedisClient } from '../../redis';
import { redisClient } from '../../redis';

const logger = getChildLogger({ msgPrefix: 'Document Model' });

Expand All @@ -50,21 +48,11 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {


let driveServer: DocumentDriveServer;
// getRedisClient().then((redisClient) => {
// driveServer = new DocumentDriveServer(
// documentModels,
// new PrismaStorage(prisma as PrismaClient),
// redisClient ? new RedisCache(redisClient as RedisClientType) : new MemoryCache(),
// );
// }).catch((e) => {

// }).finally(() => {
// initialize();
// })

driveServer = new DocumentDriveServer(
documentModels,
new PrismaStorage(prisma as PrismaClient)
new PrismaStorage(prisma as PrismaClient),
redisClient ? new RedisCache(redisClient) : new MemoryCache()
);

initialize();
Expand Down
2 changes: 1 addition & 1 deletion api/src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createClient, RedisClientType } from 'redis';

export let redisClient: RedisClientType | null = null;

export const getRedisClient = async () => {
export const initRedis = async () => {
if (!redisClient) {
redisClient = await createClient({
url: process.env.REDIS_TLS_URL,
Expand Down

0 comments on commit ad539ed

Please sign in to comment.