Skip to content

Commit

Permalink
Merge pull request #25 from BJS-kr/feat/change_default_storage
Browse files Browse the repository at this point in the history
Feat/change_default_storage
  • Loading branch information
BJS-kr authored Jun 3, 2024
2 parents 078c477 + e3612e5 commit 1aef658
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
9 changes: 5 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class AppModule {}

```typescript
// imported 'Cache' is factory to receive storage that implements ICacheStorage
// in this example, we'll initialize in-memory cache
import { Cache, ICacheStorage } from "nestjs-omacache";

// for example, we can use redis for storage
Expand All @@ -59,11 +58,13 @@ class RedisStorage implements ICacheStorage {
const ExternalCache = Cache({ storage: new RedisStorage() })

// ...or
// you can just initialize it using default storage
// default storage is in-memory, timeout based TTL cache container
// it is mainly for simple scenarios, so it does not have cache eviction mechanism
// you can just initialize it using default storage(in-memory cache)
// default storage is based on lru-cache package, so it can handle TTL and cache eviction
// default max size is 10000
// make sure you are not making memory overhead by using default in-memory storage
const InMemoryCache = Cache();

// you can implement your custom in-memory cache, which is more configurable.
```

### Use it anywhere
Expand Down
31 changes: 10 additions & 21 deletions lib/default.storage.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { LRUCache } from "lru-cache";
import { ICacheStorage } from "./types";

export class DefaultStorage implements ICacheStorage {
#storage: Map<string, any> = new Map();
#timeouts: Map<string, NodeJS.Timeout> = new Map();
constructor(size: number = 10000) {
this.#storage = new LRUCache({
max: size,
});
}
#storage: LRUCache<any, any>;

get(key: string) {
return this.#storage.get(key);
}

set(key: string, value: any, ttl?: number) {
if (ttl && ttl > 0) {
const timeout = setTimeout(() => {
this.delete(key);
}, ttl);

this.#timeouts.set(key, timeout);
}

this.#storage.set(key, value);
this.#storage.set(key, value, {
ttl,
});
}

delete(key: string) {
const timer = this.#timeouts.get(key);

if (timer) {
clearTimeout(timer);
}

return this.#storage.delete(key);
}

Expand All @@ -35,10 +28,6 @@ export class DefaultStorage implements ICacheStorage {
}

clear() {
for (const timeout of this.#timeouts.values()) {
clearTimeout(timeout);
}

this.#storage.clear();
}
}
33 changes: 13 additions & 20 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-omacache",
"version": "1.1.0",
"version": "1.1.3",
"description": "simple, flexible and powerful cache decorator factory for NestJS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -46,5 +46,8 @@
"server"
],
"author": "hahajeng1234@gmail.com",
"license": "ISC"
"license": "ISC",
"dependencies": {
"lru-cache": "^10.2.2"
}
}

0 comments on commit 1aef658

Please sign in to comment.