@stacksjs/cache 0.58.48 → 0.58.49
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/package.json +3 -2
- package/src/drivers/dynamodb.ts +119 -0
- package/src/drivers/index.ts +2 -0
- package/src/drivers/redis.ts +37 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/cache",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.58.
|
|
4
|
+
"version": "0.58.49",
|
|
5
5
|
"description": "Caching the Stacks way.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
],
|
|
43
43
|
"files": [
|
|
44
44
|
"README.md",
|
|
45
|
-
"dist"
|
|
45
|
+
"dist",
|
|
46
|
+
"src"
|
|
46
47
|
],
|
|
47
48
|
"scripts": {
|
|
48
49
|
"build": "bun --bun build.ts",
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export {}
|
|
2
|
+
|
|
3
|
+
// import type { PutItemCommandInput } from '@aws-sdk/client-dynamodb'
|
|
4
|
+
// import { DynamoDB, ListTablesCommand } from '@aws-sdk/client-dynamodb'
|
|
5
|
+
// import { cache } from '@stacksjs/config'
|
|
6
|
+
|
|
7
|
+
// const valueAttribute = 'value'
|
|
8
|
+
// const keyAttribute = 'key'
|
|
9
|
+
|
|
10
|
+
// const tableName = cache.dynamodb.table
|
|
11
|
+
|
|
12
|
+
// const dynamodb = new DynamoDB({ region: cache.dynamodb.region })
|
|
13
|
+
|
|
14
|
+
// async function createTable() {
|
|
15
|
+
// const tables = await dynamodb.send(new ListTablesCommand({}))
|
|
16
|
+
|
|
17
|
+
// const tableExists = tables.TableNames?.includes('cache')
|
|
18
|
+
|
|
19
|
+
// if (tableExists)
|
|
20
|
+
// return
|
|
21
|
+
|
|
22
|
+
// const params = {
|
|
23
|
+
// AttributeDefinitions: [
|
|
24
|
+
// {
|
|
25
|
+
// AttributeName: 'key',
|
|
26
|
+
// AttributeType: 'S',
|
|
27
|
+
// },
|
|
28
|
+
// ],
|
|
29
|
+
// KeySchema: [
|
|
30
|
+
// {
|
|
31
|
+
// AttributeName: 'key',
|
|
32
|
+
// KeyType: 'HASH',
|
|
33
|
+
// },
|
|
34
|
+
// ],
|
|
35
|
+
// ProvisionedThroughput: {
|
|
36
|
+
// ReadCapacityUnits: 5,
|
|
37
|
+
// WriteCapacityUnits: 5,
|
|
38
|
+
// },
|
|
39
|
+
// TableName: tableName,
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
// await dynamodb.createTable(params)
|
|
43
|
+
// }
|
|
44
|
+
|
|
45
|
+
// async function set(key: string, value: string | number): Promise<void> {
|
|
46
|
+
// const params: PutItemCommandInput = {
|
|
47
|
+
// TableName: tableName,
|
|
48
|
+
// Item: {
|
|
49
|
+
// [keyAttribute]: {
|
|
50
|
+
// S: key,
|
|
51
|
+
// },
|
|
52
|
+
// [valueAttribute]: {
|
|
53
|
+
// [getValueType(value)]: serialize(value),
|
|
54
|
+
// },
|
|
55
|
+
// },
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
// await dynamodb.putItem(params)
|
|
59
|
+
// }
|
|
60
|
+
|
|
61
|
+
// async function get(key: string): Promise<string | undefined | null> {
|
|
62
|
+
// const params = {
|
|
63
|
+
// TableName: tableName,
|
|
64
|
+
// Key: {
|
|
65
|
+
// [keyAttribute]: {
|
|
66
|
+
// S: key,
|
|
67
|
+
// },
|
|
68
|
+
// },
|
|
69
|
+
// }
|
|
70
|
+
|
|
71
|
+
// const response = await dynamodb.getItem(params)
|
|
72
|
+
|
|
73
|
+
// if (!response.Item)
|
|
74
|
+
// return null
|
|
75
|
+
|
|
76
|
+
// return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N
|
|
77
|
+
// }
|
|
78
|
+
|
|
79
|
+
// function getValueType(value: string | number) {
|
|
80
|
+
// if (typeof value === 'string')
|
|
81
|
+
// return 'S'
|
|
82
|
+
|
|
83
|
+
// if (typeof value === 'number')
|
|
84
|
+
// return 'N'
|
|
85
|
+
|
|
86
|
+
// return 'S'
|
|
87
|
+
// }
|
|
88
|
+
|
|
89
|
+
// async function remove(key: string): Promise<void> {
|
|
90
|
+
// const params = {
|
|
91
|
+
// TableName: tableName,
|
|
92
|
+
// Key: {
|
|
93
|
+
// [keyAttribute]: {
|
|
94
|
+
// S: key,
|
|
95
|
+
// },
|
|
96
|
+
// },
|
|
97
|
+
// }
|
|
98
|
+
|
|
99
|
+
// await dynamodb.deleteItem(params)
|
|
100
|
+
// }
|
|
101
|
+
|
|
102
|
+
// async function del(key: string): Promise<void> {
|
|
103
|
+
// const params = {
|
|
104
|
+
// TableName: tableName,
|
|
105
|
+
// Key: {
|
|
106
|
+
// [keyAttribute]: {
|
|
107
|
+
// S: key,
|
|
108
|
+
// },
|
|
109
|
+
// },
|
|
110
|
+
// }
|
|
111
|
+
|
|
112
|
+
// await dynamodb.deleteItem(params)
|
|
113
|
+
// }
|
|
114
|
+
|
|
115
|
+
// function serialize(value: string | number) {
|
|
116
|
+
// return String(value)
|
|
117
|
+
// }
|
|
118
|
+
|
|
119
|
+
// export { set, get, remove, del, createTable }
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { cache } from '@stacksjs/config'
|
|
2
|
+
import Redis, { Command } from 'ioredis'
|
|
3
|
+
|
|
4
|
+
export const client = new Redis({
|
|
5
|
+
host: cache.drivers?.redis?.host,
|
|
6
|
+
port: cache.drivers?.redis?.port,
|
|
7
|
+
username: cache.drivers?.redis?.username,
|
|
8
|
+
password: cache.drivers?.redis?.password,
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
export async function set(key: string, value: any): Promise<void> {
|
|
12
|
+
await client.set(key, value)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function get(key: string): Promise<any> {
|
|
16
|
+
const value = await client.get(key)
|
|
17
|
+
|
|
18
|
+
return value
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function remove(key: string): Promise<void> {
|
|
22
|
+
await client.del(key)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function del(key: string): Promise<void> {
|
|
26
|
+
await client.del(key)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function flushAll(): Promise<void> {
|
|
30
|
+
const command = new Command('FLUSHALL', ['ASYNC'], { replyEncoding: 'utf-8' }, () => {})
|
|
31
|
+
await client.sendCommand(command)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function flushDB(): Promise<void> {
|
|
35
|
+
const command = new Command('FLUSHDB', ['ASYNC'], { replyEncoding: 'utf-8' }, () => {})
|
|
36
|
+
await client.sendCommand(command)
|
|
37
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './drivers'
|