Documentation
¶
Index ¶
- type Cache
- type DatabaseOperations
- type GenericCache
- type MockDatabaseOperations
- func (m *MockDatabaseOperations[K, V]) Delete(ctx context.Context, key K) error
- func (m *MockDatabaseOperations[K, V]) GetDeleteCallCount() int
- func (m *MockDatabaseOperations[K, V]) GetLastDeleteCall() ...
- func (m *MockDatabaseOperations[K, V]) GetLastPersistCall() ...
- func (m *MockDatabaseOperations[K, V]) GetLoadAllCallCount() int
- func (m *MockDatabaseOperations[K, V]) GetPersistCallCount() int
- func (m *MockDatabaseOperations[K, V]) LoadAll(ctx context.Context) (map[K]V, error)
- func (m *MockDatabaseOperations[K, V]) Persist(ctx context.Context, key K, value V) error
- func (m *MockDatabaseOperations[K, V]) Reset()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { // Load bulk-loads all data from storage into memory at startup Load(ctx context.Context) error // Get retrieves a value from the cache (RAM only, never queries DB) Get(key K) (V, bool) // Set updates the cache immediately and persists to DB asynchronously Set(key K, value V) error // Delete removes from cache immediately and persists deletion asynchronously Delete(key K) error // List returns all cached values as a slice List() []V // Flush waits for all pending async writes to complete (call on shutdown) Flush() error }
Cache provides a generic interface for caching any type of data. K is the key type (must be comparable), V is the value type. All operations are thread-safe via sync.Map.
Design principles: - RAM-first: All reads from memory (no DB queries) - Async writes: Updates happen immediately in cache, persisted asynchronously - Bulk load: Load all data from DB at startup - Flush on shutdown: Ensure all pending writes complete gracefully
type DatabaseOperations ¶
type DatabaseOperations[K comparable, V any] interface { // LoadAll loads all entries from the database LoadAll(ctx context.Context) (map[K]V, error) // Persist saves a single entry to the database Persist(ctx context.Context, key K, value V) error // Delete removes a single entry from the database Delete(ctx context.Context, key K) error }
DatabaseOperations defines the interface for database operations required by the cache. This interface allows for easy mocking in tests using gomock.
type GenericCache ¶
type GenericCache[K comparable, V any] struct { // contains filtered or unexported fields }
GenericCache implements Cache[K, V] using sync.Map for thread-safe storage and a DatabaseOperations interface for database operations.
func NewGenericCache ¶
func NewGenericCache[K comparable, V any]( dbOps DatabaseOperations[K, V], ) *GenericCache[K, V]
NewGenericCache creates a new cache with the provided database operations.
Parameters:
- dbOps: Implementation of DatabaseOperations for loading, persisting, and deleting data
func (*GenericCache[K, V]) Delete ¶
func (c *GenericCache[K, V]) Delete(key K) error
Delete removes from cache immediately (synchronous) and persists deletion to the database asynchronously (non-blocking).
func (*GenericCache[K, V]) Flush ¶
func (c *GenericCache[K, V]) Flush() error
Flush blocks until all pending async writes complete. Should be called during graceful shutdown to ensure no data loss.
func (*GenericCache[K, V]) Get ¶
func (c *GenericCache[K, V]) Get(key K) (V, bool)
Get retrieves a value from the cache. Returns (value, true) if found, or (zero value, false) if not found. Never queries the database.
func (*GenericCache[K, V]) List ¶
func (c *GenericCache[K, V]) List() []V
List returns all values currently in the cache as a slice. The order is not guaranteed.
func (*GenericCache[K, V]) Load ¶
func (c *GenericCache[K, V]) Load(ctx context.Context) error
Load bulk-loads all data from the database into memory. Should be called once at application startup.
func (*GenericCache[K, V]) Set ¶
func (c *GenericCache[K, V]) Set(key K, value V) error
Set updates the cache immediately (synchronous) and persists to the database asynchronously (non-blocking). Returns immediately without waiting for DB write.
type MockDatabaseOperations ¶
type MockDatabaseOperations[K comparable, V any] struct { // Behavior configuration LoadAllFunc func(ctx context.Context) (map[K]V, error) PersistFunc func(ctx context.Context, key K, value V) error DeleteFunc func(ctx context.Context, key K) error // Call tracking LoadAllCalls []context.Context PersistCalls []struct { Ctx context.Context Key K Value V } DeleteCalls []struct { Ctx context.Context Key K } // contains filtered or unexported fields }
MockDatabaseOperations is a mock implementation of DatabaseOperations for testing. It's generic and thread-safe, suitable for testing GenericCache.
func NewMockDatabaseOperations ¶
func NewMockDatabaseOperations[K comparable, V any]() *MockDatabaseOperations[K, V]
NewMockDatabaseOperations creates a new mock with default no-op implementations.
func (*MockDatabaseOperations[K, V]) Delete ¶
func (m *MockDatabaseOperations[K, V]) Delete(ctx context.Context, key K) error
Delete implements DatabaseOperations.Delete
func (*MockDatabaseOperations[K, V]) GetDeleteCallCount ¶
func (m *MockDatabaseOperations[K, V]) GetDeleteCallCount() int
GetDeleteCallCount returns the number of times Delete was called
func (*MockDatabaseOperations[K, V]) GetLastDeleteCall ¶
func (m *MockDatabaseOperations[K, V]) GetLastDeleteCall() *struct { Ctx context.Context Key K }
GetLastDeleteCall returns the last call to Delete, or nil if none
func (*MockDatabaseOperations[K, V]) GetLastPersistCall ¶
func (m *MockDatabaseOperations[K, V]) GetLastPersistCall() *struct { Ctx context.Context Key K Value V }
GetLastPersistCall returns the last call to Persist, or nil if none
func (*MockDatabaseOperations[K, V]) GetLoadAllCallCount ¶
func (m *MockDatabaseOperations[K, V]) GetLoadAllCallCount() int
GetLoadAllCallCount returns the number of times LoadAll was called
func (*MockDatabaseOperations[K, V]) GetPersistCallCount ¶
func (m *MockDatabaseOperations[K, V]) GetPersistCallCount() int
GetPersistCallCount returns the number of times Persist was called
func (*MockDatabaseOperations[K, V]) LoadAll ¶
func (m *MockDatabaseOperations[K, V]) LoadAll(ctx context.Context) (map[K]V, error)
LoadAll implements DatabaseOperations.LoadAll
func (*MockDatabaseOperations[K, V]) Persist ¶
func (m *MockDatabaseOperations[K, V]) Persist(ctx context.Context, key K, value V) error
Persist implements DatabaseOperations.Persist
func (*MockDatabaseOperations[K, V]) Reset ¶
func (m *MockDatabaseOperations[K, V]) Reset()
Reset clears all call tracking