cache

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixSecret      = "secret:v1:"
	PrefixCustomer    = "customer:v1:"
	PrefixUser        = "user:v1:"
	PrefixTenant      = "tenant:v1:"
	PrefixPlan        = "plan:v1:"
	PrefixPrice       = "price:v1:"
	PrefixMeter       = "meter:v1:"
	PrefixEvent       = "event:v1:"
	PrefixWallet      = "wallet:v1:"
	PrefixInvoice     = "invoice:v1:"
	PrefixFeature     = "feature:v1:"
	PrefixEntitlement = "entitlement:v1:"
)

Predefined cache key prefixes for different entity types

View Source
const DefaultCleanupInterval = 1 * time.Hour

DefaultCleanupInterval is how often expired items are removed from the cache

View Source
const DefaultExpiration = 30 * time.Minute

DefaultExpiration is the default expiration time for cache entries

View Source
const (
	ExpiryDefaultInMemory = 30 * time.Minute
)

Variables

This section is empty.

Functions

func GenerateKey

func GenerateKey(prefix string, params ...interface{}) string

GenerateKey creates a cache key from a prefix and a set of parameters It joins all parameters with a colon and appends them to the prefix

func InitializeInMemoryCache

func InitializeInMemoryCache()

InitializeInMemoryCache initializes the global cache instance

Types

type Cache

type Cache interface {
	// Get retrieves a value from the cache
	// Returns the value and a boolean indicating whether the key was found
	Get(ctx context.Context, key string) (interface{}, bool)

	// Set adds a value to the cache with the specified expiration
	// If expiration is 0, the item never expires (but may be evicted)
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration)

	// Delete removes a key from the cache
	Delete(ctx context.Context, key string)

	// DeleteByPrefix removes all keys with the given prefix
	DeleteByPrefix(ctx context.Context, prefix string)

	// Flush removes all items from the cache
	Flush(ctx context.Context)
}

Cache defines the interface for caching operations

type InMemoryCache

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

InMemoryCache implements the Cache interface using github.com/patrickmn/go-cache

func GetInMemoryCache

func GetInMemoryCache() *InMemoryCache

GetCache returns the global cache instance

func Initialize

func Initialize(log *logger.Logger) *InMemoryCache

Initialize initializes the cache system

func (*InMemoryCache) Delete

func (c *InMemoryCache) Delete(_ context.Context, key string)

Delete removes a key from the cache

func (*InMemoryCache) DeleteByPrefix

func (c *InMemoryCache) DeleteByPrefix(_ context.Context, prefix string)

DeleteByPrefix removes all keys with the given prefix

func (*InMemoryCache) Flush

func (c *InMemoryCache) Flush(_ context.Context)

Flush removes all items from the cache

func (*InMemoryCache) Get

func (c *InMemoryCache) Get(_ context.Context, key string) (interface{}, bool)

Get retrieves a value from the cache

func (*InMemoryCache) Set

func (c *InMemoryCache) Set(_ context.Context, key string, value interface{}, expiration time.Duration)

Set adds a value to the cache with the specified expiration

Jump to

Keyboard shortcuts

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