cache

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTokenNotFound = fmt.Errorf("token not found")

Functions

func HashToken

func HashToken(token string) string

HashToken hashes a token string, this will makes the token much shorter. The shorter token can be found faster in the cache.

Types

type MemoryTokenStore

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

MemoryTokenStore implements TokenStore using ttlcache.

func (*MemoryTokenStore) Clear

func (s *MemoryTokenStore) Clear(_ context.Context) error

Clear removes all tokens from the cache.

func (*MemoryTokenStore) Close

func (s *MemoryTokenStore) Close() error

Close stops the cleanup goroutine.

func (*MemoryTokenStore) Count

func (s *MemoryTokenStore) Count(_ context.Context) int

Count counts the number of tokens in the cache.

func (*MemoryTokenStore) Delete

func (s *MemoryTokenStore) Delete(_ context.Context, token string) error

Delete removes a token from the cache.

func (*MemoryTokenStore) DeleteExpired

func (s *MemoryTokenStore) DeleteExpired(_ context.Context) error

DeleteExpired removes all expired tokens from the cache.

func (*MemoryTokenStore) Get

func (s *MemoryTokenStore) Get(_ context.Context, token string) (*TokenEntry, error)

Get implements TokenStore.Get.

func (*MemoryTokenStore) Set

func (s *MemoryTokenStore) Set(_ context.Context, token *TokenEntry) error

Set implements TokenStore.Set.

type TokenCache

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

TokenCache provides thread-safe caching for tokens with TTL

func NewTokenCache

func NewTokenCache(cleanupInterval time.Duration) *TokenCache

NewTokenCache creates a new token cache with the specified cleanup interval

func (*TokenCache) Close

func (tc *TokenCache) Close()

Close stops the cleanup goroutine

func (*TokenCache) Delete

func (tc *TokenCache) Delete(ctx context.Context, tokenString string)

Delete removes a token from the cache

func (*TokenCache) Get

func (tc *TokenCache) Get(ctx context.Context, tokenString string) (*TokenCacheEntry, bool)

Get retrieves a token and its claims from the cache

func (*TokenCache) Set

func (tc *TokenCache) Set(ctx context.Context, tokenString string, expiresAt time.Time, claims interface{})

Set stores a token in the cache with its expiration time and claims

type TokenCacheEntry

type TokenCacheEntry struct {
	Token     string
	ExpiresAt time.Time
	Claims    interface{}
}

TokenCacheEntry represents a cached token with its metadata

type TokenEntry

type TokenEntry struct {
	ID         string    `redis:"id"`              // Unique token identifier
	TokenType  string    `redis:"tokenType"`       // "access_token" or "refresh_token"
	TokenValue string    `redis:"tokenValue"`      // The actual token string
	ClientID   string    `redis:"clientId"`        // Client that requested the token
	UserID     string    `redis:"userId"`          // User who authorized the token
	Scope      string    `redis:"scope"`           // Authorized scopes
	ExpiresAt  time.Time `redis:"expiresAt"`       // Expiration timestamp
	IsRevoked  bool      `redis:"isRevoked"`       // Whether token is revoked
	CreatedAt  time.Time `redis:"createdAt"`       // Creation timestamp
	LastUsedAt time.Time `redis:"lastUsedAt"`      // Last usage timestamp
	Roles      []string  `redis:"roles,omitempty"` // New field
}

TokenEntry represents a cached token entry

type TokenStore

type TokenStore interface {
	// Set stores a token with its claims and expiry time
	Set(ctx context.Context, token *TokenEntry) error

	// Get retrieves a token entry from the cache
	Get(ctx context.Context, token string) (*TokenEntry, error)

	// Delete removes a token from the cache
	Delete(ctx context.Context, token string) error

	// DeleteExpired removes all expired tokens from the cache
	DeleteExpired(ctx context.Context) error

	// Clear removes all tokens from the cache
	Clear(ctx context.Context) error

	// Count returns the number of tokens in the cache
	Count(ctx context.Context) int
}

TokenStore defines the interface for token caching implementations

func NewMemoryTokenStore

func NewMemoryTokenStore(cleanupInterval time.Duration) TokenStore

NewMemoryTokenStore creates a new in-memory token store with automatic cleanup.

Directories

Path Synopsis
Package redis implements the TokenStore interface using Redis.
Package redis implements the TokenStore interface using Redis.

Jump to

Keyboard shortcuts

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