memory

package
v0.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

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 is a data structure that represents a user session in a concurrent environment. It stores session-specific information, such as a unique session ID and session values, in a thread-safe manner, ensuring that multiple goroutines can interact with the values concurrently without causing race conditions.

Fields:

  • id string: The unique identifier of the session, used to retrieve or differentiate the session.
  • values sync.Map: A thread-safe map provided by the sync package, used to store session-specific values. sync.Map uses fine-grained locking which is more efficient for cases where multiple goroutines are reading, writing, and iterating over entries in the map simultaneously.

func (*Session) Get

func (s *Session) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from the session based on a provided key. It utilizes the sync.Map's Load method to safely access the value, ensuring that simultaneous reads/writes to the values Map are properly synchronized.

Parameters:

  • ctx context.Context: The context for the operation. While it is a common pattern to include context for potential future use in cancellations and timeouts, it is not currently used in this method.
  • key string: The key associated with the value to retrieve from the session.

Returns:

  • any: The value associated with the key if it is found within the session's values map.
  • error: An error if the key does not exist in the session's values map. ErKeyNotFound is returned with the missing key as its parameter.

func (*Session) ID

func (s *Session) ID() string

ID returns the unique session identifier associated with the session instance. This identifier is intended to be used to reference or differentiate the session in a larger context, such as a session manager or store. The method provides a safe way to access the session's ID field.

Parameters: - None

Returns: - string: The unique identifier string of the current session instance.

func (*Session) Set

func (s *Session) Set(ctx context.Context, key string, value any) error

Set assigns a value to a specific key within the session. It leverages the Store method from sync.Map for a safe concurrent write operation. As of this implementation, it always returns a nil error, denoting a successful operation.

Parameters:

  • ctx context.Context: The context for the operation. Although it is often included to handle request-scoped values, cancellations, and timeouts, it is not utilized in this method at present.
  • key string: The key to associate with the value within the session's values map.
  • value any: The value to be associated with the key.

Returns:

  • error: nil, indicating that the value was successfully stored. If future implementations introduce potential errors, the method's signature and documentation would need corresponding updates.

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

func InitStore(expiration time.Duration) *Store

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 (*Store) Generate

func (s *Store) Generate(ctx context.Context, id string) (session.Session, error)

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

func (s *Store) Get(ctx context.Context, id string) (session.Session, error)

Get retrieves the session associated with the provided ID from the store. It features thread safety by using a read lock to allow multiple concurrent read operations while preventing write operations, ensuring data consistency.

Parameters:

  • ctx context.Context: The context in which the session retrieval is taking place. The context could be used to carry deadlines, cancellation signals, and other request-scoped values, but it's not used within this function.
  • id string: The unique identifier for the session we want to retrieve.

Returns: - session.Session: The session object associated with the ID, if found. - error: An error if no session is found with the given ID, otherwise nil.

If future versions of the Get function introduce returnable errors in other situations, this documentation and implementation may need to be updated accordingly.

func (*Store) Refresh

func (s *Store) Refresh(ctx context.Context, id string) error

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

func (s *Store) Remove(ctx context.Context, id string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL