可能有 node.js redis 回退?

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 停止缓存检查,数据库故障转移机制?

在 node 中没有专门的命令,但你可以自己构建它。

只需使用 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
}

希望对您有所帮助。