session

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package session provides session management for authentication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache provides an in-memory cache for sessions. It's designed for O(1) lookups to avoid database hits during authentication.

func NewCache

func NewCache(cfg CacheConfig) *Cache

NewCache creates a new session cache with the given configuration.

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all sessions from the cache.

func (*Cache) Delete

func (c *Cache) Delete(tokenHash string)

Delete removes a session from the cache.

func (*Cache) DeleteBySessionID

func (c *Cache) DeleteBySessionID(sessionID string)

DeleteBySessionID removes a specific session from the cache.

func (*Cache) DeleteByUserID

func (c *Cache) DeleteByUserID(userID string)

DeleteByUserID removes all sessions for a user from the cache.

func (*Cache) Get

func (c *Cache) Get(tokenHash string) (*CachedSession, bool)

Get retrieves a session from the cache by token hash. Returns nil, false if the session is not found or has expired.

func (*Cache) Set

func (c *Cache) Set(tokenHash string, session *CachedSession)

Set stores a session in the cache.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the current number of cached sessions.

func (*Cache) Stop

func (c *Cache) Stop()

Stop stops the cache cleanup goroutine. Call this when shutting down the application to prevent goroutine leaks.

type CacheConfig

type CacheConfig struct {
	MaxSize int           // Maximum number of cached sessions (default: 1000)
	TTL     time.Duration // Time-to-live for cached entries (default: 5 minutes)
}

CacheConfig holds configuration for the session cache.

type CachedSession

type CachedSession struct {
	UserID    string
	Email     string
	Role      string
	SessionID string
	DeviceID  *string
	ExpiresAt time.Time
	CachedAt  time.Time
}

CachedSession represents a session stored in the in-memory cache. This provides O(1) lookups without database hits for authenticated requests.

type CreateSessionInput

type CreateSessionInput struct {
	UserID    string
	Email     string
	Role      string
	DeviceID  *string
	IPAddress *string
	UserAgent *string
	ExpiresAt time.Time
}

CreateSessionInput holds input for creating a new session.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles session CRUD operations.

func NewManager

func NewManager(db *gorm.DB, cfg ManagerConfig) *Manager

NewManager creates a new session manager.

func (*Manager) CacheSize

func (m *Manager) CacheSize() int

CacheSize returns the current size of the session cache.

func (*Manager) ClearCache

func (m *Manager) ClearCache()

ClearCache clears the session cache.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, input CreateSessionInput) (*SessionInfo, error)

Create creates a new session for a user.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, sessionID string) error

Delete removes a session by its ID.

func (*Manager) DeleteByUserID

func (m *Manager) DeleteByUserID(ctx context.Context, userID string) error

DeleteByUserID removes all sessions for a user.

func (*Manager) DeleteExpired

func (m *Manager) DeleteExpired(ctx context.Context) (int64, error)

DeleteExpired removes all expired sessions from the database.

func (*Manager) GetByID

func (m *Manager) GetByID(ctx context.Context, sessionID string) (*models.Session, error)

GetByID retrieves a session by its ID.

func (*Manager) GetByTokenHash

func (m *Manager) GetByTokenHash(ctx context.Context, tokenHash string) (*CachedSession, error)

GetByTokenHash retrieves a session by its token hash. First checks the cache, then falls back to the database.

func (*Manager) GetByUserID

func (m *Manager) GetByUserID(ctx context.Context, userID string) ([]models.Session, error)

GetByUserID retrieves all sessions for a user.

func (*Manager) UpdateLastActivity

func (m *Manager) UpdateLastActivity(ctx context.Context, sessionID string) error

UpdateLastActivity updates the last activity timestamp for a session.

type ManagerConfig

type ManagerConfig struct {
	CacheMaxSize int
	CacheTTL     time.Duration
}

ManagerConfig holds configuration for the session manager.

type SessionInfo

type SessionInfo struct {
	SessionID string
	TokenHash string
	ExpiresAt time.Time
}

SessionInfo holds information about a created session.

Jump to

Keyboard shortcuts

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