cache

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides caching for parsed schemas and query ASTs.

Package cache provides caching infrastructure for db-catalyst.

The cache package defines a generic Cache interface and provides implementations for in-memory caching. It's used to speed up incremental builds by caching parsed schemas and query analyses.

Usage:

c := cache.NewMemoryCache()
c.Set(ctx, "key", value, time.Hour)
if val, ok := c.Get(ctx, "key"); ok {
    // use cached value
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeKey

func ComputeKey(content []byte) string

ComputeKey generates a cache key from content using SHA-256.

func ComputeKeyWithPrefix

func ComputeKeyWithPrefix(prefix string, content []byte) string

ComputeKeyWithPrefix generates a cache key with a prefix.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string) (any, bool)
	Set(ctx context.Context, key string, value any, ttl time.Duration)
	Delete(ctx context.Context, key string)
	Clear(ctx context.Context)
}

Cache provides a generic caching interface.

type Entry

type Entry struct {
	Value     any
	ExpiresAt time.Time
}

Entry represents a cached entry with expiration.

func (*Entry) IsExpired

func (e *Entry) IsExpired() bool

IsExpired returns true if the entry has expired.

type FileCache

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

FileCache implements Cache using file system storage. It stores cache entries as JSON files in a directory structure.

func NewFileCache

func NewFileCache(baseDir string) (*FileCache, error)

NewFileCache creates a new file-based cache. The baseDir is where cache files will be stored.

func (*FileCache) Cleanup

func (f *FileCache) Cleanup()

Cleanup removes expired entries.

func (*FileCache) Clear

func (f *FileCache) Clear(_ context.Context)

Clear removes all values from the cache.

func (*FileCache) Delete

func (f *FileCache) Delete(_ context.Context, key string)

Delete removes a value from the cache.

func (*FileCache) Get

func (f *FileCache) Get(_ context.Context, key string) (any, bool)

Get retrieves a value from the cache.

func (*FileCache) Set

func (f *FileCache) Set(_ context.Context, key string, value any, ttl time.Duration)

Set stores a value in the cache with the given TTL.

func (*FileCache) Stats

func (f *FileCache) Stats() (total int, expired int, size int64)

Stats returns statistics about the cache.

type Keyable

type Keyable interface {
	CacheKey() string
}

Keyable is implemented by types that can provide a cache key.

type MemoryCache

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

MemoryCache implements Cache using in-memory storage.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new in-memory cache.

func (*MemoryCache) Cleanup

func (m *MemoryCache) Cleanup()

Cleanup removes expired entries.

func (*MemoryCache) Clear

func (m *MemoryCache) Clear(_ context.Context)

Clear removes all values from the cache.

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(_ context.Context, key string)

Delete removes a value from the cache.

func (*MemoryCache) Get

func (m *MemoryCache) Get(_ context.Context, key string) (any, bool)

Get retrieves a value from the cache.

func (*MemoryCache) Len

func (m *MemoryCache) Len() int

Len returns the number of items in the cache (including expired).

func (*MemoryCache) Set

func (m *MemoryCache) Set(_ context.Context, key string, value any, ttl time.Duration)

Set stores a value in the cache with the given TTL.

Jump to

Keyboard shortcuts

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