test: redis with quit and error cases

This commit is contained in:
DIYgod
2019-06-13 15:23:28 +08:00
parent 76add4785a
commit 7c5aaa3c14
4 changed files with 53 additions and 11 deletions

View File

@@ -35,7 +35,7 @@ module.exports = function(app, options = {}) {
app.context.cache = {
get: async (key) => {
if (key) {
if (key && available) {
let value = await redisClient.get(key);
if (value) {
await redisClient.expire(key, config.cache.contentExpire);
@@ -45,6 +45,9 @@ module.exports = function(app, options = {}) {
}
},
set: async function(key, value, maxAge = config.cache.contentExpire) {
if (!available) {
return;
}
if (await redisClient.exists(key)) {
logger.warn(`repeated key: ${key}, ${value}`);
return;
@@ -59,9 +62,10 @@ module.exports = function(app, options = {}) {
await redisClient.setex(key, maxAge, value);
}
},
client: redisClient,
};
globalCache.get = async (key) => {
if (key) {
if (key && available) {
const value = await redisClient.get(key);
return value;
}
@@ -81,7 +85,7 @@ module.exports = function(app, options = {}) {
app.context.cache = {
get: (key) => {
if (key) {
if (key && available) {
let value = routeCache.get(key);
if (value) {
value = value + '';
@@ -96,13 +100,14 @@ module.exports = function(app, options = {}) {
if (typeof value === 'object') {
value = JSON.stringify(value);
}
if (key) {
if (key && available) {
return routeCache.set(key, value, maxAge * 1000);
}
},
client: [pageCache, routeCache],
};
globalCache.get = (key) => {
if (key) {
if (key && available) {
return pageCache.get(key);
}
};