Documentation
¶
Overview ¶
Package cache provides a simple thread-safe in-memory cache for storing arbitrary values. It uses a global cache store with read-write mutex protection for concurrent access.
Usage ¶
Define cache keys as typed constants:
const (
UserCacheKey cache.LocalCacheKey = "user"
SessionCacheKey cache.LocalCacheKey = "session"
)
Store and retrieve values:
cache.LCSet(UserCacheKey, &User{ID: "123"})
if value, ok := cache.LCGet(UserCacheKey); ok {
user := value.(*User)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LCDel ¶
func LCDel(key LocalCacheKey)
LCDel removes a value from the cache by key. If the key does not exist, this operation is a no-op. This operation is thread-safe.
func LCGet ¶
func LCGet(key LocalCacheKey) (any, bool)
LCGet retrieves a value from the cache by key. Returns the value and true if found, or nil and false if not found. This operation is thread-safe.
func LCHas ¶
func LCHas(key LocalCacheKey) bool
LCHas checks if a key exists in the cache. Returns true if the key exists, false otherwise. This operation is thread-safe.
func LCSet ¶
func LCSet(key LocalCacheKey, value any)
LCSet stores a value in the cache with the given key. If a value already exists for the key, it will be overwritten. This operation is thread-safe.
Types ¶
type LocalCacheKey ¶
type LocalCacheKey string
LocalCacheKey is a typed string for cache keys. Using typed keys helps prevent key collisions and provides better code clarity.