@stacksjs/cache 0.41.1 → 0.42.1
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/README.md +2 -2
- package/dist/drivers/dynamodb.d.ts +2 -1
- package/dist/drivers/dynamodb.mjs +33 -6
- package/package.json +9 -11
- package/dist/tests/DynamoDB.test.d.ts +0 -1
- package/dist/tests/DynamoDB.test.mjs +0 -75
- package/dist/tests/Memcached.test.d.ts +0 -1
- package/dist/tests/Memcached.test.mjs +0 -37
- package/dist/tests/Redis.test.d.ts +0 -1
- package/dist/tests/Redis.test.mjs +0 -59
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ Please see our [releases](https://github.com/stacksjs/stacks/releases) page for
|
|
|
40
40
|
|
|
41
41
|
## 💪🏼 Contributing
|
|
42
42
|
|
|
43
|
-
Please
|
|
43
|
+
Please review the [Contributing Guide](https://github.com/stacksjs/contributing) for details.
|
|
44
44
|
|
|
45
45
|
## 🏝 Community
|
|
46
46
|
|
|
@@ -50,7 +50,7 @@ For help, discussion about best practices, or any other conversation that would
|
|
|
50
50
|
|
|
51
51
|
For casual chit-chat with others using this package:
|
|
52
52
|
|
|
53
|
-
[Join the
|
|
53
|
+
[Join the Stacks Discord Server](https://discord.ow3.org)
|
|
54
54
|
|
|
55
55
|
## 📄 License
|
|
56
56
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
declare function createTable(): Promise<void>;
|
|
1
2
|
declare function set(key: string, value: string | number): Promise<void>;
|
|
2
3
|
declare function get(key: string): Promise<string | undefined | null>;
|
|
3
4
|
declare function remove(key: string): Promise<void>;
|
|
4
5
|
declare function del(key: string): Promise<void>;
|
|
5
|
-
export { set, get, remove, del };
|
|
6
|
+
export { set, get, remove, del, createTable };
|
|
@@ -1,11 +1,38 @@
|
|
|
1
|
-
import { DynamoDB } from "@aws-sdk/client-dynamodb";
|
|
1
|
+
import { DynamoDB, ListTablesCommand } from "@aws-sdk/client-dynamodb";
|
|
2
2
|
import { cache } from "@stacksjs/config";
|
|
3
3
|
const valueAttribute = "value";
|
|
4
4
|
const keyAttribute = "key";
|
|
5
|
+
const tableName = cache.dynamodb.table;
|
|
5
6
|
const dynamodb = new DynamoDB({ region: cache.dynamodb.region });
|
|
7
|
+
async function createTable() {
|
|
8
|
+
const tables = await dynamodb.send(new ListTablesCommand({}));
|
|
9
|
+
const tableExists = tables.TableNames?.includes("cache");
|
|
10
|
+
if (tableExists)
|
|
11
|
+
return;
|
|
12
|
+
const params = {
|
|
13
|
+
AttributeDefinitions: [
|
|
14
|
+
{
|
|
15
|
+
AttributeName: "key",
|
|
16
|
+
AttributeType: "S"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
KeySchema: [
|
|
20
|
+
{
|
|
21
|
+
AttributeName: "key",
|
|
22
|
+
KeyType: "HASH"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
ProvisionedThroughput: {
|
|
26
|
+
ReadCapacityUnits: 5,
|
|
27
|
+
WriteCapacityUnits: 5
|
|
28
|
+
},
|
|
29
|
+
TableName: tableName
|
|
30
|
+
};
|
|
31
|
+
await dynamodb.createTable(params);
|
|
32
|
+
}
|
|
6
33
|
async function set(key, value) {
|
|
7
34
|
const params = {
|
|
8
|
-
TableName:
|
|
35
|
+
TableName: tableName,
|
|
9
36
|
Item: {
|
|
10
37
|
[keyAttribute]: {
|
|
11
38
|
S: key
|
|
@@ -19,7 +46,7 @@ async function set(key, value) {
|
|
|
19
46
|
}
|
|
20
47
|
async function get(key) {
|
|
21
48
|
const params = {
|
|
22
|
-
TableName:
|
|
49
|
+
TableName: tableName,
|
|
23
50
|
Key: {
|
|
24
51
|
[keyAttribute]: {
|
|
25
52
|
S: key
|
|
@@ -40,7 +67,7 @@ function getValueType(value) {
|
|
|
40
67
|
}
|
|
41
68
|
async function remove(key) {
|
|
42
69
|
const params = {
|
|
43
|
-
TableName:
|
|
70
|
+
TableName: tableName,
|
|
44
71
|
Key: {
|
|
45
72
|
[keyAttribute]: {
|
|
46
73
|
S: key
|
|
@@ -51,7 +78,7 @@ async function remove(key) {
|
|
|
51
78
|
}
|
|
52
79
|
async function del(key) {
|
|
53
80
|
const params = {
|
|
54
|
-
TableName:
|
|
81
|
+
TableName: tableName,
|
|
55
82
|
Key: {
|
|
56
83
|
[keyAttribute]: {
|
|
57
84
|
S: key
|
|
@@ -63,4 +90,4 @@ async function del(key) {
|
|
|
63
90
|
function serialize(value) {
|
|
64
91
|
return String(value);
|
|
65
92
|
}
|
|
66
|
-
export { set, get, remove, del };
|
|
93
|
+
export { set, get, remove, del, createTable };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/cache",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@7.
|
|
4
|
+
"version": "0.42.1",
|
|
5
|
+
"packageManager": "pnpm@7.17.0",
|
|
6
6
|
"description": "The Stacks way to test.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"license": "MIT",
|
|
@@ -36,23 +36,21 @@
|
|
|
36
36
|
],
|
|
37
37
|
"engines": {
|
|
38
38
|
"node": ">=v18.12.1",
|
|
39
|
-
"pnpm": ">=7.
|
|
39
|
+
"pnpm": ">=7.17.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@
|
|
42
|
+
"@aws-sdk/client-dynamodb": "^3.215.0",
|
|
43
|
+
"@types/memjs": "^1.2.4",
|
|
43
44
|
"memcached": "^2.2.2",
|
|
45
|
+
"memjs": "^1.3.0",
|
|
44
46
|
"redis": "^4.5.0",
|
|
45
|
-
"vitest": "^0.25.2"
|
|
46
|
-
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"@aws-sdk/client-dynamodb": "^3.211.0",
|
|
49
|
-
"memcached": "^2.2.2",
|
|
50
|
-
"memjs": "^1.3.0"
|
|
47
|
+
"vitest": "^0.25.2",
|
|
48
|
+
"@stacksjs/config": "0.42.1"
|
|
51
49
|
},
|
|
52
50
|
"devDependencies": {
|
|
53
51
|
"mkdist": "^1.0.0",
|
|
54
52
|
"typescript": "^4.9.3",
|
|
55
|
-
"
|
|
53
|
+
"@stacksjs/testing": "0.42.1"
|
|
56
54
|
},
|
|
57
55
|
"scripts": {
|
|
58
56
|
"build": "mkdist -d",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { DynamoDB } from "@aws-sdk/client-dynamodb";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
const dynamodb = new DynamoDB({ region: "us-east-1" });
|
|
4
|
-
describe("DynamoDB test", () => {
|
|
5
|
-
it("it should set dynamodb cache", async () => {
|
|
6
|
-
await set("foo", "bar");
|
|
7
|
-
const value = await get("foo");
|
|
8
|
-
expect(value).toBe("bar");
|
|
9
|
-
});
|
|
10
|
-
it("it should get dynamodb cache", async () => {
|
|
11
|
-
await set("foo", "bar");
|
|
12
|
-
const value = await get("foo");
|
|
13
|
-
expect(value).toBe("bar");
|
|
14
|
-
});
|
|
15
|
-
it("it should delete dynamodb cache", async () => {
|
|
16
|
-
await set("foo", "bar");
|
|
17
|
-
await remove("foo");
|
|
18
|
-
const value = await get("foo");
|
|
19
|
-
expect(value).toBe(null);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
async function set(key, value) {
|
|
23
|
-
const valueAttribute = "value";
|
|
24
|
-
const keyAttribute = "key";
|
|
25
|
-
const params = {
|
|
26
|
-
TableName: "cache",
|
|
27
|
-
Item: {
|
|
28
|
-
[keyAttribute]: {
|
|
29
|
-
S: key
|
|
30
|
-
},
|
|
31
|
-
[valueAttribute]: {
|
|
32
|
-
[getValueType(value)]: serialize(value)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
await dynamodb.putItem(params);
|
|
37
|
-
}
|
|
38
|
-
async function get(key) {
|
|
39
|
-
const valueAttribute = "value";
|
|
40
|
-
const keyAttribute = "key";
|
|
41
|
-
const params = {
|
|
42
|
-
TableName: "cache",
|
|
43
|
-
Key: {
|
|
44
|
-
[keyAttribute]: {
|
|
45
|
-
S: key
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
const response = await dynamodb.getItem(params);
|
|
50
|
-
if (!response.Item)
|
|
51
|
-
return null;
|
|
52
|
-
return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N;
|
|
53
|
-
}
|
|
54
|
-
function getValueType(value) {
|
|
55
|
-
if (typeof value === "string")
|
|
56
|
-
return "S";
|
|
57
|
-
if (typeof value === "number")
|
|
58
|
-
return "N";
|
|
59
|
-
return "S";
|
|
60
|
-
}
|
|
61
|
-
async function remove(key) {
|
|
62
|
-
const keyAttribute = "key";
|
|
63
|
-
const params = {
|
|
64
|
-
TableName: "cache",
|
|
65
|
-
Key: {
|
|
66
|
-
[keyAttribute]: {
|
|
67
|
-
S: key
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
await dynamodb.deleteItem(params);
|
|
72
|
-
}
|
|
73
|
-
function serialize(value) {
|
|
74
|
-
return String(value);
|
|
75
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import Memcached from "memjs";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
const client = Memcached.Client.create("127.0.0.1:11211");
|
|
4
|
-
async function set(key, value, duration) {
|
|
5
|
-
await client.set(key, value, { expires: duration });
|
|
6
|
-
}
|
|
7
|
-
async function get(key) {
|
|
8
|
-
const value = await client.get(key);
|
|
9
|
-
const result = value.value ? value.value.toString() : null;
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
12
|
-
async function del(key) {
|
|
13
|
-
await client.delete(key);
|
|
14
|
-
}
|
|
15
|
-
async function flush() {
|
|
16
|
-
await client.flush();
|
|
17
|
-
}
|
|
18
|
-
describe("MemcachedTest", () => {
|
|
19
|
-
it("it should set memcached cache", async () => {
|
|
20
|
-
await set("test", "test", 10 * 60);
|
|
21
|
-
expect(await get("test")).toBe("test");
|
|
22
|
-
});
|
|
23
|
-
it("it should get memcached cache", async () => {
|
|
24
|
-
await set("test", "test", 10 * 60);
|
|
25
|
-
expect(await get("test")).toBe("test");
|
|
26
|
-
});
|
|
27
|
-
it("it should delete memcached cache", async () => {
|
|
28
|
-
await set("test", "test", 10 * 60);
|
|
29
|
-
await del("test");
|
|
30
|
-
expect(await get("test")).toBe(null);
|
|
31
|
-
});
|
|
32
|
-
it("it should flush memcached cache", async () => {
|
|
33
|
-
await set("test", "test", 10 * 60);
|
|
34
|
-
await flush();
|
|
35
|
-
expect(await get("test")).toBe(null);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { createClient } from "redis";
|
|
3
|
-
const client = createClient({
|
|
4
|
-
socket: {
|
|
5
|
-
host: "127.0.0.1",
|
|
6
|
-
port: 6379
|
|
7
|
-
},
|
|
8
|
-
password: ""
|
|
9
|
-
});
|
|
10
|
-
await client.connect();
|
|
11
|
-
async function set(key, value) {
|
|
12
|
-
await client.set(key, value);
|
|
13
|
-
}
|
|
14
|
-
async function get(key) {
|
|
15
|
-
const value = await client.get(key);
|
|
16
|
-
return value;
|
|
17
|
-
}
|
|
18
|
-
async function remove(key) {
|
|
19
|
-
await client.del(key);
|
|
20
|
-
}
|
|
21
|
-
async function del(key) {
|
|
22
|
-
await client.del(key);
|
|
23
|
-
}
|
|
24
|
-
async function flushAll() {
|
|
25
|
-
await client.sendCommand(["FLUSHALL", "ASYNC"]);
|
|
26
|
-
}
|
|
27
|
-
async function flushDB() {
|
|
28
|
-
await client.sendCommand(["FLUSHDB", "ASYNC"]);
|
|
29
|
-
}
|
|
30
|
-
describe("RedisTest", () => {
|
|
31
|
-
it("it should set redis cache", async () => {
|
|
32
|
-
await set("test", "test");
|
|
33
|
-
expect(await get("test")).toBe("test");
|
|
34
|
-
});
|
|
35
|
-
it("it should get redis cache", async () => {
|
|
36
|
-
await set("test", "test");
|
|
37
|
-
expect(await get("test")).toBe("test");
|
|
38
|
-
});
|
|
39
|
-
it("it should remove cache", async () => {
|
|
40
|
-
await set("test", "test");
|
|
41
|
-
await remove("test");
|
|
42
|
-
expect(await get("test")).toBe(null);
|
|
43
|
-
});
|
|
44
|
-
it("it should del cache", async () => {
|
|
45
|
-
await set("test", "test");
|
|
46
|
-
await del("test");
|
|
47
|
-
expect(await get("test")).toBe(null);
|
|
48
|
-
});
|
|
49
|
-
it("it should flush all cache", async () => {
|
|
50
|
-
await set("test", "test");
|
|
51
|
-
await flushAll();
|
|
52
|
-
expect(await get("test")).toBe(null);
|
|
53
|
-
});
|
|
54
|
-
it("it should flush all DB", async () => {
|
|
55
|
-
await set("test", "test");
|
|
56
|
-
await flushDB();
|
|
57
|
-
expect(await get("test")).toBe(null);
|
|
58
|
-
});
|
|
59
|
-
});
|