cache

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cache provides a simple, concurrent-safe in-memory cache with expiration support.

Package cache provides a simple, concurrent-safe in-memory cache with expiration support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache stores arbitrary data with expiration time.

func New

func New(defaultDuration time.Duration) *Cache

New creates a new cache that asynchronously cleans expired entries after the given time passes.

func (*Cache) Delete

func (cache *Cache) Delete(key string)

Delete deletes the key and its value from the cache.

func (*Cache) DeleteExpired

func (cache *Cache) DeleteExpired()

func (*Cache) Get

func (cache *Cache) Get(key string) (any, bool)

Get gets the value for the given key.

func (*Cache) Set

func (cache *Cache) Set(key string, value any, duration time.Duration)

Set sets a value for the given key with an expiration duration. If the duration is less than 0, it will be stored forever. If the duration is 0, the default expiration time will be used

type GenericCache added in v1.0.1

type GenericCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

GenericCache stores arbitrary data with expiration time.

func NewGeneric added in v1.0.1

func NewGeneric[K comparable, V any](defaultDuration time.Duration) *GenericCache[K, V]

NewGeneric creates a new cache that asynchronously cleans expired entries after the given time passes.

func (*GenericCache[K, V]) Delete added in v1.0.1

func (cache *GenericCache[K, V]) Delete(key K)

Delete deletes the key and its value from the cache.

func (*GenericCache[K, V]) DeleteExpired added in v1.0.1

func (cache *GenericCache[K, V]) DeleteExpired()

DeleteExpired deletes all expired items from the cache.

func (*GenericCache[K, V]) Get added in v1.0.1

func (cache *GenericCache[K, V]) Get(key K) (V, bool)

Get gets the value for the given key.

func (*GenericCache[K, V]) Set added in v1.0.1

func (cache *GenericCache[K, V]) Set(key K, value V, duration time.Duration)

Set sets a value for the given key with an expiration duration. If the duration is less than 0, it will be stored forever. If the duration is 0, the default expiration time will be used

Jump to

Keyboard shortcuts

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