Skip to content

Commit

Permalink
Migrate external cache to our library
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Sep 20, 2024
1 parent bd548c0 commit 44c9957
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 355 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@nestjs/graphql": "^12.0.9",
"@nestjs/platform-express": "^10.2.7",
"@patarapolw/prettyprint": "^1.0.3",
"@seedcompany/cache": "^1.0.0",
"@seedcompany/common": ">=0.13.1 <1",
"@seedcompany/data-loader": "^0.5.4",
"@seedcompany/nest": ">=0.1 <1",
Expand Down Expand Up @@ -84,7 +85,7 @@
"jsonwebtoken": "^9.0.2",
"lazy-get-decorator": "^2.2.1",
"lodash": "npm:lodash-es@^4.17.21",
"lru-cache": "^7.18.3",
"lru-cache": "^11.0.1",
"luxon": "^3.5.0",
"mime": "beta",
"nanoid": "^4.0.2",
Expand Down
17 changes: 0 additions & 17 deletions src/core/cache/backing.interface.ts

This file was deleted.

60 changes: 0 additions & 60 deletions src/core/cache/cache.decorator.ts

This file was deleted.

23 changes: 11 additions & 12 deletions src/core/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Module } from '@nestjs/common';
import { CacheModule as LibCacheModule } from '@seedcompany/cache';
import { LruStore } from '@seedcompany/cache/stores/lru';
import { RedisStore } from '@seedcompany/cache/stores/redis';
import Redis from 'ioredis';
import { ConfigService } from '../config/config.service';
import { ILogger, LoggerToken } from '../logger';
import { CacheBackingService } from './backing.interface';
import { CacheService } from './cache.service';
import { InMemoryCache } from './in-memory.cache';
import { RedisCache } from './redis.cache';

@Module({
providers: [
CacheService,
{
provide: CacheBackingService,
imports: [
LibCacheModule.registerAsync({
inject: [ConfigService, LoggerToken('redis')],
useFactory: (config: ConfigService, logger: ILogger) => {
const connStr = config.redis.url;
if (!connStr) {
return new InMemoryCache(config.lruCache);
const store = new LruStore(config.lruCache);
return { store };
}
const redis = new Redis(connStr);
redis.on('ready', () => {
Expand All @@ -25,10 +23,11 @@ import { RedisCache } from './redis.cache';
redis.on('error', (error) => {
logger.error('Connection encountered an error', { error });
});
return new RedisCache(redis);
const store = new RedisStore(redis);
return { store };
},
},
}),
],
exports: [CacheService],
exports: [LibCacheModule],
})
export class CacheModule {}
130 changes: 0 additions & 130 deletions src/core/cache/cache.service.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/core/cache/in-memory.cache.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/core/cache/index.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/core/cache/redis.cache.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/core/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@seedcompany/nestjs-email';
import { CookieOptions } from 'express';
import type { Server as HttpServer } from 'http';
import LRUCache from 'lru-cache';
import { LRUCache } from 'lru-cache';
import { Duration, DurationLike } from 'luxon';
import { nanoid } from 'nanoid';
import { Config as Neo4JDriverConfig } from 'neo4j-driver';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const makeConfig = (env: EnvironmentService) =>
ttl: env.duration('LRU_CACHE_TTL').optional()?.as('milliseconds'),
max: env.number('LRU_CACHE_MAX').optional(),
maxSize: env.number('LRU_CACHE_MAX_SIZE').optional('30MB'),
} satisfies LRUCache.Options<string, unknown>;
} satisfies LRUCache.Options<string, unknown, unknown>;

httpTimeouts = {
/** @see HttpServer.keepAliveTimeout */
Expand Down
4 changes: 2 additions & 2 deletions src/core/edgedb/alias-id-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { isUUID } from 'class-validator';
import DataLoader from 'dataloader';
import LRU from 'lru-cache';
import { LRUCache as LRU } from 'lru-cache';
import { ID, NotFoundException } from '~/common';
import { IdResolver } from '~/common/validators/short-id.validator';
import { ILogger, Logger } from '~/core/logger';
Expand All @@ -21,7 +21,7 @@ export class AliasIdResolver implements IdResolver {
// and there's no cache invalidation, we'll just use an LRU cache
cacheMap: new LRU({
max: 10_000,
}),
}) as DataLoader.CacheMap<ID, Promise<ID>>,
});
}

Expand Down
Loading

0 comments on commit 44c9957

Please sign in to comment.