gsession_storage.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). 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. package gsession
  7. import (
  8. "github.com/gogf/gf/container/gmap"
  9. "time"
  10. )
  11. // Storage is the interface definition for session storage.
  12. type Storage interface {
  13. // New creates a custom session id.
  14. // This function can be used for custom session creation.
  15. New(ttl time.Duration) (id string)
  16. // Get retrieves and returns session value with given key.
  17. // It returns nil if the key does not exist in the session.
  18. Get(id string, key string) interface{}
  19. // GetMap retrieves all key-value pairs as map from storage.
  20. GetMap(id string) map[string]interface{}
  21. // GetSize retrieves and returns the size of key-value pairs from storage.
  22. GetSize(id string) int
  23. // Set sets one key-value session pair to the storage.
  24. // The parameter <ttl> specifies the TTL for the session id.
  25. Set(id string, key string, value interface{}, ttl time.Duration) error
  26. // SetMap batch sets key-value session pairs as map to the storage.
  27. // The parameter <ttl> specifies the TTL for the session id.
  28. SetMap(id string, data map[string]interface{}, ttl time.Duration) error
  29. // Remove deletes key with its value from storage.
  30. Remove(id string, key string) error
  31. // RemoveAll deletes all key-value pairs from storage.
  32. RemoveAll(id string) error
  33. // GetSession returns the session data as *gmap.StrAnyMap for given session id from storage.
  34. //
  35. // The parameter <ttl> specifies the TTL for this session.
  36. // The parameter <data> is the current old session data stored in memory,
  37. // and for some storage it might be nil if memory storage is disabled.
  38. //
  39. // This function is called ever when session starts. It returns nil if the TTL is exceeded.
  40. GetSession(id string, ttl time.Duration, data *gmap.StrAnyMap) (*gmap.StrAnyMap, error)
  41. // SetSession updates the data for specified session id.
  42. // This function is called ever after session, which is changed dirty, is closed.
  43. // This copy all session data map from memory to storage.
  44. SetSession(id string, data *gmap.StrAnyMap, ttl time.Duration) error
  45. // UpdateTTL updates the TTL for specified session id.
  46. // This function is called ever after session, which is not dirty, is closed.
  47. UpdateTTL(id string, ttl time.Duration) error
  48. }