cache

package
v0.32.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShardCount added in v0.32.0

func ShardCount() uint

ShardCount returns the default number of LRU shards for an in-memory cache: shardsPerCPU per available CPU (GOMAXPROCS), capped at maxShards. A single-CPU host returns 1 — it has no lock contention, so it keeps a single LRU identical to the unsharded cache. The expiration cache rounds this up to a power of two.

Types

type ExpiringCache

type ExpiringCache[T any] interface {
	// Put adds the value to the cache unter the passed key with expiration. If expiration <=0, entry will NOT be cached
	Put(key string, val *T, expiration time.Duration)

	// Get returns the value of cached entry with remained TTL. If entry is not cached, returns nil
	Get(key string) (val *T, expiration time.Duration)

	// TotalCount returns the total count of valid (not expired) elements
	TotalCount() int

	// Clear removes all cache entries
	Clear()
}

type MockExpiringCache added in v0.29.0

type MockExpiringCache[T any] struct {
	mock.Mock
}

MockExpiringCache is an autogenerated mock type for the ExpiringCache type

func NewMockExpiringCache added in v0.29.0

func NewMockExpiringCache[T any](t interface {
	mock.TestingT
	Cleanup(func())
}) *MockExpiringCache[T]

NewMockExpiringCache creates a new instance of MockExpiringCache. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockExpiringCache[T]) Clear added in v0.29.0

func (_mock *MockExpiringCache[T]) Clear()

Clear provides a mock function for the type MockExpiringCache

func (*MockExpiringCache[T]) EXPECT added in v0.29.0

func (_m *MockExpiringCache[T]) EXPECT() *MockExpiringCache_Expecter[T]

func (*MockExpiringCache[T]) Get added in v0.29.0

func (_mock *MockExpiringCache[T]) Get(key string) (*T, time.Duration)

Get provides a mock function for the type MockExpiringCache

func (*MockExpiringCache[T]) Put added in v0.29.0

func (_mock *MockExpiringCache[T]) Put(key string, val *T, expiration time.Duration)

Put provides a mock function for the type MockExpiringCache

func (*MockExpiringCache[T]) TotalCount added in v0.29.0

func (_mock *MockExpiringCache[T]) TotalCount() int

TotalCount provides a mock function for the type MockExpiringCache

type MockExpiringCache_Clear_Call added in v0.29.0

type MockExpiringCache_Clear_Call[T any] struct {
	*mock.Call
}

MockExpiringCache_Clear_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Clear'

func (*MockExpiringCache_Clear_Call[T]) Return added in v0.29.0

func (*MockExpiringCache_Clear_Call[T]) Run added in v0.29.0

func (*MockExpiringCache_Clear_Call[T]) RunAndReturn added in v0.29.0

func (_c *MockExpiringCache_Clear_Call[T]) RunAndReturn(run func()) *MockExpiringCache_Clear_Call[T]

type MockExpiringCache_Expecter added in v0.29.0

type MockExpiringCache_Expecter[T any] struct {
	// contains filtered or unexported fields
}

func (*MockExpiringCache_Expecter[T]) Clear added in v0.29.0

Clear is a helper method to define mock.On call

func (*MockExpiringCache_Expecter[T]) Get added in v0.29.0

func (_e *MockExpiringCache_Expecter[T]) Get(key interface{}) *MockExpiringCache_Get_Call[T]

Get is a helper method to define mock.On call

  • key string

func (*MockExpiringCache_Expecter[T]) Put added in v0.29.0

func (_e *MockExpiringCache_Expecter[T]) Put(key interface{}, val interface{}, expiration interface{}) *MockExpiringCache_Put_Call[T]

Put is a helper method to define mock.On call

  • key string
  • val *T
  • expiration time.Duration

func (*MockExpiringCache_Expecter[T]) TotalCount added in v0.29.0

TotalCount is a helper method to define mock.On call

type MockExpiringCache_Get_Call added in v0.29.0

type MockExpiringCache_Get_Call[T any] struct {
	*mock.Call
}

MockExpiringCache_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get'

func (*MockExpiringCache_Get_Call[T]) Return added in v0.29.0

func (_c *MockExpiringCache_Get_Call[T]) Return(val *T, expiration time.Duration) *MockExpiringCache_Get_Call[T]

func (*MockExpiringCache_Get_Call[T]) Run added in v0.29.0

func (_c *MockExpiringCache_Get_Call[T]) Run(run func(key string)) *MockExpiringCache_Get_Call[T]

func (*MockExpiringCache_Get_Call[T]) RunAndReturn added in v0.29.0

func (_c *MockExpiringCache_Get_Call[T]) RunAndReturn(run func(key string) (*T, time.Duration)) *MockExpiringCache_Get_Call[T]

type MockExpiringCache_Put_Call added in v0.29.0

type MockExpiringCache_Put_Call[T any] struct {
	*mock.Call
}

MockExpiringCache_Put_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Put'

func (*MockExpiringCache_Put_Call[T]) Return added in v0.29.0

func (*MockExpiringCache_Put_Call[T]) Run added in v0.29.0

func (_c *MockExpiringCache_Put_Call[T]) Run(run func(key string, val *T, expiration time.Duration)) *MockExpiringCache_Put_Call[T]

func (*MockExpiringCache_Put_Call[T]) RunAndReturn added in v0.29.0

func (_c *MockExpiringCache_Put_Call[T]) RunAndReturn(run func(key string, val *T, expiration time.Duration)) *MockExpiringCache_Put_Call[T]

type MockExpiringCache_TotalCount_Call added in v0.29.0

type MockExpiringCache_TotalCount_Call[T any] struct {
	*mock.Call
}

MockExpiringCache_TotalCount_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'TotalCount'

func (*MockExpiringCache_TotalCount_Call[T]) Return added in v0.29.0

func (*MockExpiringCache_TotalCount_Call[T]) Run added in v0.29.0

func (*MockExpiringCache_TotalCount_Call[T]) RunAndReturn added in v0.29.0

func (_c *MockExpiringCache_TotalCount_Call[T]) RunAndReturn(run func() int) *MockExpiringCache_TotalCount_Call[T]

type RedisExpiringCache added in v0.30.0

type RedisExpiringCache[T any] struct {
	// contains filtered or unexported fields
}

RedisExpiringCache wraps an ExpiringCache[T] with transparent Redis write-through and pub/sub synchronisation across instances.

func NewRedisExpiringByteCache added in v0.30.0

func NewRedisExpiringByteCache(
	ctx context.Context,
	inner ExpiringCache[[]byte],
	client *goredis.Client,
	opts RedisOptions[[]byte],
) (*RedisExpiringCache[[]byte], error)

NewRedisExpiringByteCache creates a RedisExpiringCache for []byte values, using identity encode/decode when no codec is provided.

func NewRedisExpiringCache added in v0.30.0

func NewRedisExpiringCache[T any](
	ctx context.Context,
	inner ExpiringCache[T],
	client *goredis.Client,
	opts RedisOptions[T],
) (*RedisExpiringCache[T], error)

NewRedisExpiringCache creates a new RedisExpiringCache decorator.

It performs a blocking startup scan of existing Redis keys and loads them into inner before launching the background writer and subscriber goroutines. The goroutines run until ctx is cancelled.

func (*RedisExpiringCache[T]) Clear added in v0.30.0

func (c *RedisExpiringCache[T]) Clear()

Clear removes all entries from the inner cache and from Redis (all keys matching the configured prefix).

func (*RedisExpiringCache[T]) Get added in v0.30.0

func (c *RedisExpiringCache[T]) Get(key string) (*T, time.Duration)

Get reads from the inner cache only.

func (*RedisExpiringCache[T]) Put added in v0.30.0

func (c *RedisExpiringCache[T]) Put(key string, val *T, expiration time.Duration)

Put stores the value in the inner cache immediately, then enqueues a non-blocking send to Redis. If the send buffer is full the entry is dropped and a warning is logged – the DNS path is never blocked.

func (*RedisExpiringCache[T]) TotalCount added in v0.30.0

func (c *RedisExpiringCache[T]) TotalCount() int

TotalCount delegates to the inner cache.

type RedisOptions added in v0.30.0

type RedisOptions[T any] struct {
	// Prefix is prepended to all Redis keys.
	Prefix string
	// Channel is the pub/sub channel name used for cross-instance sync.
	Channel string
	// Encode serializes a cache value to bytes.
	Encode func(*T) ([]byte, error)
	// Decode deserializes bytes back to a cache value.
	Decode func([]byte) (*T, error)
	// BatchSize is the maximum number of entries per Redis pipeline flush.
	BatchSize int
	// FlushInterval is the maximum time between flushes.
	FlushInterval time.Duration
	// SendBufSize is the capacity of the internal send buffer channel.
	SendBufSize int
}

RedisOptions configures the RedisExpiringCache decorator.

type ReloadPublishable added in v0.31.0

type ReloadPublishable[T any] interface {
	SetReloadPublisher(publish func(key string, val *T, ttl time.Duration))
}

ReloadPublishable is implemented by inner caches that reload entries internally (e.g. the prefetching cache). The Redis decorator wires its write-through publish in so those reloaded entries are synced like any other Put, without the inner cache or its callers needing to know about Redis. Implementations should add a compile-time assertion against this interface so a signature drift fails to build instead of silently disabling Redis sync of reloaded entries.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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