@geekmidas/cache 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/memory.ts CHANGED
@@ -2,37 +2,37 @@ import { addSeconds } from 'date-fns';
2
2
  import { type Cache, getExpirationInSeconds } from './';
3
3
 
4
4
  export class InMemoryCache implements Cache {
5
- private store: Map<string, unknown> = new Map();
6
- private expirations: Map<string, Date> = new Map();
5
+ private store: Map<string, unknown> = new Map();
6
+ private expirations: Map<string, Date> = new Map();
7
7
 
8
- async get<T>(key: string): Promise<T | undefined> {
9
- const expiration = await this.ttl(key);
10
- if (!expiration) {
11
- await this.delete(key);
12
- return undefined;
13
- }
8
+ async get<T>(key: string): Promise<T | undefined> {
9
+ const expiration = await this.ttl(key);
10
+ if (!expiration) {
11
+ await this.delete(key);
12
+ return undefined;
13
+ }
14
14
 
15
- return this.store.get(key) as T | undefined;
16
- }
15
+ return this.store.get(key) as T | undefined;
16
+ }
17
17
 
18
- async ttl(key: string): Promise<number> {
19
- const expiration = this.expirations.get(key);
20
- if (!expiration) {
21
- return 0;
22
- }
18
+ async ttl(key: string): Promise<number> {
19
+ const expiration = this.expirations.get(key);
20
+ if (!expiration) {
21
+ return 0;
22
+ }
23
23
 
24
- return getExpirationInSeconds(expiration);
25
- }
24
+ return getExpirationInSeconds(expiration);
25
+ }
26
26
 
27
- async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {
28
- this.store.set(key, value);
29
- const now = new Date();
30
- const expiresAt = addSeconds(now, ttl);
31
- this.expirations.set(key, expiresAt);
32
- }
27
+ async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {
28
+ this.store.set(key, value);
29
+ const now = new Date();
30
+ const expiresAt = addSeconds(now, ttl);
31
+ this.expirations.set(key, expiresAt);
32
+ }
33
33
 
34
- async delete(key: string): Promise<void> {
35
- this.store.delete(key);
36
- this.expirations.delete(key);
37
- }
34
+ async delete(key: string): Promise<void> {
35
+ this.store.delete(key);
36
+ this.expirations.delete(key);
37
+ }
38
38
  }
package/src/upstash.ts CHANGED
@@ -2,40 +2,40 @@ import { Redis } from '@upstash/redis';
2
2
  import type { Cache } from './';
3
3
 
4
4
  export class UpstashCache implements Cache {
5
- private client: Redis;
5
+ private client: Redis;
6
6
 
7
- constructor(url: string, token: string) {
8
- this.client = new Redis({
9
- url,
10
- token,
11
- });
12
- }
7
+ constructor(url: string, token: string) {
8
+ this.client = new Redis({
9
+ url,
10
+ token,
11
+ });
12
+ }
13
13
 
14
- async ttl(key: string): Promise<number> {
15
- const ttl = await this.client.ttl(key);
14
+ async ttl(key: string): Promise<number> {
15
+ const ttl = await this.client.ttl(key);
16
16
 
17
- if (ttl === -2) {
18
- return 0; // Key does not exist
19
- }
17
+ if (ttl === -2) {
18
+ return 0; // Key does not exist
19
+ }
20
20
 
21
- return ttl; // Returns TTL in seconds
22
- }
21
+ return ttl; // Returns TTL in seconds
22
+ }
23
23
 
24
- async get<T>(key: string): Promise<T | undefined> {
25
- const v = await this.client.get(key);
24
+ async get<T>(key: string): Promise<T | undefined> {
25
+ const v = await this.client.get(key);
26
26
 
27
- if (v === null) {
28
- return undefined;
29
- }
27
+ if (v === null) {
28
+ return undefined;
29
+ }
30
30
 
31
- return v as T;
32
- }
31
+ return v as T;
32
+ }
33
33
 
34
- async set<T>(key: string, value: T, ttl = 3600): Promise<void> {
35
- await this.client.set(key, value, { ex: ttl });
36
- }
34
+ async set<T>(key: string, value: T, ttl = 3600): Promise<void> {
35
+ await this.client.set(key, value, { ex: ttl });
36
+ }
37
37
 
38
- async delete(key: string): Promise<void> {
39
- await this.client.del(key);
40
- }
38
+ async delete(key: string): Promise<void> {
39
+ await this.client.del(key);
40
+ }
41
41
  }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "composite": true
7
+ },
8
+ "include": ["src/**/*"]
9
+ }
package/tsdown.config.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { defineConfig } from 'tsdown';
2
2
 
3
3
  export default defineConfig({
4
- external: ['expo-secure-store', '@upstash/redis'],
4
+ external: ['expo-secure-store', '@upstash/redis'],
5
5
  });