cache

package
v0.0.0-...-1116384 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = fmt.Errorf("key not found")

ErrKeyNotFound is returned when a key is not found in the cache.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Set(ctx context.Context, key string, value string, ttl time.Duration) error
	Get(ctx context.Context, key string) (string, error)
	Del(ctx context.Context, key string) error
	database.Database
}

Cache is a simple key-value store backed by an SQLite database.

func NewCache

func NewCache(ctx context.Context, opts ...Option) (Cache, error)

NewCache creates a new cache instance with the given name and applies any provided options. The cache is backed by an SQLite database.

The cache is automatically created if it does not exist.

Parameters:

  • ctx: the context
  • opts: the cache options

Returns:

  • cache: the cache instance
  • error: an error if the operation failed

Configuration defaults:

  • syncInterval: 1 second
  • timezone: UTC

Configuration options:

  • WithSyncInterval: sets a custom sync interval for the cache.
  • WithPath: sets the path to the cache database.
  • WithTimezone: sets a custom timezone for the cache.
  • WithPurgePercent: sets the percentage of cache entries to purge.
  • WithPurgeTimeout: sets the timeout for purging cache entries.
  • WithDBOptions: sets the database options.

Example:

cache, err := cache.NewCache(ctx)
if err != nil {
	panic(err)
}

type Option

type Option func(*cache)

CacheOption is a function that configures a cache instance.

func WithPath

func WithPath(path string) Option

WithPath sets the path to the cache database. The cache is automatically created if it does not exist.

func WithPurgePercent

func WithPurgePercent(percent float64) Option

WithPurgePercent sets the percentage of cache entries to delete when purging.

func WithPurgeTimeout

func WithPurgeTimeout(timeout time.Duration) Option

WithPurgeTimeout sets the timeout for purging cache entries.

func WithSyncInterval

func WithSyncInterval(interval cron.Interval) Option

WithSyncInterval sets a custom sync interval for the cache. The sync interval determines how often the cache is synchronized with the database.

func WithTimezone

func WithTimezone(timezone *time.Location) Option

WithTimezone sets a custom timezone for the cache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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