From 5a5d9122bf0bbd6bf09d3be6f6f99b8b452ce255 Mon Sep 17 00:00:00 2001 From: Sky Date: Thu, 21 Apr 2016 23:56:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E7=82=B9=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E9=85=8D=E5=90=88Wiki?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/category.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/proxy/category.js b/proxy/category.js index 3cb90a6..76417c8 100644 --- a/proxy/category.js +++ b/proxy/category.js @@ -34,16 +34,22 @@ exports.getAll = function (isAll, cached, callback) { isAll = true; cached = true; } + //缓存的key名称 var cache_key = isAll ? 'categories_all' : 'categories'; if (cached) { + //尝试读取缓存 redisClient.getItem(cache_key, function (err, categories) { + //读取缓存出错 if (err) { return callback(err); } + //缓存中有数据 if (categories) { return callback(null, categories); } + //缓存中没有数据,则从数据库中读取 categoryModel.find(function (err, categories) { + //读取数据库出错 if (err) { return callback(err); } @@ -51,7 +57,9 @@ exports.getAll = function (isAll, cached, callback) { categories.unshift(cateAll); categories.push(cateOther); } + //从数据库中读到数据 if (categories) { + //将数据塞入缓存 redisClient.setItem(cache_key, categories, redisClient.defaultExpired, function (err) { if (err) { return callback(err); @@ -190,4 +198,4 @@ exports.save = function (array, callback) { } }); }); -}; \ No newline at end of file +};