Documentation
¶
Overview ¶
Package session provides session management for authentication.
Index ¶
- type Cache
- func (c *Cache) Clear()
- func (c *Cache) Delete(tokenHash string)
- func (c *Cache) DeleteBySessionID(sessionID string)
- func (c *Cache) DeleteByUserID(userID string)
- func (c *Cache) Get(tokenHash string) (*CachedSession, bool)
- func (c *Cache) Set(tokenHash string, session *CachedSession)
- func (c *Cache) Size() int
- func (c *Cache) Stop()
- type CacheConfig
- type CachedSession
- type CreateSessionInput
- type Manager
- func (m *Manager) CacheSize() int
- func (m *Manager) ClearCache()
- func (m *Manager) Create(ctx context.Context, input CreateSessionInput) (*SessionInfo, error)
- func (m *Manager) Delete(ctx context.Context, sessionID string) error
- func (m *Manager) DeleteByUserID(ctx context.Context, userID string) error
- func (m *Manager) DeleteExpired(ctx context.Context) (int64, error)
- func (m *Manager) GetByID(ctx context.Context, sessionID string) (*models.Session, error)
- func (m *Manager) GetByTokenHash(ctx context.Context, tokenHash string) (*CachedSession, error)
- func (m *Manager) GetByUserID(ctx context.Context, userID string) ([]models.Session, error)
- func (m *Manager) UpdateLastActivity(ctx context.Context, sessionID string) error
- type ManagerConfig
- type SessionInfo
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) DeleteBySessionID ¶
DeleteBySessionID removes a specific session from the cache.
func (*Cache) DeleteByUserID ¶
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.
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) Create ¶
func (m *Manager) Create(ctx context.Context, input CreateSessionInput) (*SessionInfo, error)
Create creates a new session for a user.
func (*Manager) DeleteByUserID ¶
DeleteByUserID removes all sessions for a user.
func (*Manager) DeleteExpired ¶
DeleteExpired removes all expired sessions from the database.
func (*Manager) GetByTokenHash ¶
GetByTokenHash retrieves a session by its token hash. First checks the cache, then falls back to the database.
func (*Manager) GetByUserID ¶
GetByUserID retrieves all sessions for a user.
type ManagerConfig ¶
ManagerConfig holds configuration for the session manager.