Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Express 为什么在使用connect redis进行会话管理时,redis密钥会随着每个请求不断增加?_Express_Redis_Sails.js - Fatal编程技术网

Express 为什么在使用connect redis进行会话管理时,redis密钥会随着每个请求不断增加?

Express 为什么在使用connect redis进行会话管理时,redis密钥会随着每个请求不断增加?,express,redis,sails.js,Express,Redis,Sails.js,我正在使用v3.x管理我的sails应用程序的会话(使用express会话) 当在前端看到时,一切都已设置好,并且工作正常,例如当我重新启动服务器时,会话将得到维护,用户已登录 但是当我检查redis cli时,我发现每个http请求都添加了一个密钥; 这是你想要的行为吗?当系统扩展时,这将对存储造成沉重负担,因为每个请求都会在redis中输入 我不明白这是如何工作的,但是我希望当用户登录时,它将存储会话id和cookie信息,稍后将用于匹配身份验证信息;无论用户在登录后发出多少请求,它都将使用

我正在使用v3.x管理我的sails应用程序的会话(使用express会话)

当在前端看到时,一切都已设置好,并且工作正常,例如当我重新启动服务器时,会话将得到维护,用户已登录

但是当我检查redis cli时,我发现每个http请求都添加了一个密钥; 这是你想要的行为吗?当系统扩展时,这将对存储造成沉重负担,因为每个请求都会在redis中输入

我不明白这是如何工作的,但是我希望当用户登录时,它将存储会话id和cookie信息,稍后将用于匹配身份验证信息;无论用户在登录后发出多少请求,它都将使用相同的会话id和cookie信息对用户进行身份验证。是我错了还是我配置错了

编辑:正如@japrescott所指出的,这不是一个期望的行为。我正在添加更多信息以解决问题

更多观察结果:

我在服务器上多次记录了传入的请求,并观察到了这一点

  • req.session.id
    对所有请求保持不变。即使用户注销,它也保持不变
  • req.session.cookie
    req.cookie
    给出不同的输出。对于每个请求,
    req.session.cookie
    中唯一更改的内容是
    expires
    字段
  • 在客户端,cookie是通过名称
    sails.sid
    为会话正确设置的,该名称似乎工作正常。它的
    过期
    字段在登录时更改,并在整个登录会话中保持不变
  • 当我使用选项
    saveUninitialized:false
    时,redis不会为会话存储任何键值对(甚至不会存储一次,甚至在登录时也不会存储)
该问题在Firefox上无法重现。redis中没有额外的条目。但上述日志显示的行为与firefox的上述4点相同

该问题可在不同操作系统的Chrome上重现

这就是redis中存储的多个条目(仅显示值)的外观

Saved on Request#1(login) : {\"cookie\":{\"originalMaxAge\":2332799999,\"expires\":\"2019-11-22T05:40:39.017Z\",\"httpOnly\":true,\"path\":\"/\"},\"messages\":{\"success\":[],\"error\":[],\"warning\":[]},\"timezone\":\"Asia/Kolkata\",\"passport\":{\"user\":\"5bc9b1559e47b71da3a61d5a\"}}

Saved on Request#2 : "{\"cookie\":{\"originalMaxAge\":2332800000,\"expires\":\"2019-11-22T05:57:06.225Z\",\"httpOnly\":true,\"path\":\"/\"}}"

Saved on Request#3 : "{\"cookie\":{\"originalMaxAge\":2332800000,\"expires\":\"2019-11-22 T05:57:11.186Z\",\"httpOnly\":true,\"path\":\"/\"}}

这是从客户端接收到的完整的请求头

//Request from Chrome
{
  "host": "localhost:3000",
  "connection": "keep-alive",
  "upgrade-insecure-requests": "1",
  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "en-US,en;q=0.9",
  "referer": "http://localhost:3000/user/dashboard",
  "cookie": "_ga=GA1.1.1dsd50.15dsfs8814; _hjid=dsf665e4-sfsdd-4dfd-912c-dsfsdfsdf; _hjIncludedInSample=1; io=J-dsfsd; _gid=GA1.1.1dsfdxx.dfdfxxw064; sails.sid=s%3A0TILcXehUjhcPDykvxwopctILbBJB2pe.ts95dvdwV9nktKeCO6%2BNahMuJ3TrWb5h%2F8%2BccRiTap0",
  "sec-fetch-mode": "navigate",
  "sec-fetch-user": "?1",
  "sec-fetch-site": "same-origin",
  "dnt": "1",
}

//Request from Firefox
{
  "host": "localhost:3000",
  "connection": "keep-alive",
  "upgrade-insecure-requests": "1",
  "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  "accept-language": "en-US,en;q=0.5",
  "accept-encoding": "gzip, deflate",
  "referer": "http://localhost:3000/user",
  "cookie": "_ga=GA1.1.1afdsfdscd42.15dsfdsfs28; _hjid=b6asdfsd6-sdfds0df-4sd2b-8sdfd-0sdfsdb99b4; sails.sid=s%3AAUruqhip_1eTZjWq4FHMAajfdFiYpN5o.xjI8R8x04IX8MGIXHCyT1xw9ErvZdsfd2MJciToUE; _gid=GA1.1.7sdfsd86.1dsfsd3207; _hjIncludedInSample=1",
  "if-none-match": "W/\"4a44-s94WDFGXnSin4kOElQeWwQ\""
}


是的,每个会话中每个用户应该只有一个条目(直到过期),所以一定是出了问题。看起来您没有为用户设置cookie,或者用户没有将cookie发送回您。设置cookie时,请检查您是否设置了正确的域/路径/到期日。谢谢@japrescott我检查了域/路径/到期日,没有问题路径:'/',到期日是将来的日期。当我记录
req.session.id
req.cookies
时,它会为每个请求返回相同的会话id和cookie。这不应该是。打开页面时是否有ajax请求?某些地方没有发送cookies。没有ajax请求。我在“编辑”部分添加了更多的观察结果,我应该如何进行调试以找到根本原因?
Saved on Request#1(login) : {\"cookie\":{\"originalMaxAge\":2332799999,\"expires\":\"2019-11-22T05:40:39.017Z\",\"httpOnly\":true,\"path\":\"/\"},\"messages\":{\"success\":[],\"error\":[],\"warning\":[]},\"timezone\":\"Asia/Kolkata\",\"passport\":{\"user\":\"5bc9b1559e47b71da3a61d5a\"}}

Saved on Request#2 : "{\"cookie\":{\"originalMaxAge\":2332800000,\"expires\":\"2019-11-22T05:57:06.225Z\",\"httpOnly\":true,\"path\":\"/\"}}"

Saved on Request#3 : "{\"cookie\":{\"originalMaxAge\":2332800000,\"expires\":\"2019-11-22 T05:57:11.186Z\",\"httpOnly\":true,\"path\":\"/\"}}

//Request from Chrome
{
  "host": "localhost:3000",
  "connection": "keep-alive",
  "upgrade-insecure-requests": "1",
  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "en-US,en;q=0.9",
  "referer": "http://localhost:3000/user/dashboard",
  "cookie": "_ga=GA1.1.1dsd50.15dsfs8814; _hjid=dsf665e4-sfsdd-4dfd-912c-dsfsdfsdf; _hjIncludedInSample=1; io=J-dsfsd; _gid=GA1.1.1dsfdxx.dfdfxxw064; sails.sid=s%3A0TILcXehUjhcPDykvxwopctILbBJB2pe.ts95dvdwV9nktKeCO6%2BNahMuJ3TrWb5h%2F8%2BccRiTap0",
  "sec-fetch-mode": "navigate",
  "sec-fetch-user": "?1",
  "sec-fetch-site": "same-origin",
  "dnt": "1",
}

//Request from Firefox
{
  "host": "localhost:3000",
  "connection": "keep-alive",
  "upgrade-insecure-requests": "1",
  "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  "accept-language": "en-US,en;q=0.5",
  "accept-encoding": "gzip, deflate",
  "referer": "http://localhost:3000/user",
  "cookie": "_ga=GA1.1.1afdsfdscd42.15dsfdsfs28; _hjid=b6asdfsd6-sdfds0df-4sd2b-8sdfd-0sdfsdb99b4; sails.sid=s%3AAUruqhip_1eTZjWq4FHMAajfdFiYpN5o.xjI8R8x04IX8MGIXHCyT1xw9ErvZdsfd2MJciToUE; _gid=GA1.1.7sdfsd86.1dsfsd3207; _hjIncludedInSample=1",
  "if-none-match": "W/\"4a44-s94WDFGXnSin4kOElQeWwQ\""
}