|
@@ -1,14 +1,41 @@
|
|
import redis from 'redis';
|
|
import redis from 'redis';
|
|
|
|
+// 内存存储
|
|
|
|
+import nodeCache from 'node-cache';
|
|
import util from 'util';
|
|
import util from 'util';
|
|
import consola from 'consola/dist/consola';
|
|
import consola from 'consola/dist/consola';
|
|
import config from '../config';
|
|
import config from '../config';
|
|
|
|
|
|
|
|
+// 是否使用redis
|
|
|
|
+let isUseRedis: boolean = true
|
|
|
|
+// 内存存储实例化
|
|
|
|
+const cache = new nodeCache()
|
|
|
|
+
|
|
// 创建redis连接
|
|
// 创建redis连接
|
|
const client = redis.createClient({
|
|
const client = redis.createClient({
|
|
host: config.redis.host,
|
|
host: config.redis.host,
|
|
port: config.redis.port,
|
|
port: config.redis.port,
|
|
password: config.redis.password,
|
|
password: config.redis.password,
|
|
- auth_pass: config.redis.password
|
|
|
|
|
|
+ auth_pass: config.redis.password,
|
|
|
|
+ retry_strategy: function(options) {
|
|
|
|
+ if (options.error && options.error.code === "ECONNREFUSED") {
|
|
|
|
+ // redis连接失败时使用内存存储
|
|
|
|
+ isUseRedis = false
|
|
|
|
+ // End reconnecting on a specific error and flush all commands with
|
|
|
|
+ // a individual error
|
|
|
|
+ return new Error("The server refused the connection");
|
|
|
|
+ }
|
|
|
|
+ if (options.total_retry_time > 1000 * 60 * 60) {
|
|
|
|
+ // End reconnecting after a specific timeout and flush all commands
|
|
|
|
+ // with a individual error
|
|
|
|
+ return new Error("Retry time exhausted");
|
|
|
|
+ }
|
|
|
|
+ if (options.attempt > 10) {
|
|
|
|
+ // End reconnecting with built in error
|
|
|
|
+ return undefined;
|
|
|
|
+ }
|
|
|
|
+ // reconnect after
|
|
|
|
+ return Math.min(options.attempt * 100, 3000);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
client.on('error', err => {
|
|
client.on('error', err => {
|
|
@@ -19,6 +46,7 @@ client.on('ready', () => {
|
|
});
|
|
});
|
|
client.on('connect', () => {
|
|
client.on('connect', () => {
|
|
consola.success('redis is connecting');
|
|
consola.success('redis is connecting');
|
|
|
|
+ isUseRedis = true
|
|
});
|
|
});
|
|
client.on('reconnecting', () => {
|
|
client.on('reconnecting', () => {
|
|
consola.success('redis is reconnecting...');
|
|
consola.success('redis is reconnecting...');
|
|
@@ -26,9 +54,6 @@ client.on('reconnecting', () => {
|
|
client.on('end', () => {
|
|
client.on('end', () => {
|
|
consola.success('redis is closed');
|
|
consola.success('redis is closed');
|
|
});
|
|
});
|
|
-client.on('end', () => {
|
|
|
|
- consola.success('redis is closed');
|
|
|
|
-});
|
|
|
|
|
|
|
|
// redis查询和设置await/async化
|
|
// redis查询和设置await/async化
|
|
const promisify = util.promisify
|
|
const promisify = util.promisify
|
|
@@ -36,14 +61,41 @@ const getClient = promisify(client.get).bind(client);
|
|
const setClient = promisify(client.set).bind(client);
|
|
const setClient = promisify(client.set).bind(client);
|
|
const setexClient = promisify(client.setex).bind(client);
|
|
const setexClient = promisify(client.setex).bind(client);
|
|
|
|
|
|
|
|
+// 内存存储promise化,为了和redis使用方法保持一致
|
|
|
|
+const getCache = (key: string): Promise<string> => {
|
|
|
|
+ return new Promise(resolve => {
|
|
|
|
+ resolve(cache.get(key))
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+const setCache = (key: string, value: string, ttl: number = 0): Promise<boolean> => {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ const success = cache.set(key, value, ttl)
|
|
|
|
+ if (success) {
|
|
|
|
+ resolve(success)
|
|
|
|
+ } else {
|
|
|
|
+ reject(success)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+const setexCache = (key: string, ttl: number, value: string, ) => setCache(key, value, ttl)
|
|
|
|
+
|
|
// 初始化测试
|
|
// 初始化测试
|
|
-client.setex('test', 30, '111111');
|
|
|
|
|
|
+// client.setex('test', 30, '111111');
|
|
// send_sms_verify_code_sms_
|
|
// send_sms_verify_code_sms_
|
|
// 18339012129
|
|
// 18339012129
|
|
|
|
|
|
export = () => {
|
|
export = () => {
|
|
return async (ctx, next) => {
|
|
return async (ctx, next) => {
|
|
- ctx.$redis = { set: setClient, get: getClient, setex: setexClient };
|
|
|
|
|
|
+ console.log(isUseRedis);
|
|
|
|
+ if (isUseRedis) { // 如果redis连接失败,采用内存
|
|
|
|
+ ctx.$redis = { set: setClient, get: getClient, setex: setexClient };
|
|
|
|
+ } else {
|
|
|
|
+ ctx.$redis = {
|
|
|
|
+ set: setCache,
|
|
|
|
+ get: getCache,
|
|
|
|
+ setex: setexCache
|
|
|
|
+ }
|
|
|
|
+ }
|
|
await next();
|
|
await next();
|
|
};
|
|
};
|
|
};
|
|
};
|