简体   繁体   中英

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

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.

Session lives longer and it's stored in cookies or in ets. Thanks to that you keep eg. data about the successfully logged in user across the requests.

TL;DR:

assign works for each request and it's wipe out after the end of the request cycle.

put_session inserts a value in the session and it's available until the session is cleared / expired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM