github.com/gogf/gf@v1.16.9/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/gogf/gf. 6 7 package gsession 8 9 import ( 10 "context" 11 "github.com/gogf/gf/container/gmap" 12 "time" 13 ) 14 15 // StorageMemory implements the Session Storage interface with memory. 16 type StorageMemory struct{} 17 18 // NewStorageMemory creates and returns a file storage object for session. 19 func NewStorageMemory() *StorageMemory { 20 return &StorageMemory{} 21 } 22 23 // New creates a session id. 24 // This function can be used for custom session creation. 25 func (s *StorageMemory) New(ctx context.Context, ttl time.Duration) (id string, err error) { 26 return "", ErrorDisabled 27 } 28 29 // Get retrieves session value with given key. 30 // It returns nil if the key does not exist in the session. 31 func (s *StorageMemory) Get(ctx context.Context, id string, key string) (value interface{}, err error) { 32 return nil, ErrorDisabled 33 } 34 35 // GetMap retrieves all key-value pairs as map from storage. 36 func (s *StorageMemory) GetMap(ctx context.Context, id string) (data map[string]interface{}, err error) { 37 return nil, ErrorDisabled 38 } 39 40 // GetSize retrieves the size of key-value pairs from storage. 41 func (s *StorageMemory) GetSize(ctx context.Context, id string) (size int, err error) { 42 return -1, ErrorDisabled 43 } 44 45 // Set sets key-value session pair to the storage. 46 // The parameter `ttl` specifies the TTL for the session id (not for the key-value pair). 47 func (s *StorageMemory) Set(ctx context.Context, id string, key string, value interface{}, ttl time.Duration) error { 48 return ErrorDisabled 49 } 50 51 // SetMap batch sets key-value session pairs with map to the storage. 52 // The parameter `ttl` specifies the TTL for the session id(not for the key-value pair). 53 func (s *StorageMemory) SetMap(ctx context.Context, id string, data map[string]interface{}, ttl time.Duration) error { 54 return ErrorDisabled 55 } 56 57 // Remove deletes key with its value from storage. 58 func (s *StorageMemory) Remove(ctx context.Context, id string, key string) error { 59 return ErrorDisabled 60 } 61 62 // RemoveAll deletes all key-value pairs from storage. 63 func (s *StorageMemory) RemoveAll(ctx context.Context, id string) error { 64 return ErrorDisabled 65 } 66 67 // GetSession returns the session data as *gmap.StrAnyMap for given session id from storage. 68 // 69 // The parameter `ttl` specifies the TTL for this session, and it returns nil if the TTL is exceeded. 70 // The parameter `data` is the current old session data stored in memory, 71 // and for some storage it might be nil if memory storage is disabled. 72 // 73 // This function is called ever when session starts. 74 func (s *StorageMemory) GetSession(ctx context.Context, id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error) { 75 return data, nil 76 } 77 78 // SetSession updates the data map for specified session id. 79 // This function is called ever after session, which is changed dirty, is closed. 80 // This copy all session data map from memory to storage. 81 func (s *StorageMemory) SetSession(ctx context.Context, id string, data *gmap.StrAnyMap, ttl time.Duration) error { 82 return nil 83 } 84 85 // UpdateTTL updates the TTL for specified session id. 86 // This function is called ever after session, which is not dirty, is closed. 87 // It just adds the session id to the async handling queue. 88 func (s *StorageMemory) UpdateTTL(ctx context.Context, id string, ttl time.Duration) error { 89 return nil 90 }