github.com/wangyougui/gf/v2@v2.6.5/os/gsession/gsession_storage_memory.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/wangyougui/gf.
     6  
     7  package gsession
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gmap"
    14  	"github.com/wangyougui/gf/v2/container/gvar"
    15  	"github.com/wangyougui/gf/v2/os/gcache"
    16  )
    17  
    18  // StorageMemory implements the Session Storage interface with memory.
    19  type StorageMemory struct {
    20  	StorageBase
    21  	// cache is the memory data cache for session TTL,
    22  	// which is available only if the Storage does not store any session data in synchronizing.
    23  	// Please refer to the implements of StorageFile, StorageMemory and StorageRedis.
    24  	//
    25  	// Its value is type of `*gmap.StrAnyMap`.
    26  	cache *gcache.Cache
    27  }
    28  
    29  // NewStorageMemory creates and returns a file storage object for session.
    30  func NewStorageMemory() *StorageMemory {
    31  	return &StorageMemory{
    32  		cache: gcache.New(),
    33  	}
    34  }
    35  
    36  // RemoveAll deletes session from storage.
    37  func (s *StorageMemory) RemoveAll(ctx context.Context, sessionId string) error {
    38  	_, err := s.cache.Remove(ctx, sessionId)
    39  	return err
    40  }
    41  
    42  // GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
    43  //
    44  // The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded.
    45  // The parameter `data` is the current old session data stored in memory,
    46  // and for some storage it might be nil if memory storage is disabled.
    47  //
    48  // This function is called ever when session starts.
    49  func (s *StorageMemory) GetSession(ctx context.Context, sessionId string, ttl time.Duration) (*gmap.StrAnyMap, error) {
    50  	// Retrieve memory session data from manager.
    51  	var (
    52  		v   *gvar.Var
    53  		err error
    54  	)
    55  	v, err = s.cache.Get(ctx, sessionId)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	if v != nil {
    60  		return v.Val().(*gmap.StrAnyMap), nil
    61  	}
    62  	return gmap.NewStrAnyMap(true), nil
    63  }
    64  
    65  // SetSession updates the data map for specified session id.
    66  // This function is called ever after session, which is changed dirty, is closed.
    67  // This copy all session data map from memory to storage.
    68  func (s *StorageMemory) SetSession(ctx context.Context, sessionId string, sessionData *gmap.StrAnyMap, ttl time.Duration) error {
    69  	return s.cache.Set(ctx, sessionId, sessionData, ttl)
    70  }
    71  
    72  // UpdateTTL updates the TTL for specified session id.
    73  // This function is called ever after session, which is not dirty, is closed.
    74  // It just adds the session id to the async handling queue.
    75  func (s *StorageMemory) UpdateTTL(ctx context.Context, sessionId string, ttl time.Duration) error {
    76  	_, err := s.cache.UpdateExpire(ctx, sessionId, ttl)
    77  	return err
    78  }