现在Redis是可选的了

This commit is contained in:
eshengsky
2017-05-09 16:07:44 +08:00
parent 69dceaed9f
commit f265c6df78
3 changed files with 32 additions and 13 deletions

View File

@@ -38,12 +38,12 @@ _不仅仅是博客更是 Demo_适合新人入门学习的完整项目。
* JS库 [jQuery](http://jquery.com/)
* UI库 [Bootstrap 3](http://getbootstrap.com/)
* 持久化 [MongoDB](https://www.mongodb.org/)
* 缓存 [Redis](http://redis.io/)
* 缓存(可选) [Redis](http://redis.io/)
* 日志 [winston](https://github.com/winstonjs/winston/)
## 快速开始
#### 准备条件
安装最新版[Node.js](https://nodejs.org/en/download/)、[bower](http://bower.io/)、[MongoDB](https://www.mongodb.org/downloads/)、[Redis](http://redis.io/download/)。
安装最新版[Node.js](https://nodejs.org/en/download/)、[bower](http://bower.io/)、[MongoDB](https://www.mongodb.org/downloads/)、[Redis](http://redis.io/download/)(可选)
如果使用Windows平台可以去[https://github.com/MSOpenTech/redis/releases](https://github.com/MSOpenTech/redis/releases)下载安装Redis
#### 安装依赖
* 服务端依赖
@@ -56,12 +56,15 @@ $ bower install
```
#### 参数配置
根据实际情况修改 config.json 配置文件DbPath 是MongoDB路径RedisHost Redis所在ipRedisPort Redis端口号。
根据实际情况修改 config.json 配置文件DbPath 是 MongoDB 路径Redis 是缓存配置Redis.Active 表示是否启用Redis缓存默认不开启Redis.Host 表示 Redis 服务器 ip 地址Redis.Port 表示 Redis 端口号。
```JSON
{
"DbPath": "mongodb://localhost/iBlog2",
"RedisHost": "127.0.0.1",
"RedisPort": 6379
"Redis": {
"Active": false,
"Host": "127.0.0.1",
"Port": 6379
}
}
```
后台管理员账号信息在 config/account.json 中配置,默认管理员账号 admin ,密码 123456 ,密码需[md5加密](http://md5jiami.51240.com/)存储。

View File

@@ -1,5 +1,8 @@
{
"DbPath": "mongodb://localhost/iBlog2",
"RedisHost": "127.0.0.1",
"RedisPort": 6379
"Redis": {
"Active": false,
"Host": "127.0.0.1",
"Port": 6379
}
}

View File

@@ -1,11 +1,15 @@
var redis = require('redis');
var config = require('../config');
// use custom redis url or localhost
var client = redis.createClient(config.RedisPort || 6379, config.RedisHost || 'localhost');
client.on('error', function (err) {
console.error('Redis连接错误: ' + err);
process.exit(1);
});
var redisActive = config.Redis.Active;
if (redisActive) {
// use custom redis url or localhost
var client = redis.createClient(config.Redis.Port || 6379, config.Redis.Host || 'localhost');
client.on('error', function (err) {
console.error('Redis连接错误: ' + err);
process.exit(1);
});
}
/**
* 设置缓存
@@ -15,6 +19,9 @@ client.on('error', function (err) {
* @param callback 回调函数
*/
exports.setItem = function (key, value, expired, callback) {
if (!redisActive) {
return callback(null);
}
client.set(key, JSON.stringify(value), function (err) {
if (err) {
return callback(err);
@@ -32,6 +39,9 @@ exports.setItem = function (key, value, expired, callback) {
* @param callback 回调函数
*/
exports.getItem = function (key, callback) {
if (!redisActive) {
return callback(null, null);
}
client.get(key, function (err, reply) {
if (err) {
return callback(err);
@@ -46,6 +56,9 @@ exports.getItem = function (key, callback) {
* @param callback 回调函数
*/
exports.removeItem = function (key, callback) {
if (!redisActive) {
return callback(null);
}
client.del(key, function (err) {
if (err) {
return callback(err);