[英]Possible to have node.js redis fallback?
在Laravel中,您可以執行以下操作
$object = Cache->remember(key, duration, function() {
$result = mysql_fetch_something();// retrieve from MySQL here
return $result;
});
從根本上講,Laravel首先檢查緩存是否存在,如果不存在,它允許您從數據庫中檢索值並將其自動放入緩存中,同時還返回它。 節點中是否有類似的構造? 也就是說,1停止緩存檢查,數據庫故障轉移機制?
在節點中,沒有特殊的命令,但是您可以自己構建。
只需使用redis命令EXISTS檢查密鑰是否在redis中,如果不是,則僅檢查mysql並將其存儲。
您可以做這樣的事情。 在cache.js
var isCacheAvailable = true;
exports.init = function () {
var server = config.get('Cache.server');
var port = config.get('Cache.port');
client = redis.createClient(port,server);
// handle redis connection temporarily going down without app crashing
client.on("error", function (err) {
logger.error("Error connecting to redis server " + server + ":" + port, err);
isCacheAvailable = false;
});
}
exports.isCacheAvailable = function(){
return isCacheAvailable;
}
檢查打算在其中使用緩存的isCacheAvailable()
函數。
if(cache.isCacheAvailable()) {
// use cache to fetch data
} else {
// fallback to mysql db
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.