Documentation
¶
Index ¶
- type InvalidationCache
- type MemoryCache
- type MemorySessionStore
- type MySQLStore
- type RedisCache
- type RedisConfig
- type SQLiteStore
- func (s *SQLiteStore) Close() error
- func (s *SQLiteStore) Delete(sessionID string) error
- func (s *SQLiteStore) Exists(sessionID string) (bool, error)
- func (s *SQLiteStore) GetActiveByUser(userID string) ([]*Session, error)
- func (s *SQLiteStore) Save(session *Session) error
- func (s *SQLiteStore) Set(sessionID string, ttl time.Duration) error
- type Session
- type SessionStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InvalidationCache ¶
type InvalidationCache interface {
// Set marks a session ID as invalidated with the given TTL.
// After TTL expires, the entry is automatically removed.
Set(sessionID string, ttl time.Duration) error
// Exists returns true if the session ID has been invalidated
// and the TTL has not expired.
Exists(sessionID string) (bool, error)
// Close releases any resources held by the cache.
Close() error
}
InvalidationCache defines the interface for tracking invalidated session IDs. Implementations must be safe for concurrent use.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements InvalidationCache using an in-memory map. Expired entries are cleaned up periodically.
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache creates a new in-memory invalidation cache. It starts a background goroutine that periodically cleans up expired entries.
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close() error
Close stops the background cleanup goroutine.
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore implements SessionStore using an in-memory map. This is useful for testing but not recommended for production.
func NewMemorySessionStore ¶
func NewMemorySessionStore() *MemorySessionStore
NewMemorySessionStore creates a new in-memory session store.
func (*MemorySessionStore) Close ¶
func (s *MemorySessionStore) Close() error
Close is a no-op for the memory store.
func (*MemorySessionStore) Delete ¶
func (s *MemorySessionStore) Delete(sessionID string) error
Delete removes a session by its ID.
func (*MemorySessionStore) GetActiveByUser ¶
func (s *MemorySessionStore) GetActiveByUser(userID string) ([]*Session, error)
GetActiveByUser returns all non-expired sessions for a user.
func (*MemorySessionStore) Save ¶
func (s *MemorySessionStore) Save(session *Session) error
Save persists a new session.
type MySQLStore ¶
type MySQLStore struct {
// contains filtered or unexported fields
}
MySQLStore implements SessionStore using MySQL.
func NewMySQL ¶
func NewMySQL(db *sql.DB) (*MySQLStore, error)
NewMySQL creates a new MySQL session store. The DSN format is: user:password@tcp(host:port)/database
func NewMySQLFromDSN ¶
func NewMySQLFromDSN(dsn string) (*MySQLStore, error)
NewMySQLFromDSN creates a new MySQL session store from a DSN.
func (*MySQLStore) Delete ¶
func (s *MySQLStore) Delete(sessionID string) error
Delete marks a session as invalidated (soft delete for audit trail).
func (*MySQLStore) GetActiveByUser ¶
func (s *MySQLStore) GetActiveByUser(userID string) ([]*Session, error)
GetActiveByUser returns all non-expired, non-invalidated sessions for a user.
func (*MySQLStore) Save ¶
func (s *MySQLStore) Save(session *Session) error
Save persists a new session.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements InvalidationCache using Redis. It leverages Redis's native TTL for automatic expiration.
func NewRedisCache ¶
func NewRedisCache(client *redis.Client, keyPrefix string) (*RedisCache, error)
NewRedisCache creates a new Redis invalidation cache from a Redis client and a key prefix. prefix typicallyends with a colon.
func NewRedisFromConfig ¶
func NewRedisFromConfig(cfg RedisConfig) (*RedisCache, error)
NewRedis creates a new Redis invalidation cache.
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(sessionID string) error
Delete removes an invalidation entry (useful for testing).
type RedisConfig ¶
type RedisConfig struct {
// Addr is the Redis server address (e.g., "localhost:6379")
Addr string
// Password is the Redis password (empty for no auth)
Password string
// DB is the Redis database number (0-15)
DB int
// KeyPrefix is prepended to all keys (default: "heimdall:invalidated:")
// typically ends with a colon.
KeyPrefix string
}
RedisConfig contains configuration options for Redis.
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore implements SessionStore using SQLite. It uses the pure Go modernc.org/sqlite driver.
func NewSQLite ¶
func NewSQLite(dbPath string) (*SQLiteStore, error)
NewSQLite creates a new SQLite session store. The database file is created if it doesn't exist.
func (*SQLiteStore) Close ¶
func (s *SQLiteStore) Close() error
Close closes the database connection.
func (*SQLiteStore) Delete ¶
func (s *SQLiteStore) Delete(sessionID string) error
Delete marks a session as invalidated (soft delete for audit trail).
func (*SQLiteStore) Exists ¶
func (s *SQLiteStore) Exists(sessionID string) (bool, error)
Exists returns true if the session ID has been invalidated. Checks the invalidated_at column in the sessions table.
func (*SQLiteStore) GetActiveByUser ¶
func (s *SQLiteStore) GetActiveByUser(userID string) ([]*Session, error)
GetActiveByUser returns all non-expired, non-invalidated sessions for a user.
func (*SQLiteStore) Save ¶
func (s *SQLiteStore) Save(session *Session) error
Save persists a new session.
func (*SQLiteStore) Set ¶
func (s *SQLiteStore) Set(sessionID string, ttl time.Duration) error
Set marks a session ID as invalidated. Note: This is typically already done by SessionStore.Delete(), so this is a no-op if the session was already invalidated. The TTL parameter is ignored since invalidated sessions are kept permanently for audit.
type Session ¶
type Session struct {
SessionID string
UserID string
DeviceIP string
DeviceUA string
Browser string
OS string
DeviceType string
LocCity string
LocCountry string
LocLat float64
LocLng float64
TTLSeconds int64
CreatedAt time.Time
}
Session represents a user session for storage. This is a copy of the main Session type to avoid circular imports.
type SessionStore ¶
type SessionStore interface {
// Save persists a new session. If a session with the same ID exists,
// it will be overwritten.
Save(session *Session) error
// Delete marks a session as invalidated (soft delete or hard delete based on your implementation).
// The built-in provider implementations soft delete.
// The session is kept for audit purposes but excluded from active queries.
Delete(sessionID string) error
// GetActiveByUser returns all non-expired, non-invalidated sessions for a user.
// Sessions are ordered by CreatedAt descending (newest first).
// Use [0] to get the latest session.
GetActiveByUser(userID string) ([]*Session, error)
// Close releases any resources held by the store.
Close() error
}
SessionStore defines the interface for session storage backends. Implementations must be safe for concurrent use.