cache

package
v1.1.59 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixSecret                   = "secret:v1:"
	PrefixCustomer                 = "customer:v1:"
	PrefixUser                     = "user:v1:"
	PrefixTenant                   = "tenant:v1:"
	PrefixPlan                     = "plan:v1:"
	PrefixSubscription             = "subscription:v1:"
	PrefixPrice                    = "price:v1:"
	PrefixMeter                    = "meter:v1:"
	PrefixEvent                    = "event:v1:"
	PrefixWallet                   = "wallet:v1:"
	PrefixInvoice                  = "invoice:v1:"
	PrefixFeature                  = "feature:v1:"
	PrefixEntitlement              = "entitlement:v1:"
	PrefixPayment                  = "payment:v1:"
	PrefixCreditGrantApplication   = "creditgrantapplication:v1:"
	PrefixCreditNote               = "creditnote:v1:"
	PrefixTaxRate                  = "taxrate:v1:"
	PrefixTaxAssociation           = "taxassociation:v1:"
	PrefixTaxApplied               = "taxapplied:v1:"
	PrefixCoupon                   = "coupon:v1:"
	PrefixCouponAssociation        = "couponassociation:v1:"
	PrefixCouponApplication        = "couponapplication:v1:"
	PrefixAddon                    = "addon:v1:"
	PrefixAddonAssociation         = "addonassociation:v1:"
	PrefixEntityIntegrationMapping = "entity_integration_mapping:v1:"
	PrefixConnection               = "connection:v1:"
	PrefixSettings                 = "settings:v1:"
	PrefixSubscriptionLineItem     = "subscription_line_item:v1:"
	PrefixWalletAlertThrottle      = "wallet_alert_throttle:v1:"
	PrefixCostsheet                = "costsheet:v1:"
	PrefixPriceUnit                = "price_unit:v1:"
	PrefixWalletRealTimeBalance    = "wallet_realtime_balance:v1:"
)

Predefined cache key prefixes for different entity types

View Source
const (
	ExpiryDefaultInMemory = 30 * time.Minute
	ExpiryDefaultRedis    = 30 * time.Minute
)
View Source
const (
	// DeleteRetryDelay specifies how long to wait before retrying a failed delete operation
	DeleteRetryDelay = 100 * time.Millisecond

	// ScanCount determines how many keys to scan at once when using SCAN
	ScanCount = 100
)
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

Variables

This section is empty.

Functions

func FinishSpan added in v1.0.17

func FinishSpan(span *sentry.Span)

FinishSpan safely finishes a span, handling nil spans

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

func InitializeRedisCache added in v1.0.60

func InitializeRedisCache()

InitializeRedisCache initializes the global Redis cache instance

func SetSpanError added in v1.0.17

func SetSpanError(span *sentry.Span, err error)

SetSpanError marks a span as failed and adds error information

func SetSpanSuccess added in v1.0.17

func SetSpanSuccess(span *sentry.Span)

SetSpanSuccess marks a span as successful

func StartCacheSpan added in v1.0.17

func StartCacheSpan(ctx context.Context, cache, operation string, params map[string]interface{}) *sentry.Span

StartCache Span creates a new span for a cache operation Returns nil if Sentry is not available in the context

func UnmarshalCacheValue added in v1.0.60

func UnmarshalCacheValue[T any](value interface{}) (*T, bool)

UnmarshalCacheValue attempts to convert a cache value to the specified type. It handles both in-memory cache (which stores actual objects) and Redis cache (which stores JSON strings). Returns the typed value and true if successful, nil and false otherwise.

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)

	// ForceCacheGet retrieves a value from the cache without checking if the cache is enabled
	ForceCacheGet(ctx context.Context, key string) (interface{}, bool)

	// ForceCacheSet adds a value to the cache without checking if the cache is enabled
	ForceCacheSet(ctx context.Context, key string, value interface{}, expiration time.Duration)
}

Cache defines the interface for caching operations

func Initialize

func Initialize(config *config.Configuration, log *logger.Logger) Cache

Initialize initializes the cache system based on the specified type

func NewInMemoryCache added in v1.0.17

func NewInMemoryCache() Cache

NewInMemoryCache creates a new InMemoryCache instance

type CacheType added in v1.0.60

type CacheType string

CacheType represents the type of cache to use

const (
	// CacheTypeInMemory represents an in-memory cache
	CacheTypeInMemory CacheType = "inmemory"

	// CacheTypeRedis represents a Redis-backed cache
	CacheTypeRedis CacheType = "redis"
)

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 (*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) ForceCacheGet added in v1.0.47

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

func (*InMemoryCache) ForceCacheSet added in v1.0.47

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

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

type RedisCache added in v1.0.60

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

RedisCache implements the Cache interface using Redis

func GetRedisCache added in v1.0.60

func GetRedisCache() *RedisCache

GetRedisCache returns the global Redis cache instance

func NewRedisCache added in v1.0.60

func NewRedisCache() *RedisCache

NewRedisCache creates a new Redis cache

func (*RedisCache) Delete added in v1.0.60

func (c *RedisCache) Delete(ctx context.Context, key string)

Delete removes a key from the cache with retry

func (*RedisCache) DeleteByPrefix added in v1.0.60

func (c *RedisCache) DeleteByPrefix(ctx context.Context, prefix string)

DeleteByPrefix removes all keys with the given prefix

func (*RedisCache) Flush added in v1.0.60

func (c *RedisCache) Flush(ctx context.Context)

Flush removes all items from the cache

func (*RedisCache) ForceCacheGet added in v1.0.60

func (c *RedisCache) ForceCacheGet(ctx context.Context, key string) (interface{}, bool)

Get value from cache bypassing configuration checks

func (*RedisCache) ForceCacheSet added in v1.0.60

func (c *RedisCache) ForceCacheSet(ctx context.Context, key string, value interface{}, expiration time.Duration)

Set value from cache bypassing configuration checks

func (*RedisCache) Get added in v1.0.60

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

Get retrieves a value from the cache

func (*RedisCache) GetRedisKey added in v1.0.60

func (c *RedisCache) GetRedisKey(key string) string

Helper function to add prefix to key

func (*RedisCache) Set added in v1.0.60

func (c *RedisCache) Set(ctx 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