Plug v1.3.5 Plug.Session.ETS

Stores the session in an in-memory ETS table.

This store does not create the ETS table; it expects that an existing named table with public properties is passed as an argument.

We don’t recommend using this store in production as every session will be stored in ETS and never cleaned until you create a task responsible for cleaning up old entries.

Also, since the store is in-memory, it means sessions are not shared between servers. If you deploy to more than one machine, using this store is again not recommended.

This store, however, can be used as an example for creating custom storages, based on Redis, Memcached, or a database itself.

Options

  • :table - ETS table name (required)

For more information on ETS tables, visit the Erlang documentation at http://www.erlang.org/doc/man/ets.html.

Storage

The data is stored in ETS in the following format:

{sid :: String.t, data :: map, timestamp :: :erlang.timestamp}

The timestamp is updated whenever there is a read or write to the table and it may be used to detect if a session is still active.

Examples

# Create an ETS table when the application starts
:ets.new(:session, [:named_table, :public, read_concurrency: true])

# Use the session plug with the table name
plug Plug.Session, store: :ets, key: "sid", table: :session

Summary

Functions

Removes the session associated with given session id from the store

Parses the given cookie

Initializes the store

Stores the session associated with given session id

Functions

delete(conn, sid, table)

Removes the session associated with given session id from the store.

Callback implementation for Plug.Session.Store.delete/3.

get(conn, sid, table)

Parses the given cookie.

Returns a session id and the session contents. The session id is any value that can be used to identify the session by the store.

The session id may be nil in case the cookie does not identify any value in the store. The session contents must be a map.

Callback implementation for Plug.Session.Store.get/3.

init(opts)

Initializes the store.

The options returned from this function will be given to get/3, put/4 and delete/3.

Callback implementation for Plug.Session.Store.init/1.

put(conn, sid, data, table)

Stores the session associated with given session id.

If nil is given as id, a new session id should be generated and returned.

Callback implementation for Plug.Session.Store.put/4.