localcache

package
v3.1.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package localcache provides localcache functionality.

Index

Constants

This section is empty.

Variables

View Source
var AuthCache = NewUserAuthCache()

AuthCache is a global cache instance used to store and manage user authentication states efficiently.

View Source
var LocalCache = NewCache(5*time.Minute, 0)

LocalCache is a cache object with a default expiration duration of 5 minutes. Cleanup interval is set to 0 to avoid a globally running janitor. Specific subsystems (e.g., LDAP) should manage their own lifecycle-bound janitors.

Functions

This section is empty.

Types

type Cache deprecated

type Cache struct {
	*MemoryShardedCache
}

Cache is a custom in-memory cache implementation (kept for backward compatibility)

Deprecated: Use MemoryShardedCache instead.

func NewCache deprecated

func NewCache(defaultExpiration, cleanupInterval time.Duration) *Cache

NewCache creates a new cache with the given default expiration and cleanup interval For backward compatibility, it now returns a MemoryShardedCache wrapped in a Cache struct

Deprecated: Use NewMemoryShardedCache instead.

func (*Cache) Delete

func (c *Cache) Delete(k string)

Delete delegates to MemoryShardedCache.Delete

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

DeleteExpired delegates to MemoryShardedCache.DeleteExpired

func (*Cache) Get

func (c *Cache) Get(k string) (any, bool)

Get delegates to MemoryShardedCache.Get

func (*Cache) Set

func (c *Cache) Set(k string, x any, d time.Duration)

Set delegates to MemoryShardedCache.Set

func (*Cache) Stop

func (c *Cache) Stop()

Stop stops the background janitor on the underlying sharded cache, if any.

type Item

type Item struct {
	Object     any
	Expiration int64
}

Item represents a cached item with expiration time

type LRUCache

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

LRUCache is a small, goroutine-safe LRU cache with per-entry TTL.

func NewLRU

func NewLRU(capacity int) *LRUCache

NewLRU creates a new LRUCache with the given capacity. Capacity <= 0 disables caching.

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

Delete removes a key if present.

func (*LRUCache) Get

func (c *LRUCache) Get(key string) (any, bool)

Get returns the value for key if present and not expired.

func (*LRUCache) Len

func (c *LRUCache) Len() int

Len returns the number of items currently stored in the LRU cache. This method is thread-safe.

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value any, ttl time.Duration)

Set inserts or updates a key with a value and TTL. ttl<=0 means no expiry.

func (*LRUCache) SetOnEvict

func (c *LRUCache) SetOnEvict(cb func(key string, value any))

SetOnEvict registers a callback that will be invoked whenever an entry is evicted from the cache. The callback receives the key and value of the evicted entry.

type MemoryCacheShard

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

MemoryCacheShard represents a single shard of the memory cache

type MemoryShardedCache

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

MemoryShardedCache is a cache that is split into multiple shards to reduce mutex contention

func NewMemoryShardedCache

func NewMemoryShardedCache(numShards int, defaultExpiration, cleanupInterval time.Duration) *MemoryShardedCache

NewMemoryShardedCache creates a new sharded cache with the specified number of shards

func (*MemoryShardedCache) Clear

func (sc *MemoryShardedCache) Clear()

Clear removes every stored item while preserving the cache lifecycle.

func (*MemoryShardedCache) Delete

func (sc *MemoryShardedCache) Delete(k string)

Delete removes an item from the MemoryShardedCache

func (*MemoryShardedCache) DeleteByPrefix

func (sc *MemoryShardedCache) DeleteByPrefix(prefix string)

DeleteByPrefix removes all items from the MemoryShardedCache that have a key starting with the given prefix

func (*MemoryShardedCache) DeleteExpired

func (sc *MemoryShardedCache) DeleteExpired()

DeleteExpired removes all expired items from the MemoryShardedCache

func (*MemoryShardedCache) Get

func (sc *MemoryShardedCache) Get(k string) (any, bool)

Get retrieves an item from the MemoryShardedCache

func (*MemoryShardedCache) Len

func (sc *MemoryShardedCache) Len() int

Len returns the total number of items currently stored across all shards.

func (*MemoryShardedCache) Set

func (sc *MemoryShardedCache) Set(k string, x any, d time.Duration)

Set adds an item to the MemoryShardedCache with the given expiration duration

func (*MemoryShardedCache) Stop

func (sc *MemoryShardedCache) Stop()

Stop stops the background janitor if it is running. It is safe to call multiple times.

type Resettable

type Resettable interface {
	// Reset resets all fields of the object to their zero values
	Reset()
}

Resettable is an interface for objects that can be reset Objects implementing this interface can be reset to their zero values This is useful for objects that are stored in a sync.Pool

type SimpleCache

type SimpleCache interface {
	// Set stores a value in the cache with the specified key and an optional time-to-live (TTL) duration.
	Set(key string, value any, ttl time.Duration)

	// Get retrieves the value associated with the specified key from the cache. Returns the value and a boolean indicating presence.
	Get(key string) (any, bool)

	// Delete removes the value associated with the given key from the cache.
	Delete(key string)

	// Len returns the number of items currently stored in the cache.
	Len() int
}

SimpleCache provides the tiny surface needed by callers without tying them to a specific cache impl.

type UserAuthCache

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

UserAuthCache is a cache for authentication results It stores whether a user has been successfully authenticated It can be used by both LDAP and Lua backends

func NewUserAuthCache

func NewUserAuthCache() *UserAuthCache

NewUserAuthCache creates a new UserAuthCache

func (*UserAuthCache) Clear

func (c *UserAuthCache) Clear()

Clear removes all entries from the cache

func (*UserAuthCache) Close

func (c *UserAuthCache) Close()

Close releases the cache janitor owned by this instance.

func (*UserAuthCache) Delete

func (c *UserAuthCache) Delete(username string)

Delete removes an entry from the cache

func (*UserAuthCache) Get

func (c *UserAuthCache) Get(username string) (bool, bool)

Get retrieves an entry from the cache Returns the authentication status and whether the entry was found

func (*UserAuthCache) IsAuthenticated

func (c *UserAuthCache) IsAuthenticated(username string) bool

IsAuthenticated checks if a user is authenticated Returns true if the user is in the cache and authenticated

func (*UserAuthCache) Set

func (c *UserAuthCache) Set(username string, authenticated bool)

Set adds or updates an entry in the cache

Jump to

Keyboard shortcuts

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