Session Extension
Session is a special object created by the server to hold user’s state.
Hertz also provides an implementation of Session, which references Gin’s implementation.
Install
Download and install
Import into your code
Example
Config
Hertz can configure the Session for a series of operations by using middleware.
The Session
interface defines the main methods to configure the Session operation.
The introduction of the interface methods is as follows:
Note: Session Wraps thinly gorilla-session methods.
Method | Function Signatures | Description |
---|---|---|
ID | ID() string |
Used to fetch the Session ID generated by stores, it should not be used for user data. |
Get | Get(key interface{}) interface{} |
Used to get the session value associated to the given key. |
Set | Set(key, val interface{}) |
Used to set the session value associated to the given key. |
Delete | Delete(key interface{}) |
Used to remove the session value associated to the given key. |
Clear | Clear() |
Used to delete all values in the session. |
AddFlash | AddFlash(value interface{}, vars ...string) |
Used to add a flash message to the session. |
Flashes | Flashes(vars ...string) []interface{} |
Used to get a slice of flash messages from the session. |
Options | Options(Options) |
Used to set configuration for a session. |
Save | Save() error |
Used to save all sessions used during the current request. |
NewStore
The sessions
middleware provides NewStore
to store sessions in Cookie or Redis.
Cookie
Function signatures of cookie.NewStore
:
Sample Code:
Redis
Function signatures of redis.NewStore
:
Sample Code:
New
The sessions
middleware provides New
to create a single Session.
Function signatures:
Sample Code:
Many
The sessions
middleware provides Many
to create multiple sessions.
Function signatures:
Sample Code:
Default
The sessions
middleware provides Default
to fetch a single Session.
Function signatures:
Sample Code:
DefaultMany
The sessions
middleware provides DefaultMany
to get the Session based on its name.
Function signatures:
Sample Code:
Distributed Session
Hertz also provides a bizdemo for distributed session solution based on Redis.
Note: This demo is only a simple demonstration of the distributed session, the specific business code needs to be modified by the user combined with the corresponding business logic
The distributed session solution based on redis is to store the sessions of different servers in redis or redis cluster, which aims to solve the problem that the sessions of multiple servers are not synchronized in the case of distributed system.
Display of core code
- Initialize session middleware:
- Store the session after user sign in:
- When the user visits the home page directly, determine whether the corresponding Session exists, and if not, then redirect to the login page (in this example) or restricts the resources that can be browsed or used after login:
- Clear the session after user sign out:
Session middleware encapsulates most of the complex logic, users only need to call the simple interfaces to complete the corresponding business process.
Full Example
As for usage, you may refer to example and hertz_session