哪种缓存方法是Node/Mongo/NginX最快/最轻的?

Mav*_*ick 19 caching nginx varnish redis node.js

我的任务是为一个客户开展项目,该客户有一个他估计每天会收到1-2M点击量的网站.他拥有一个5800万用户的现有数据库,需要按照每个注册的方式为新品牌提供种子.大多数网站的内容都是通过外部API提供的数据提供的,我们的Mongo设置中存储的大部分数据都是配置文件信息和保存的API参数.

NginX将在端口80上,并在端口8000 - 8010上负载均衡到节点群集.

我的问题是如何处理缓存.我来自LAMP背景,所以我习惯于用PHP编写静态HTML文件,并为最小化MySQL负载提供服务,或者为需要更高级别缓存的网站使用Memcached.这个设置对我来说有点陌生.

最小响应时间和CPU负载而言,哪个是最理想的?

1:使用NginX进行页面级缓存

参考:http://andytson.com/blog/2010/04/page-level-caching-with-nginx/

server {
    listen            80;
    servername        mysite.com;

    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $host;

    location / {
        proxy_pass    http://localhost:8080/;
        proxy_cache   anonymous;
    }

    # don't cache admin folder, send all requests through the proxy
    location /admin {
        proxy_pass    http://localhost:8080/;
    }

    # handle static files directly. Set their expiry time to max, so they'll
    # always use the browser cache after first request
    location ~* (css|js|png|jpe?g|gif|ico)$ {
        root          /var/www/${host}/http;
        expires       max;
    }
}
Run Code Online (Sandbox Code Playgroud)


2:Redis作为缓存桶

hash()功能是numbers()此页面上的功能:http://jsperf.com/hashing-strings

function hash(str) {
    var res = 0,
        len = str.length;
    for (var i = 0; i < len; i++) {
        res = res * 31 + str.charCodeAt(i);
    }
    return res;
}

var apiUrl = 'https://www.myexternalapi.com/rest/someparam/someotherparam/?auth=3dfssd6s98d7f09s8df98sdef';
var key    = hash(apiUrl).toString(); // 1.8006908172911553e+136

myRedisClient.set(key,theJSONresponse, function(err) {...});
Run Code Online (Sandbox Code Playgroud)


3:节点写入JSON文件

hash()功能是numbers()此页面上的功能:http://jsperf.com/hashing-strings

function hash(str) {
    var res = 0,
        len = str.length;
    for (var i = 0; i < len; i++) {
        res = res * 31 + str.charCodeAt(i);
    }
    return res;
}

var fs     = require('fs');
var apiUrl = 'https://www.myexternalapi.com/rest/someparam/someotherparam/?auth=3dfssd6s98d7f09s8df98sdef';
var key    = hash(apiUrl).toString(); // 1.8006908172911553e+136

fs.writeFile('/var/www/_cache/' + key + '.json', theJSONresponse, function(err) {...});
Run Code Online (Sandbox Code Playgroud)


4:前面的清漆

我做了一些研究和基准测试,就像这个网站上显示的那样,让我远离这个解决方案,但我仍然愿意考虑它,如果它最有意义:http://todsul.com/nginx-varnish

Ali*_*guy 18

我会做一个组合,并使用Redis缓存具有短TTL的会话用户API调用,并使用Nginx缓存长期RESTless数据和静态资产.我不会写JSON文件,因为我认为文件系统IO将是所列选项中最慢和最耗费CPU的.


Chu*_* Ma 5

  1. nginx 页面级缓存非常适合缓存静态内容。但对于动态内容来说,这就不好了。例如,如果上游内容发生更改,如何使缓存失效?

  2. Redis 非常适合内存数据存储。但我不喜欢用它作为缓存。由于内存量有限,我不得不经常担心内存不足。是的,您可以为 Redis 中的过期密钥设置策略。但这是额外的工作,而且仍然不如我希望它成为缓存提供程序的那么好。

对选项 3 和 4 没有经验。

我很惊讶你没有在这里包含内存缓存作为选项。根据我的经验,它作为缓存提供程序是可靠的。Redis 不具备的一项 Memcache 功能是,它不能保证密钥不会在您指定的到期时间之前过期。这对于数据存储来说是不利的,但它使 memcache 成为缓存的完美候选者:您无需担心耗尽分配给 memcache 的内存。memcache 将删除较少使用的键(即较少使用的缓存),即使这些键尚未达到到期时间。

Nginx 提供了这个内置的memcache 模块。它很坚固。如果你在线谷歌,有很多教程。

我最喜欢这个(见下面的链接)。缓存失效很简单:例如,如果上游更新了页面,只需从上游应用程序服务器中删除memcache键即可。作者声称响应时间增加了 4 倍。相信它足以满足您的用例。

http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/