[英]What is the difference between assign and put_session in Plug.Conn of the Phoenix Framework?
The documentation ( https://hexdocs.pm/plug/Plug.Conn.html ) names two functions that allow for storing a key-value pair in a conn
文档( https://hexdocs.pm/plug/Plug.Conn.html )命名两个函数,允许在
conn
存储键值对
assign(conn, key, value)
Assigns a value to a key in the connection
为连接中的键分配值
put_session(conn, key, value)
Puts the specified value in the session for the given key
将指定值放在给定键的会话中
What is the difference between these two functions? 这两个功能有什么区别?
Basically assign
works only for particular connection - Plug.Conn structure - which means that this value would be gone with the end of the request - as conn will also dies - at the end of the request / response cycle. 基本上,
assign
只适用于特定的连接 - Plug.Conn结构 - 这意味着在请求/响应周期结束时,该值将随着请求的结束而消失 - 因为conn也会死亡。
Session lives longer and it's stored in cookies or in ets. 会话寿命更长,并存储在cookie或ets中。 Thanks to that you keep eg.
多亏了你保持eg。 data about the successfully logged in user across the requests.
有关跨请求成功登录用户的数据。
TL;DR: TL; DR:
assign
works for each request and it's wipe out after the end of the request cycle. 为每个请求
assign
工作,并在请求周期结束后消除。
put_session
inserts a value in the session and it's available until the session is cleared / expired. put_session
在会话中插入一个值,直到会话清除/过期才可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.