Reading notes 1

Memcached

Caching services, which can be used to store sessions; Ali cloud series products have

session

Summary:

Is a data structure stored on the server to track user status. This data can be stored in clusters, databases, and files.

How does session work?

  • Each session has a session_ID. You can query the corresponding session according to the session_ID
  • Session_id is usually stored in the cookie of the client. After the session is saved, the server sends the corresponding session_ID in the cookie to the client
  • When the request arrives, the server checks the session_ID stored in the cookie and associates the session_ID with the session on the server to save and modify the data

Session storage has four common options: memory, cookie, cache, and database

  • Memory: The development environment is relatively easy to store memory, the problem is that state cannot be shared (only locally accessible)
  • Cookie: If the session is stored in a cookie, the session is stored in the user’s browser. Each time the user accesses the session, he/she will bring his/her own information. Security is guaranteed as long as best practices are followed. Its disadvantage is to increase the amount of data transmission, the advantage is more convenient
  • Cache: shareable (Memcached)
  • Database: Can be shared

cookie

Summary:

It is a mechanism for the client to save user information and record some user information. It is also a way to realize the Session.

What is a Cookie

  • A Cookie is a small text file that a browser stores on a user’s computer
  • Cookies are in plain text format and do not contain any executable code
  • Cookies consist of key-value pairs separated by semicolons and Spaces
  • Cookies are stored in the browser, but are usually set on the server side
  • Cookie size is limited to about 4KB

How cookies work:

  • First, we assume that there are no cookies under the current domain name
  • Next, the browser sends a request to the server (without the Cookie)
  • The server sets the Cookie and sends it to the browser (optionally)
  • The browser saves the Cookie
  • And then, with every subsequent request, these cookies are sent to the server

Setting cookies in the browser

The subsequent request contains a cookie

Parameter configuration

app.use(async(ctx, next) => {
ctx.cookies.set(
      'cid',//key
      'hello world', //value
      {
        domain: 'localhost'// Write the cookie to the domain path:'/index'MaxAge: 10 * 60 * 1000, // Cookie expires: new Date('2017-02-15'), // Cookies are invalid when there is a maxAge httpOnly:false, // Whether it is only used to fetch in HTTP requests (if yestrue, cannot be modified on the client.false// Whether to override})})Copy the code

See: Cookie practices in the front end, a brief note on the use of cookies and sessions