github.com/gogf/gf/v2@v2.7.4/os/gsession/gsession_storage.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gsession
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/container/gmap"
    14  )
    15  
    16  // Storage is the interface definition for session storage.
    17  type Storage interface {
    18  	// New creates a custom session id.
    19  	// This function can be used for custom session creation.
    20  	New(ctx context.Context, ttl time.Duration) (sessionId string, err error)
    21  
    22  	// Get retrieves and returns certain session value with given key.
    23  	// It returns nil if the key does not exist in the session.
    24  	Get(ctx context.Context, sessionId string, key string) (value interface{}, err error)
    25  
    26  	// GetSize retrieves and returns the size of key-value pairs from storage.
    27  	GetSize(ctx context.Context, sessionId string) (size int, err error)
    28  
    29  	// Data retrieves all key-value pairs as map from storage.
    30  	Data(ctx context.Context, sessionId string) (sessionData map[string]interface{}, err error)
    31  
    32  	// Set sets one key-value session pair to the storage.
    33  	// The parameter `ttl` specifies the TTL for the session id.
    34  	Set(ctx context.Context, sessionId string, key string, value interface{}, ttl time.Duration) error
    35  
    36  	// SetMap batch sets key-value session pairs as map to the storage.
    37  	// The parameter `ttl` specifies the TTL for the session id.
    38  	SetMap(ctx context.Context, sessionId string, mapData map[string]interface{}, ttl time.Duration) error
    39  
    40  	// Remove deletes key-value pair from specified session from storage.
    41  	Remove(ctx context.Context, sessionId string, key string) error
    42  
    43  	// RemoveAll deletes session from storage.
    44  	RemoveAll(ctx context.Context, sessionId string) error
    45  
    46  	// GetSession returns the session data as `*gmap.StrAnyMap` for given session from storage.
    47  	//
    48  	// The parameter `ttl` specifies the TTL for this session.
    49  	// The parameter `data` is the current old session data stored in memory,
    50  	// and for some storage it might be nil if memory storage is disabled.
    51  	//
    52  	// This function is called ever when session starts.
    53  	// It returns nil if the session does not exist or its TTL is expired.
    54  	GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error)
    55  
    56  	// SetSession updates the data for specified session id.
    57  	// This function is called ever after session, which is changed dirty, is closed.
    58  	// This copy all session data map from memory to storage.
    59  	SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error
    60  
    61  	// UpdateTTL updates the TTL for specified session id.
    62  	// This function is called ever after session, which is not dirty, is closed.
    63  	UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error
    64  }