drivers

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package drivers provides session store implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemoryStore

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

MemoryStore is a fast in-process session store backed by a sync.Map. It is the default session store in development and is suitable for single-node production. Use a database or Redis store for multi-node deployments.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a MemoryStore and starts the background eviction goroutine. Call Close when the store is no longer needed to stop it.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close stops the background eviction goroutine and its ticker. It is safe to call multiple times. Wire it into application shutdown (e.g. defer store.Close()).

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, id string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, id string) (map[string]any, error)

func (*MemoryStore) Regenerate

func (s *MemoryStore) Regenerate(ctx context.Context, oldID string) (string, error)

func (*MemoryStore) Set

func (s *MemoryStore) Set(_ context.Context, id string, data map[string]any, ttl time.Duration) error

type Redis

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

Redis is a session store backed by Redis, suitable for multi-node deployments where sessions must be shared across processes.

Sessions are stored as JSON under "oni:session:<id>" with a TTL equal to the session lifetime; every Save refreshes the TTL (sliding expiration). Note that JSON round-tripping means numeric session values come back as float64 and concrete types are flattened to their JSON forms.

func NewRedis

func NewRedis(url string) (*Redis, error)

NewRedis creates a Redis session store from a Redis URL, e.g. "redis://:password@localhost:6379/0". The connection is established lazily on first use; an invalid URL fails immediately.

func NewRedisFromClient

func NewRedisFromClient(c *redis.Client) *Redis

NewRedisFromClient wraps an existing go-redis client, letting applications share one client between sessions, cache, and queues.

func (*Redis) Close

func (r *Redis) Close() error

Close closes the underlying Redis client. It is safe to call on stores built via NewRedisFromClient too — only do so once the client is no longer shared.

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, id string) error

Delete removes the session. Deleting a non-existent session is not an error.

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, id string) (map[string]any, error)

Get returns the session data for id. A missing or expired key yields session.ErrNotFound (the Manager then issues a fresh session); transport errors are returned verbatim so callers can distinguish an outage from a logged-out user. Corrupt payloads are treated as not-found so a bad record self-heals into a fresh session instead of wedging the user.

func (*Redis) Regenerate

func (r *Redis) Regenerate(ctx context.Context, oldID string) (string, error)

Regenerate rotates the session ID, preserving data and remaining TTL. The new session is stored BEFORE the old one is deleted: if the store fails midway, the data still exists under at least one ID instead of being lost (same ordering as the memory driver).

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, id string, data map[string]any, ttl time.Duration) error

Set stores the session data as JSON with the given TTL, refreshing the expiry on every call (Manager.Save calls this with the session lifetime).

Jump to

Keyboard shortcuts

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