Last active
August 29, 2015 14:14
-
-
Save mangreen/3dc0a09be9e111e8a88d to your computer and use it in GitHub Desktop.
資料庫緩存nodejs+redis+db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
思路: | |
1.redis緩存我們業務需要的數據(有生命週期),以減輕數據庫的壓力,當查詢數據時,先走redis,redis沒有數據走數據庫,同時把得到的數據存入redis; | |
2.當數據發生delete或者update操作時,刪掉redis中的數據,同時更改數據庫。 | |
注意的地方:第一步中,從數據庫中取數據存入redis,redis存儲失敗成功不重要。第二步中,必須等redis中清楚數據成功以後才能更改數據庫,不然下次取數據就會有問題。 | |
//存儲緩存 | |
YY.get_cache = function(self, tablename, where, row, args) { | |
if (tablename === 'card' ) { | |
//卡組查詢 | |
if (where.indexOf('card_group ' ) !== -1 ) { | |
var groupid = where.match(new RegExp('card_group=([^ ])'))[1]; | |
var key = 'group_id = '+groupid; | |
var redis = pomelo.app.get('redis '); | |
redis.get(key, function(err, mres) { | |
if (err) { | |
args(err); | |
} else { | |
//卡組信息緩存存在 | |
if (mres) { | |
//console.log( 'card_group from cache' ); | |
res = JSON.parse(mres); | |
YY.get_multi_card(self, res, args); | |
} else { //卡組緩存不存在 | |
//console.log( 'card_group no cache' ); | |
var tmp_reg = where.match(new RegExp('role_id=([^ ])')); | |
if (tmp_reg) { | |
var roleid = tmp_reg[1]; | |
var sql = YY.selectSQL("card_group , 'group_id=' + groupid + AND role_id = "+roleid, " *"); | |
self.query(sql, function(err, res) { | |
YY.get_multi_card(self, res[0], args); | |
redis.set(key, JSON.stringify(res[0]), 'NX', 'EX', 43200, function(err, mres) { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
}); | |
} else { | |
console.error('select card_group, role_id is required ' ); | |
} | |
} | |
} | |
}); | |
} else if (where.indexOf('card_id' ) !== -1 ) { //通過card_id來查詢 | |
var card_id_arr = where.match(/card_id=(\d+)/g).map(function(n) { | |
return n.match(/\d+/)[0] | |
}); | |
var redis = pomelo.app.get('redis '); | |
//多張卡片的查詢 | |
if (card_id_arr.length > 1) { | |
YY.get_multi_card(self, card_id_arr, args); | |
} else { //單張卡片的查詢 | |
var key = 'card_id =' + card_id_arr[0]; | |
redis.get(key, function(err, res) { | |
if (err) { | |
args(err); | |
} else { //單張卡片沒有緩存 | |
if (res === null) { | |
//console.log( 'card no cache' ); | |
var sql = YY.selectSQL('card', where, row); | |
self.query(sql, function(err, res) { | |
if (err) { | |
args(err); | |
} else { | |
args(null, res); | |
if (res.length !== 0) { | |
redis.set(key, JSON.stringify(res), 'NX', 'EX', 43200, function(err, res) { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
} | |
} | |
}); | |
} else { | |
//console.log( 'card from cache' ); | |
args(null, JSON.parse(res)); | |
} | |
} | |
}); | |
} | |
} else if (where.indexOf('role_id' ) !== -1 ) { //通過role_id來查詢 | |
var sql = YY.selectSQL('card', where, row); self.query(sql, function(err, res) { | |
if (err) { | |
args(err); | |
} else { | |
args(null, res); | |
if (res.length !== 0) { | |
var redis = pomelo.app.get('redis '); | |
res.forEach(function(card) { | |
//判斷key是否存在,如果不存在,就緩存 | |
var key = 'card_id =' + card.card_id; | |
redis.exists(key, function(err, res) { | |
if (err) { | |
console.error(err); | |
} else { | |
if (res === 0) { | |
redis.set(key, JSON.stringify([card]), 'NX', 'EX', 43200, function(err, res) { | |
if (err) { | |
console.error(err); | |
} | |
}); | |
} | |
} | |
}); | |
}); | |
} | |
} | |
}); | |
} else { | |
console.log('no such condition is found ' ); | |
} | |
} | |
}; | |
//刪除緩存 | |
YY.del_cache = function(self, tablename, data, where, args, memc) { | |
var async = require('async' ); | |
var redis = pomelo.app.get('redis' ); | |
/* 刪除key */ | |
function removekey( keys ) { | |
var fun_arr = []; | |
keys.forEach( function( key, idx ) { | |
if( idx === 0 ) { | |
fun_arr.push( | |
function( cb ) { | |
redis.exists( key, cb ); | |
} | |
); | |
} else { | |
fun_arr.push( | |
function( res, cb ) { | |
redis.exists( key, cb ); | |
} | |
); | |
} | |
fun_arr.push( | |
function( res, cb ) { | |
//沒有緩存 | |
if( res === 0 ) { | |
cb( null, null ); | |
} else { | |
redis.del( key, function( err, res ) { | |
//刪除成功 | |
if( res ) { | |
cb( null, null ); | |
} else { //刪除失敗 | |
cb( 'mysql cahce del failed' ); | |
} | |
}); | |
} | |
} | |
); | |
}); | |
fun_arr.push( | |
function( res, cb ) { | |
if( memc.sql == 'update' ) { | |
//console.log( 'update cache success' ); | |
self[ memc.sql ]( tablename, data, where, args, true ); | |
} else if( memc.sql == 'delete' ) { | |
//console.log( 'delete cache success' ); | |
self[ memc.sql ]( tablename, where, args, true ); | |
} | |
} | |
); | |
async.waterfall( fun_arr, function( err ) { | |
args( err ); | |
}); | |
} | |
//卡組邏輯 | |
if( tablename == 'card_group' ) { | |
var groupid = where.match( new RegExp('group_id=([^ ])') )[ 1 ]; | |
var keys = [ 'group_id=' + groupid ]; | |
removekey( keys ); | |
} else if( tablename == 'card' ) { | |
var card_id_arr = where.match(/card_id=(\d+)/g).map(function(n){return n.match(/\d+/)[0]}); | |
var keys = []; | |
for( var i = 0; i < card_id_arr.length; i++ ) { | |
keys.push( 'card_id=' + card_id_arr[ i ] ); | |
} | |
removekey( keys ); | |
} else{ | |
console.error( 'mysql delete cache error, no such options' ); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment