cachemanager

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCleanupInterval = 30 * time.Minute
View Source
const DefaultExpiration = 10 * time.Minute

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheManager

type CacheManager[K comparable, V any] interface {
	Get(ctx context.Context, key K) (V, bool)
	GetMultiple(ctx context.Context, keys []K) (map[K]V, bool)
	GetWithRefresh(ctx context.Context, key K, ttl time.Duration) (V, bool)
	Set(ctx context.Context, key K, value V, ttl time.Duration)
	Delete(ctx context.Context, keys ...K) error
	Flush(ctx context.Context) error
}

type CacheManagerMock

type CacheManagerMock[K comparable, V any] struct {
	GetFunc            func(ctx context.Context, key K) (V, bool)
	GetMultipleFunc    func(ctx context.Context, keys []K) (map[K]V, bool)
	GetWithRefreshFunc func(ctx context.Context, key K, ttl time.Duration) (V, bool)
	SetFunc            func(ctx context.Context, key K, value V, ttl time.Duration)
	DeleteFunc         func(ctx context.Context, keys ...K) error
	FlushFunc          func(ctx context.Context) error
}

CacheManagerMock is a mock implementation of CacheManager for testing. This uses the function-field pattern consistent with moq-generated mocks. Note: moq doesn't support generic interfaces, so this is hand-written.

func (*CacheManagerMock[K, V]) Delete

func (m *CacheManagerMock[K, V]) Delete(ctx context.Context, keys ...K) error

func (*CacheManagerMock[K, V]) Flush

func (m *CacheManagerMock[K, V]) Flush(ctx context.Context) error

func (*CacheManagerMock[K, V]) Get

func (m *CacheManagerMock[K, V]) Get(ctx context.Context, key K) (V, bool)

func (*CacheManagerMock[K, V]) GetMultiple

func (m *CacheManagerMock[K, V]) GetMultiple(ctx context.Context, keys []K) (map[K]V, bool)

func (*CacheManagerMock[K, V]) GetWithRefresh

func (m *CacheManagerMock[K, V]) GetWithRefresh(ctx context.Context, key K, ttl time.Duration) (V, bool)

func (*CacheManagerMock[K, V]) Set

func (m *CacheManagerMock[K, V]) Set(ctx context.Context, key K, value V, ttl time.Duration)

type InMemoryCacheManager

type InMemoryCacheManager[K ~string, V any] struct {
	// contains filtered or unexported fields
}

InMemoryCacheManager is the concrete implementation of the CacheManager interface

func NewInMemoryCacheManager

func NewInMemoryCacheManager[K ~string, V any](useCase string, defaultExpiration, cleanupInterval time.Duration) *InMemoryCacheManager[K, V]

NewInMemoryCacheManager initializes the in-memory cache with a default cleanup interval

func (*InMemoryCacheManager[K, V]) Delete

func (c *InMemoryCacheManager[K, V]) Delete(ctx context.Context, keys ...string) error

Delete remove a value in the cache by its key

func (*InMemoryCacheManager[K, V]) Flush

func (c *InMemoryCacheManager[K, V]) Flush(ctx context.Context) error

Delete remove a value in the cache by its key

func (*InMemoryCacheManager[K, V]) Get

func (c *InMemoryCacheManager[K, V]) Get(ctx context.Context, key string) (V, bool)

Get retrieves an item from the cache by its key

func (*InMemoryCacheManager[K, V]) GetMultiple

func (c *InMemoryCacheManager[K, V]) GetMultiple(ctx context.Context, keys []string) (map[string]V, bool)

func (*InMemoryCacheManager[K, V]) GetWithRefresh

func (c *InMemoryCacheManager[K, V]) GetWithRefresh(ctx context.Context, key string, ttl time.Duration) (V, bool)

GetWithRefresh retrieves an item from the cache if one is found we extend the ttl by putting the item back in the cache

func (*InMemoryCacheManager[K, V]) Set

func (c *InMemoryCacheManager[K, V]) Set(ctx context.Context, key string, value V, ttl time.Duration)

Set sets a value in the cache with a key and TTL

Jump to

Keyboard shortcuts

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