store

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

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.

func (*MemoryCache) Exists

func (c *MemoryCache) Exists(sessionID string) (bool, error)

Exists returns true if the session ID has been invalidated and not expired.

func (*MemoryCache) Set

func (c *MemoryCache) Set(sessionID string, ttl time.Duration) error

Set marks a session ID as invalidated with the given TTL.

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) Close

func (s *MySQLStore) Close() error

Close closes the database connection.

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) Close

func (c *RedisCache) Close() error

Close closes the Redis connection.

func (*RedisCache) Delete

func (c *RedisCache) Delete(sessionID string) error

Delete removes an invalidation entry (useful for testing).

func (*RedisCache) Exists

func (c *RedisCache) Exists(sessionID string) (bool, error)

Exists returns true if the session ID has been invalidated.

func (*RedisCache) Set

func (c *RedisCache) Set(sessionID string, ttl time.Duration) error

Set marks a session ID as invalidated with the given TTL.

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.

func (*Session) ExpiresAt

func (s *Session) ExpiresAt() time.Time

ExpiresAt returns the expiration time of the session.

func (*Session) IsExpired

func (s *Session) IsExpired() bool

IsExpired returns true if the session has expired.

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.

Jump to

Keyboard shortcuts

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