Skip to content

Commit

Permalink
Merge pull request #37 from BJS-kr/fix/v11compat
Browse files Browse the repository at this point in the history
Fix/v11compat
  • Loading branch information
BJS-kr authored Feb 16, 2025
2 parents 889de27 + 37de6b0 commit e4b6d6f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 214 deletions.
Empty file added .prettierrc
Empty file.
26 changes: 12 additions & 14 deletions lib/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { CACHE } from './constants';
import { CacheKind, CacheOptions } from './types';
import { cacheEventEmitter } from './cache';
import { Injectable, OnModuleInit } from "@nestjs/common";
import { DiscoveryService, MetadataScanner, Reflector } from "@nestjs/core";
import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
import { CACHE } from "./constants";
import { CacheKind, CacheOptions } from "./types";
import { cacheEventEmitter } from "./cache";

@Injectable()
export class CacheService implements OnModuleInit {
constructor(
private readonly discoveryService: DiscoveryService,
private readonly scanner: MetadataScanner,
private readonly reflector: Reflector,
private readonly reflector: Reflector
) {}

private getAllInstances() {
Expand All @@ -35,9 +35,7 @@ export class CacheService implements OnModuleInit {
instance,
methodNames: [
...new Set(
this.scanner.getAllFilteredMethodNames(
Object.getPrototypeOf(instance),
),
this.scanner.getAllMethodNames(Object.getPrototypeOf(instance))
),
],
}))
Expand All @@ -56,19 +54,19 @@ export class CacheService implements OnModuleInit {
instance,
cacheOptions: this.reflector.get<CacheOptions<CacheKind>>(
CACHE,
method,
method
),
methodName,
})),
}))
);
}

private initializeAllPersistentCache() {
this.extractCacheMetadata(this.getAllInstances()).forEach(
({ instance, cacheOptions }) => {
const { kind, key } = cacheOptions;
if (kind === 'persistent') cacheEventEmitter.emit(key, instance);
},
if (kind === "persistent") cacheEventEmitter.emit(key, instance);
}
);
}

Expand Down
47 changes: 1 addition & 46 deletions lib/test/cache.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
import { Cache } from "../cache";
import { createClient } from "redis";
import { ICacheStorage } from "../types";
import { DefaultStorage } from "../default.storage";

export const defaultStorage = new DefaultStorage();
export const InMemCache = Cache({ storage: defaultStorage });

export class RedisCacheStorage implements ICacheStorage {
private client;

constructor() {
this.client = createClient({
url: "redis://localhost:6379",
name: "class",
});
this.client.connect();
}

async get(key: string): Promise<any> {
const value = await this.client.get(key);
try {
return JSON.parse(value);
} catch (error) {
// 파싱에 실패한 경우, 원래의 문자열 값 반환
return value;
}
}

async set(key: string, value: any, ttl?: number): Promise<void> {
const stringValue = JSON.stringify(value);
if (ttl) {
await this.client.setEx(key, ttl, stringValue);
} else {
await this.client.set(key, stringValue);
}
}

async delete(key: string): Promise<boolean> {
const result = await this.client.del(key);
return result === 1;
}

async has(key: string): Promise<boolean> {
const result = await this.client.exists(key);
return result === 1;
}
}

export const RedisCache = Cache({ storage: new RedisCacheStorage() });
export const AnotherRedisCache = Cache({ storage: new RedisCacheStorage() });
6 changes: 2 additions & 4 deletions lib/test/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import {
Query,
} from "@nestjs/common";
import { sleep, startTime } from "./util";
import { InMemCache, RedisCache, AnotherRedisCache } from "./cache.decorator";
import {
InMemTestService,
} from "./service";
import { InMemCache } from "./cache.decorator";
import { InMemTestService } from "./service";
import { SECOND } from "../time.constants";
@Controller()
export class InMemTestController {
Expand Down
4 changes: 2 additions & 2 deletions lib/test/in-memory.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { describe, it, before, after } from "node:test";
import { equal } from "node:assert/strict";
import { Test } from "@nestjs/testing";
import { InMemTestService } from "./service";
import { HttpServer, INestApplication } from "@nestjs/common";
import { INestApplication } from "@nestjs/common";
import { biggerThan, lessThan, sleep } from "./util";
import { CacheModule } from "../cache.module";
import { InMemTestController } from "./controller";
import { intervals } from "../cache";
import { defaultStorage } from "./cache.decorator";
import { App } from 'supertest/types';
import { App } from "supertest/types";

let httpServer: App;
let app: INestApplication;
Expand Down
4 changes: 2 additions & 2 deletions lib/test/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { sleep } from "./util";
import { InMemCache, RedisCache, AnotherRedisCache } from "./cache.decorator";
import { InMemCache } from "./cache.decorator";
import { SECOND } from "../time.constants";

@Injectable()
Expand Down Expand Up @@ -40,4 +40,4 @@ export class InMemTestService {
await sleep(1000);
return param.join("");
}
}
}
142 changes: 7 additions & 135 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e4b6d6f

Please sign in to comment.