forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
60 lines (52 loc) · 2.15 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
import * as cloud from "@pulumi/cloud";
import * as config from "./config";
// A simple cache abstraction that wraps Redis.
export class Cache {
public readonly get: (key: string) => Promise<string>;
public readonly set: (key: string, value: string) => Promise<void>;
private readonly redis: cloud.Service;
constructor(name: string, memory: number = 128) {
let pw = config.redisPassword;
this.redis = new cloud.Service(name, {
containers: {
redis: {
image: "redis:alpine",
memory: memory,
ports: [{ port: 6379 }],
command: ["redis-server", "--requirepass", pw],
},
},
});
let endpoint = this.redis.endpoints.apply(endpoints => endpoints.redis[6379]);
this.get = async (key: string) => {
let ep = (await endpoint).get();
console.log(`Getting key '${key}' on Redis@${ep.hostname}:${ep.port}`);
let client = require("redis").createClient(ep.port, ep.hostname, { password: pw });
return new Promise<string>((resolve, reject) => {
client.get(key, (err: any, v: any) => {
if (err) {
reject(err);
} else {
resolve(v);
}
});
});
};
this.set = async (key: string, value: string) => {
let ep = (await endpoint).get();
console.log(`Setting key '${key}' to '${value}' on Redis@${ep.hostname}:${ep.port}`);
let client = require("redis").createClient(ep.port, ep.hostname, { password: pw });
return new Promise<void>((resolve, reject) => {
client.set(key, value, (err: any, v: any) => {
if (err) {
reject(err);
} else {
console.log("Set succeeed: " + JSON.stringify(v))
resolve();
}
});
});
};
}
}