search

Home  >  Q&A  >  body text

node 使用 redis 来缓存数据,需要每次请求都创建链接吗?

目的:使用redis来缓存网站的固定内容数据在node开启的时候

遇到的问题:

var redis = require('redis'),
    client = redis.createClient();
    
client.hset([hash, hashtest, value],function(err,reply){
               callback(err,reply);
               // client.quit();
});

client.hget([hash,hashtest],function(err,reply){
               callback(err,reply);
               // client.quit(); 
})   

每次在请求完成以后是否需要client.quit 关闭链接,然后在请求的时候再次createClient 链接呢??

PHP中文网PHP中文网2796 days ago784

reply all(3)I'll reply

  • 阿神

    阿神2017-04-24 09:13:02

    There are two configurations related to this in the configuration file:
    maxclients is used to set the maximum supported client connections, which will be rejected if exceeded; this is used to set how many connections your redis client can support at the same time; (Of course, in addition to this value , you also need to look at the limit setting of the Linux system)
    timeout timeout setting, used to set the time after a user connects and no redis request is triggered, the connection will be actively closed from the server side to save system resources;

    # Set the max number of connected clients at the same time. By default
    # this limit is set to 10000 clients, however if the Redis server is not
    # able to configure the process file limit to allow for the specified limit
    # the max number of allowed clients is set to the current file limit
    # minus 32 (as Redis reserves a few file descriptors for internal uses).
    #
    # Once the limit is reached Redis will close all the new connections sending
    # an error 'max number of clients reached'.
    #
    # maxclients 10000
    
    # Set the max number of connected clients at the same time. By default
    # this limit is set to 10000 clients, however if the Redis server is not
    # able to configure the process file limit to allow for the specified limit
    # the max number of allowed clients is set to the current file limit
    # minus 32 (as Redis reserves a few file descriptors for internal uses).
    #
    # Once the limit is reached Redis will close all the new connections sending
    # an error 'max number of clients reached'.
    #
    # maxclients 10000
    # Close the connection after a client is idle for N seconds (0 to disable)
    timeout 0
    

    For redis connection requests, there are generally two scenarios:

    1. The number of servers that trigger user requests is limited (for example, within 100), so you can set the timeout to 0, so that the redis connection request is always valid and will not be disconnected after connecting once; it will only be reconnected after the service is restarted or an error occurs;

    2. A large number of users have concurrent requests. In this case, the timeout can be set within a few minutes (determined according to the business), and the client does not actively close the connection after connecting. When it is found that the connection is closed (closed by the server due to timeout) ) before requesting a connection;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:13:02

    It seems to have its own connection pool

    reply
    0
  • 阿神

    阿神2017-04-24 09:13:02

    It is not recommended to reconnect for every request. It’s just a waste of time spent building links. It is better to establish a link (createClient) in each model and keep it there. Because of the single-threaded processing characteristics of redis, the connection pool itself is of little use to redis.

    reply
    0
  • Cancelreply