Documentation
¶
Index ¶
- type Session
- func (s *Session) Delete(ctx context.Context, key string) error
- func (s *Session) Get(ctx context.Context, key string) (any, error)
- func (s *Session) ID() string
- func (s *Session) IsModified() bool
- func (s *Session) Save() error
- func (s *Session) Set(ctx context.Context, key string, value any) error
- func (s *Session) SetMaxAge(seconds int)
- type Store
- func (s *Store) GC(ctx context.Context) error
- func (s *Store) Generate(ctx context.Context, id string) (session.Session, error)
- func (s *Store) Get(ctx context.Context, id string) (session.Session, error)
- func (s *Store) Refresh(ctx context.Context, id string) error
- func (s *Store) Remove(ctx context.Context, id string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents an in-memory session object that stores and retrieves user-specific data with a unique identifier. It implements the session.Session interface and provides thread-safe operations on session data.
Fields:
- id: A unique identifier for the session, used to reference it across HTTP requests.
- values: A thread-safe map that stores key-value pairs of session data.
- modified: A flag to track whether the session has been modified since creation.
- expiration: The duration for which the session is valid.
- maxAge: Maximum lifetime of the session in seconds.
func (*Session) Delete ¶ added in v0.1.20
Delete removes a key-value pair from the session. If the key does not exist, this operation is a no-op.
Parameters:
- ctx context.Context: The context in which the deletion is requested. This context is not used in the current implementation but is included for interface compliance.
- key string: The identifier of the key-value pair to be removed from the session.
Returns:
- error: An error if the deletion operation fails; otherwise, nil is returned.
This method is safe for concurrent use by multiple goroutines.
func (*Session) Get ¶
Get retrieves a value from the session based on the provided key. This method provides thread-safe access to session data.
Parameters:
- ctx context.Context: The context in which the retrieval is requested. The context can carry deadlines, cancellation signals, and other request-scoped values across API boundaries. However, this method does not utilize these context features.
- key string: The identifier used to retrieve the corresponding value from the session's data store.
Returns:
- any: The retrieved value associated with the key. If the key is not found, nil is returned.
- error: An error if the retrieval operation fails; otherwise, nil is returned.
This method is safe for concurrent use by multiple goroutines as it uses the thread-safe sync.Map to store session values.
func (*Session) ID ¶
ID returns the unique identifier of the session. This identifier is used to associate the session with a particular user or client across multiple HTTP requests.
Returns:
- string: The unique identifier of the session.
This method is safe for concurrent use by multiple goroutines.
func (*Session) IsModified ¶ added in v0.1.20
IsModified returns whether the session has been modified since it was last saved or created.
Returns:
- bool: true if the session has been modified, false otherwise.
This method is safe for concurrent use by multiple goroutines.
func (*Session) Save ¶ added in v0.1.19
Save persists any changes made to the session. In this in-memory implementation, there is no need for explicit persistence, so this method simply resets the modified flag.
Returns:
- error: An error if the save operation fails; otherwise, nil is returned to indicate success.
This method is safe for concurrent use by multiple goroutines.
func (*Session) Set ¶
Set stores a value in the session using the provided key. If the key already exists, the value is updated. This method ensures thread-safe modification of session data.
Parameters:
- ctx context.Context: The context in which the storage is requested. The context can carry deadlines, cancellation signals, and other request-scoped values across API boundaries. However, this method does not utilize these context features.
- key string: The identifier under which the value should be stored in the session's data store.
- value any: The data to be stored in the session. This can be of any type.
Returns:
- error: An error if the storage operation fails; otherwise, nil is returned.
This method is safe for concurrent use by multiple goroutines as it uses the thread-safe sync.Map to store session values.
func (*Session) SetMaxAge ¶ added in v0.1.20
SetMaxAge sets the maximum lifetime of the session in seconds.
Parameters:
- seconds: The maximum lifetime of the session in seconds. A positive value sets the session to expire after the specified number of seconds. A negative value means the session expires when the browser is closed. A zero value deletes the session immediately.
This method is safe for concurrent use by multiple goroutines.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store represents a storage mechanism for session information. It provides thread-safe access to session data and ensures that session information is stored and retrieved efficiently with an automated expiration policy.
Fields:
- mutex sync.RWMutex: A read/write mutual exclusion lock. It is used to ensure that only one goroutine can write to the sessions cache at a time and that there can be multiple concurrent readers. This ensures that the sessions cache is safe to use concurrently across multiple goroutines without data race conditions.
- sessions *cache.Cache: A pointer to an instance of a cache that stores session data. This cache library typically provides a fast in-memory key:value store with expiration capabilities for stored items. The pointer allows the Store to manage sessions in a centralized manner.
- expiration time.Duration: A duration after which a session is considered expired and can be removed from the cache. This value sets a time limit on how long session data should persist in the cache before being automatically deleted.
func InitStore ¶
InitStore initializes and returns a new Store instance with the specified expiration duration.
The Store instance created by this function is prepared to manage session data with an automated expiration policy, which purges the session data after the specified duration. A store instance provides a thread-safe way to interact with session data across multiple goroutines.
Parameters:
- expiration time.Duration: The duration after which sessions should expire and be removed from the cache. This duration dictates how long a session will be kept in memory before being deleted automatically.
Returns:
- *Store: A pointer to a newly created Store instance, which holds the session cache and the expiration policy for session data.
Example: To create a store with a 30-minute expiration period for sessions, call InitStore with time.Minute * 30 as the parameter.
func NewStore ¶ added in v0.1.19
NewStore creates a new memory store for session data. It initializes the store with a default expiration duration.
func (*Store) GC ¶ added in v0.1.20
GC performs garbage collection on expired sessions in the session store. This method triggers the cache's internal garbage collection mechanism to clean up expired sessions, freeing up memory and resources.
Parameters:
- ctx context.Context: The context in which the garbage collection is requested. This context can be used to control the execution of the garbage collection, but is not utilized in the current implementation.
Returns:
- error: An error if the garbage collection operation fails; otherwise, nil indicating success.
Note that go-cache has its own internal garbage collection that runs on a separate goroutine, so this method primarily forces an immediate clean-up cycle. For most applications, relying on the automatic garbage collection is sufficient.
func (*Store) Generate ¶
Generate creates a new session with the specified ID and stores it in the Store's session cache. It ensures that the session is safely created and stored even when accessed by multiple goroutines simultaneously.
The function locks the Store's mutex before creating the new session to prevent concurrent write access, providing thread safety. It then adds the session to the store's cache with the predefined expiration policy before returning the session to the caller.
Parameters:
- ctx context.Context: The context in which the session is generated. The context allows for controlling cancellations and timeouts, but is not utilized in this function.
- id string: The unique identifier for the new session.
Returns:
- session.Session: The newly created session object with the provided ID.
- error: Any errors encountered during the generation of the session; returns nil since this implementation does not produce errors.
This method may need to be updated if error-handling logic is introduced, such as when checks for existing session IDs need to be implemented or if the cache.Set method could potentially return an error.
func (*Store) Get ¶
Get retrieves a session from the Store's session cache using the provided session ID. It provides thread-safe access to session data through read-locking mechanisms.
Parameters:
- ctx context.Context: The context in which the session retrieval is requested. The context can carry deadlines, cancellation signals, and other request-scoped values across API boundaries. However, the method does not utilize these context features in its current implementation.
- id string: The unique identifier used to look up the session in the session cache.
Returns:
- session.Session: The retrieved session object if found.
- error: An error is returned when the session with the specified ID cannot be found or if there are issues during the retrieval process.
The method assumes that the errs.ErrIdSessionNotFound function returns an appropriate error when a session ID doesn't exist in the cache.
func (*Store) Refresh ¶
Refresh updates the expiration time of an existing session, effectively "refreshing" it. This method looks up a session by its ID and, if found, extends its life in the Store's cache according to the Store's expiration policy.
The method provides thread safety by locking the Store's mutex, thereby preventing concurrent access to the sessions cache during the update process.
Parameters:
- ctx context.Context: The context in which the session refresh is executed. The context provides the ability to handle cancellations and timeouts, although this functionality is not utilized in the current function implementation.
- id string: The unique identifier of the session that is being refreshed.
Returns:
- error: An error is returned if the session with the specified ID cannot be found; otherwise, nil is returned after successfully refreshing the session expiration time.
This method assumes that the errs.ErrIdSessionNotFound function returns a relevant error when a session with a given ID does not exist.
func (*Store) Remove ¶
Remove deletes a session from the Store's session cache using the provided session ID. It's designed to ensure thread-safe deletion of session data, avoiding concurrent access issues.
Parameters:
- ctx context.Context: The context in which session removal is requested. This typically contains information about deadlines, cancellation signals, and other request-scoped values relevant to the operation. However, the context is not directly utilized within this function.
- id string: The unique identifier of the session to be removed from the cache.
Returns:
- error: An error is returned if any issues occur during the deletion process; however, in this implementation, the function always returns nil, indicating success.
It's important to handle potential errors in future revisions of this method, particularly when operations that can fail are introduced.