forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
66 lines (57 loc) · 2.48 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
61
62
63
64
65
66
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// Note: @pulumi/cloud is a preview package demonstrating how to create cross-cloud Pulumi
// components. If you are targeting a specific cloud like AWS, Azure, or GCP, we recommend you use
// platform-specific packages like @pulumi/aws, @pulumi/azure or @pulumi/gcp. These packages give
// you full access to the breadth of the platform's capabilities and comes with many abstractions to
// make developing against that platform easier.
import * as cloud from "@pulumi/cloud";
import * as pulumi from "@pulumi/pulumi";
import * as config from "./config";
// A simple cache abstraction that wraps Redis.
export class Cache {
private readonly redis: cloud.Service;
private readonly endpoint: pulumi.Output<cloud.Endpoint>;
constructor(name: string, memory: number = 128) {
const pw = config.redisPassword;
this.redis = new cloud.Service(name, {
containers: {
redis: {
image: "redis:alpine",
memory: memory,
ports: [{ port: 6379, external: true }],
command: ["redis-server", "--requirepass", pw],
},
},
});
this.endpoint = this.redis.endpoints.redis[6379];
}
public get(key: string): Promise<string> {
const ep = this.endpoint.get();
console.log(`Getting key '${key}' on Redis@${ep.hostname}:${ep.port}`);
const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
return new Promise<string>((resolve, reject) => {
client.get(key, (err: any, v: any) => {
if (err) {
reject(err);
} else {
resolve(v);
}
});
});
}
public set(key: string, value: string): Promise<void> {
const ep = this.endpoint.get();
console.log(`Setting key '${key}' to '${value}' on Redis@${ep.hostname}:${ep.port}`);
const client = require("redis").createClient(ep.port, ep.hostname, { password: config.redisPassword });
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();
}
});
});
}
}