Skip to content

Commit ab93d39

Browse files
chore: wip
1 parent 9b33cdd commit ab93d39

File tree

2 files changed

+90
-109
lines changed

2 files changed

+90
-109
lines changed
Lines changed: 39 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,46 @@
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
42
import { BentoCache, bentostore } from 'bentocache'
53
import { dynamoDbDriver } from 'bentocache/drivers/dynamodb'
4+
import { BaseCacheDriver } from './base'
65

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+
}
3813

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'
4522

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+
},
5240
})
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+
}
9244
}
45+
46+
export const dynamodb: DynamoDBCacheDriver = new DynamoDBCacheDriver()
Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
1+
// index.ts
12
import type { CacheDriver } from '@stacksjs/types'
23
import { config } from '@stacksjs/config'
3-
import { dynamodb } from './dynamodb'
4-
import { fileSystem } from './filesystem'
5-
4+
import { dynamodb, type DynamoDBOptions } from './dynamodb'
5+
import { fileSystem, type FileSystemOptions } from './filesystem'
66
import { memory } from './memory'
7-
import { redis } from './redis'
8-
9-
const driver = config.cache.driver || 'memory'
10-
11-
let driverInstance = fileSystem
12-
13-
if (driver === 'redis') {
14-
driverInstance = redis
15-
}
16-
17-
if (driver === 'fileSystem') {
18-
driverInstance = fileSystem
19-
}
20-
21-
if (driver === 'memory') {
22-
driverInstance = memory
23-
}
24-
25-
if (driver === 'dynamodb') {
26-
driverInstance = dynamodb
7+
import { redis, type RedisOptions } from './redis'
8+
9+
// Map of available drivers
10+
const drivers: Record<string, CacheDriver> = {
11+
redis,
12+
fileSystem,
13+
memory,
14+
dynamodb,
2715
}
2816

29-
export const cache: CacheDriver = driverInstance
17+
// Get configured driver name with fallback to memory
18+
const driverName = config.cache?.driver || 'memory'
19+
20+
// Select the appropriate driver
21+
const selectedDriver = drivers[driverName] || memory
22+
23+
// Export the selected driver as the default cache implementation
24+
export const cache: CacheDriver = selectedDriver
25+
26+
// Also export individual drivers and base class for direct usage
27+
export { BaseCacheDriver } from './base'
28+
export { dynamodb, DynamoDBCacheDriver, type DynamoDBOptions } from './dynamodb'
29+
export { fileSystem, FileSystemCacheDriver, type FileSystemOptions } from './filesystem'
30+
export { memory, MemoryCacheDriver } from './memory'
31+
export { redis, RedisCacheDriver, type RedisOptions } from './redis'
32+
33+
/**
34+
* Create a custom cache driver instance with specific options
35+
*/
36+
export function createCache(driver: 'redis', options?: RedisOptions): CacheDriver
37+
export function createCache(driver: 'fileSystem', options?: FileSystemOptions): CacheDriver
38+
export function createCache(driver: 'memory', options?: { maxSize?: number; maxItems?: number }): CacheDriver
39+
export function createCache(driver: 'dynamodb', options?: DynamoDBOptions): CacheDriver
40+
export function createCache(
41+
driver: 'redis' | 'fileSystem' | 'memory' | 'dynamodb',
42+
options?: any
43+
): CacheDriver {
44+
switch (driver) {
45+
case 'redis':
46+
return new RedisCacheDriver(options)
47+
case 'fileSystem':
48+
return new FileSystemCacheDriver(options)
49+
case 'memory':
50+
return new MemoryCacheDriver(options)
51+
case 'dynamodb':
52+
return new DynamoDBCacheDriver(options)
53+
default:
54+
return memory
55+
}
56+
}

0 commit comments

Comments
 (0)