Skip to main content
Version: 2.8.x(Latest)

The GoFrame framework provides comprehensive Session management capabilities, implemented by the gsession component. Since the Session mechanism is most commonly used in HTTP services, the subsequent chapters will focus on the use of Session in the context of HTTP services.

Introduction

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/gsession

You can obtain a Session object at any time through ghttp.Request since both Cookie and Session are related to request sessions and thus are part of the Request member objects and are publicly accessible. The default expiration time for Session in the GoFrame framework is 24 hours.

The SessionId is transmitted by default through Cookie, but it also supports transmission via Header from the client. The identification name of SessionId can be modified through the SetSessionIdName of ghttp.Server. The operations of Session are concurrency-safe, which is why the framework does not operate data directly in a map form in its Session design. During the HTTP request flow, we can obtain the Session object through the ghttp.Request object and perform the corresponding data operations.

In addition, the SessionId in ghttp.Server is generated using the client's RemoteAddr + Header request information via the guid module, ensuring randomness and uniqueness: https://github.com/gogf/gf/blob/master/net/ghttp/ghttp_request.go

gsession Module

The management functionality of Session is implemented by the independent gsession module and is perfectly integrated into ghttp.Server. Since this module is decoupled and independent, it can be applied to more different scenarios, such as TCP communication, gRPC API services, etc. The gsession module has three important objects/APIs:

  1. gsession.Manager: Manages Session objects, Storage persistence storage objects, and expiration time control.
  2. gsession.Session: A single Session management object, used for CRUD operations on Session parameters and other data management operations.
  3. gsession.Storage: This is an API definition used for the persistent storage of Session objects, data writing/reading, and survival updates. Developers can implement customized persistent storage features based on this API. For the API definition, see: https://github.com/gogf/gf/blob/master/os/gsession/gsession_storage.go

Storage Implementations

gsession implements and provides developers with four common Session storage implementations:

StorageDistributed SupportPersistence SupportMemory UsageExecution EfficiencyBrief Introduction
StorageFileNoYesMediumMediumBased on file storage (default). A more efficient persistent storage method under single-node deployment: Session - File
StorageMemoryNoNoHighHighBased on pure memory storage. Single-node deployment, highest performance, but cannot be persisted, and is lost on restart: Session - Memory
StorageRedisYesYesMediumMediumBased on Redis storage (Key-Value). Remote Redis node stores Session data, supporting multi-node deployment: Session - Redis-KeyValue
StorageRedisHashTableYesYesLowLowBased on Redis storage (HashTable). Remote Redis node stores Session data, supporting multi-node deployment: Session - Redis-HashTable

Each method has its advantages and disadvantages. For detailed introductions, please refer to the corresponding sections.

Initialization of Session

Taking a common HTTP request as an example, the Session object in ghttp.Request adopts a "Lazy Initialization" design approach. By default, there is a Session property object in the Request, but it is not initialized (an empty object). The initialization is only executed when methods of the Session property object are used. This design ensures that the execution performance of requests not utilizing the Session feature is maintained, while also ensuring the ease of use of the component.

Destruction/Logout of Session

When a user's Session is no longer needed, for instance, when a user logs out, the session needs to be hard-deleted from storage. This can be done by calling the RemoveAll method.

Documentation

📄️ Session - Redis-KeyValue

Using Redis in the GoFrame framework for Session KeyValue storage to solve the issue of Session sharing across multiple nodes. By using the StorageRedis object to implement Redis storage, it improves execution efficiency, suitable for scenarios with small amounts of user Session data, and provides specific examples and explanations. In the example, the Session expiration time is set to 1 minute, demonstrating methods of setting, getting, deleting Sessions, and the recovery function of Session data in Redis.

📄️ Session - Storage Interface

Developing Session-Storage interfaces using the gsession component in the GoFrame framework. The built-in Storage implementation within the component can meet the needs of most business scenarios. Developers can also customize session storage according to specific cases. The article describes in detail the definition of the Storage interface and its invocation timing. To improve session performance, it is recommended to use the gmap container type. This guide will help developers better implement and optimize storage interfaces.