memory

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDriver

func NewDriver(storeConfig dgcache.StoreConfig) (cache.Driver, error)

NewDriver creates a new in-memory cache driver.

Types

type Config

type Config struct {
	// MaxItems is the maximum number of items in the cache.
	// 0 means unlimited (default).
	MaxItems int

	// MaxBytes is the maximum total size of cached items in bytes.
	// 0 means unlimited (default).
	MaxBytes int64

	// EvictionPolicy determines how items are evicted when limits are reached.
	// Options: "lru" (default), "lfu", "fifo"
	EvictionPolicy string

	// CleanupInterval is how often expired items are cleaned up.
	// Default: 1 minute
	CleanupInterval time.Duration

	// EnableMetrics enables collection of cache statistics.
	// Default: false
	EnableMetrics bool
}

Config represents the configuration for the memory cache driver.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default memory cache configuration.

func (Config) WithCleanupInterval

func (c Config) WithCleanupInterval(interval time.Duration) Config

WithCleanupInterval sets the cleanup interval.

func (Config) WithEvictionPolicy

func (c Config) WithEvictionPolicy(policy string) Config

WithEvictionPolicy sets the eviction policy.

func (Config) WithMaxBytes

func (c Config) WithMaxBytes(max int64) Config

WithMaxBytes sets the maximum total size in bytes.

func (Config) WithMaxItems

func (c Config) WithMaxItems(max int) Config

WithMaxItems sets the maximum number of items.

func (Config) WithMetrics

func (c Config) WithMetrics(enabled bool) Config

WithMetrics enables or disables metrics collection.

type Driver

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

Driver is an in-memory cache driver.

func (*Driver) Close

func (d *Driver) Close() error

Close closes the driver and releases resources.

func (*Driver) Decrement

func (d *Driver) Decrement(ctx context.Context, key string, value int64) (int64, error)

Decrement decrements the value of a key.

func (*Driver) Flush

func (d *Driver) Flush(ctx context.Context) error

Flush removes all items from the cache.

func (*Driver) FlushTags

func (d *Driver) FlushTags(ctx context.Context, tags ...string) error

FlushTags removes all items associated with the given tags.

func (*Driver) Forever

func (d *Driver) Forever(ctx context.Context, key string, value interface{}) error

Forever stores a value in the cache indefinitely.

func (*Driver) Forget

func (d *Driver) Forget(ctx context.Context, key string) error

Forget removes a value from the cache.

func (*Driver) ForgetMultiple

func (d *Driver) ForgetMultiple(ctx context.Context, keys []string) error

ForgetMultiple removes multiple values from the cache.

func (*Driver) Get

func (d *Driver) Get(ctx context.Context, key string) (interface{}, error)

Get retrieves a value from the cache.

func (*Driver) GetMultiple

func (d *Driver) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)

GetMultiple retrieves multiple values from the cache.

func (*Driver) GetPrefix

func (d *Driver) GetPrefix() string

GetPrefix returns the cache key prefix.

func (*Driver) Has

func (d *Driver) Has(ctx context.Context, key string) (bool, error)

Has checks if a key exists in the cache.

func (*Driver) Increment

func (d *Driver) Increment(ctx context.Context, key string, value int64) (int64, error)

Increment increments the value of a key.

func (*Driver) Missing

func (d *Driver) Missing(ctx context.Context, key string) (bool, error)

Missing checks if a key does not exist in the cache.

func (*Driver) Name

func (d *Driver) Name() string

Name returns the driver name.

func (*Driver) Put

func (d *Driver) Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error

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

func (*Driver) PutMultiple

func (d *Driver) PutMultiple(ctx context.Context, items map[string]interface{}, ttl time.Duration) error

PutMultiple stores multiple values in the cache.

func (*Driver) SetPrefix

func (d *Driver) SetPrefix(prefix string)

SetPrefix sets the cache key prefix.

func (*Driver) Stats

func (d *Driver) Stats() cache.Stats

Stats returns a snapshot of current cache statistics.

func (*Driver) Tags

func (d *Driver) Tags(tags ...string) cache.TaggedStore

Tags returns a new TaggedStore instance with the given tags.

type Metrics

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

Metrics tracks cache statistics.

func (*Metrics) RecordDelete

func (m *Metrics) RecordDelete(bytes int64)

RecordDelete decrements the item count and updates size tracking.

func (*Metrics) RecordEviction

func (m *Metrics) RecordEviction(bytes int64)

RecordEviction increments the eviction counter and updates size tracking.

func (*Metrics) RecordHit

func (m *Metrics) RecordHit()

RecordHit increments the hit counter.

func (*Metrics) RecordMiss

func (m *Metrics) RecordMiss()

RecordMiss increments the miss counter.

func (*Metrics) RecordSet

func (m *Metrics) RecordSet(bytes int64)

RecordSet increments the set counter and updates size tracking.

func (*Metrics) RecordUpdate

func (m *Metrics) RecordUpdate(oldBytes, newBytes int64)

RecordUpdate updates size tracking for an existing item.

func (*Metrics) Reset

func (m *Metrics) Reset()

Reset resets all metrics to zero.

func (*Metrics) Stats

func (m *Metrics) Stats() cache.Stats

Stats returns a snapshot of current cache statistics.

Jump to

Keyboard shortcuts

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