1
- import type { CacheDriver } from '@stacksjs/types'
2
- import type { GetOptions } from 'bentocache/types'
3
- import process from 'node:process'
1
+ // dynamodb-cache-driver.ts
4
2
import { BentoCache , bentostore } from 'bentocache'
5
3
import { dynamoDbDriver } from 'bentocache/drivers/dynamodb'
4
+ import { BaseCacheDriver } from './base'
6
5
7
- const awsAccessKeyId = process . env . AWS_ACCESS_KEY_ID || 'dummy'
8
- const awsSecretAccessKey = process . env . AWS_SECRET_ACCESS_KEY || 'dummy'
9
- const dynamoEndpoint = process . env . AWS_DYNAMODB_ENDPOINT || 'http://localhost:8000'
10
- const tableName = process . env . AWS_DYNAMODB_TABLE || 'stacks'
11
- const region = process . env . AWS_REGION || 'us-east-1'
12
-
13
- const client = new BentoCache ( {
14
- default : 'dynamo' ,
15
- stores : {
16
- dynamo : bentostore ( ) . useL2Layer (
17
- dynamoDbDriver ( {
18
- endpoint : dynamoEndpoint ,
19
- region,
20
- table : {
21
- name : tableName ,
22
- } ,
23
- credentials : {
24
- accessKeyId : awsAccessKeyId ,
25
- secretAccessKey : awsSecretAccessKey ,
26
- } ,
27
- } ) ,
28
- ) ,
29
- } ,
30
- } )
31
-
32
- export const dynamodb : CacheDriver = {
33
- async set ( key : string , value : string , ttl ?: number ) : Promise < void > {
34
- const data : { key : string , value : string , gracePeriod ?: { enabled : boolean , duration : string } } = {
35
- key,
36
- value,
37
- }
6
+ export interface DynamoDBOptions {
7
+ endpoint ?: string
8
+ region ?: string
9
+ tableName ?: string
10
+ accessKeyId ?: string
11
+ secretAccessKey ?: string
12
+ }
38
13
39
- if ( ttl ) {
40
- data . gracePeriod = {
41
- enabled : true ,
42
- duration : `${ ttl } m` ,
43
- }
44
- }
14
+ export class DynamoDBCacheDriver extends BaseCacheDriver {
15
+ constructor ( options : DynamoDBOptions = { } ) {
16
+ // Use environment variables with fallbacks
17
+ const awsAccessKeyId = options . accessKeyId ?? process . env . AWS_ACCESS_KEY_ID ?? 'dummy'
18
+ const awsSecretAccessKey = options . secretAccessKey ?? process . env . AWS_SECRET_ACCESS_KEY ?? 'dummy'
19
+ const dynamoEndpoint = options . endpoint ?? process . env . AWS_DYNAMODB_ENDPOINT ?? 'http://localhost:8000'
20
+ const tableName = options . tableName ?? process . env . AWS_DYNAMODB_TABLE ?? 'stacks'
21
+ const region = options . region ?? process . env . AWS_REGION ?? 'us-east-1'
45
22
46
- await client . set ( data )
47
- } ,
48
- async setForever ( key : string , value : string ) : Promise < void > {
49
- await client . setForever ( {
50
- key,
51
- value,
23
+ const client = new BentoCache ( {
24
+ default : 'dynamo' ,
25
+ stores : {
26
+ dynamo : bentostore ( ) . useL2Layer (
27
+ dynamoDbDriver ( {
28
+ endpoint : dynamoEndpoint ,
29
+ region,
30
+ table : {
31
+ name : tableName ,
32
+ } ,
33
+ credentials : {
34
+ accessKeyId : awsAccessKeyId ,
35
+ secretAccessKey : awsSecretAccessKey ,
36
+ } ,
37
+ } ) ,
38
+ ) ,
39
+ } ,
52
40
} )
53
- } ,
54
- async get < T > ( key : GetOptions < T > ) : Promise < T > {
55
- const items = await client . get < T > ( key )
56
-
57
- return items
58
- } ,
59
- async getOrSet < T > ( key : string , value : T ) : Promise < T > {
60
- const result = await client . getOrSet < T > ( {
61
- key,
62
- factory : async ( ) => value ,
63
- } )
64
-
65
- return result
66
- } ,
67
- async del ( key : string ) : Promise < void > {
68
- await client . delete ( { key } )
69
- } ,
70
- async deleteMany ( keys : string [ ] ) : Promise < void > {
71
- await client . deleteMany ( { keys } )
72
- } ,
73
- async remove ( key : string ) : Promise < void > {
74
- await client . delete ( { key } )
75
- } ,
76
- async has ( key : string ) : Promise < boolean > {
77
- return await client . has ( { key } )
78
- } ,
79
- async missing ( key : string ) : Promise < boolean > {
80
- return await client . missing ( { key } )
81
- } ,
82
- async deleteAll ( ) : Promise < void > {
83
- await client . clear ( )
84
- } ,
85
- async clear ( ) : Promise < void > {
86
- await client . clear ( )
87
- } ,
88
- async disconnect ( ) : Promise < void > {
89
- await client . disconnect ( )
90
- } ,
91
- client,
41
+
42
+ super ( client )
43
+ }
92
44
}
45
+
46
+ export const dynamodb : DynamoDBCacheDriver = new DynamoDBCacheDriver ( )
0 commit comments