forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisQueryResultCache.ts
More file actions
172 lines (147 loc) · 5.25 KB
/
RedisQueryResultCache.ts
File metadata and controls
172 lines (147 loc) · 5.25 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {QueryResultCache} from "./QueryResultCache";
import {QueryResultCacheOptions} from "./QueryResultCacheOptions";
import {PlatformTools} from "../platform/PlatformTools";
import {Connection} from "../connection/Connection";
import {QueryRunner} from "../query-runner/QueryRunner";
/**
* Caches query result into Redis database.
*/
export class RedisQueryResultCache implements QueryResultCache {
// -------------------------------------------------------------------------
// Protected Properties
// -------------------------------------------------------------------------
/**
* Redis module instance loaded dynamically.
*/
protected redis: any;
/**
* Connected redis client.
*/
protected client: any;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
this.redis = this.loadRedis();
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates a connection with given cache provider.
*/
async connect(): Promise<void> {
const cacheOptions: any = this.connection.options.cache;
if (cacheOptions && cacheOptions.options) {
this.client = this.redis.createClient(cacheOptions.options);
} else {
this.client = this.redis.createClient();
}
}
/**
* Creates a connection with given cache provider.
*/
async disconnect(): Promise<void> {
return new Promise<void>((ok, fail) => {
this.client.quit((err: any, result: any) => {
if (err) return fail(err);
ok();
this.client = undefined;
});
});
}
/**
* Creates table for storing cache if it does not exist yet.
*/
async synchronize(queryRunner: QueryRunner): Promise<void> {
}
/**
* Caches given query result.
* Returns cache result if found.
* Returns undefined if result is not cached.
*/
getFromCache(options: QueryResultCacheOptions, queryRunner?: QueryRunner): Promise<QueryResultCacheOptions|undefined> {
return new Promise((ok, fail) => {
if (options.identifier) {
this.client.get(options.identifier, (err: any, result: any) => {
if (err) return fail(err);
ok(JSON.parse(result));
});
} else if (options.query) {
this.client.get(options.query, (err: any, result: any) => {
if (err) return fail(err);
ok(JSON.parse(result));
});
} else {
ok(undefined);
}
});
}
/**
* Checks if cache is expired or not.
*/
isExpired(savedCache: QueryResultCacheOptions): boolean {
return (savedCache.time! + savedCache.duration) < new Date().getTime();
}
/**
* Stores given query result in the cache.
*/
async storeInCache(options: QueryResultCacheOptions, savedCache: QueryResultCacheOptions, queryRunner?: QueryRunner): Promise<void> {
return new Promise<void>((ok, fail) => {
if (options.identifier) {
this.client.set(options.identifier, JSON.stringify(options), (err: any, result: any) => {
if (err) return fail(err);
ok();
});
} else if (options.query) {
this.client.set(options.query, JSON.stringify(options), (err: any, result: any) => {
if (err) return fail(err);
ok();
});
}
});
}
/**
* Clears everything stored in the cache.
*/
async clear(queryRunner?: QueryRunner): Promise<void> {
return new Promise<void>((ok, fail) => {
this.client.flushdb((err: any, result: any) => {
if (err) return fail(err);
ok();
});
});
}
/**
* Removes all cached results by given identifiers from cache.
*/
async remove(identifiers: string[], queryRunner?: QueryRunner): Promise<void> {
await Promise.all(identifiers.map(identifier => {
return this.deleteKey(identifier);
}));
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Removes a single key from redis database.
*/
protected deleteKey(key: string): Promise<void> {
return new Promise<void>((ok, fail) => {
this.client.del(key, (err: any, result: any) => {
if (err) return fail(err);
ok();
});
});
}
/**
* Loads redis dependency.
*/
protected loadRedis(): any {
try {
return PlatformTools.load("redis");
} catch (e) {
throw new Error(`Cannot use cache because redis is not installed. Please run "npm i redis --save".`);
}
}
}