Documentation
¶
Overview ¶
Package cachefunk provides caching wrappers for functions
Index ¶
- Constants
- Variables
- func Cache[Params any, ResultType any](cache *CacheFunk, key string, resolverFunc func(Params) (ResultType, error), ...) (ResultType, error)
- func CacheWithContext[Params any, ResultType any](cache *CacheFunk, key string, ...) (ResultType, error)
- func CacheWithIgnore[Params any, ResultType any](cache *CacheFunk, key string, ...) (ResultType, error)
- func DisableWarnings()
- func EnableWarnings()
- func GetIgnoreCacheFromContext(ctx context.Context) bool
- func SHA256Base64Pather(key string, params string) []string
- func SHA256HexPather(key string, params string) []string
- func SetIgnoreCacheInContext(ctx context.Context, value bool) context.Context
- func SetWarningLog(logger *log.Logger)
- func Wrap[Params any, ResultType any](cache *CacheFunk, key string, retrieveFunc func(Params) (ResultType, error)) func(Params) (ResultType, error)
- func WrapWithContext[Params any, ResultType any](cache *CacheFunk, key string, ...) func(context.Context, Params) (ResultType, error)
- func WrapWithIgnore[Params any, ResultType any](cache *CacheFunk, key string, ...) func(bool, Params) (ResultType, error)
- type BodyCodec
- type CacheEntry
- type CacheFunk
- func (c *CacheFunk) Cleanup(forceCleanupExpired bool)
- func (c *CacheFunk) Clear() error
- func (c *CacheFunk) EntryCount() (int64, error)
- func (c *CacheFunk) ExpiredEntryCount() (int64, error)
- func (c *CacheFunk) Get(key string, config *KeyConfig, params string, value any) error
- func (c *CacheFunk) GetLazy(key string, config *KeyConfig, params string) (LazyLoad, error)
- func (c *CacheFunk) Set(key string, config *KeyConfig, params string, value any) error
- type CacheStorage
- type Compression
- type Config
- type DiskStorage
- func (c *DiskStorage) Cleanup(key string, config *KeyConfig, expireBeforeTime time.Time) error
- func (c *DiskStorage) Clear() error
- func (c *DiskStorage) Dump(n int64)
- func (c *DiskStorage) EntryCount() (int64, error)
- func (c *DiskStorage) ExpiredEntryCount(key string, config *KeyConfig, expireBeforeTime time.Time) (int64, error)
- func (c *DiskStorage) Get(key string, config *KeyConfig, params string, expireBeforeTime time.Time) ([]byte, error)
- func (c *DiskStorage) IterateFiles(basePath string, callback func(string, fs.DirEntry))
- func (c *DiskStorage) Set(key string, config *KeyConfig, params string, value []byte, ...) error
- type DiskStoragePather
- type GORMStorage
- func (c *GORMStorage) Cleanup(key string, config *KeyConfig, expireBeforeTime time.Time) error
- func (c *GORMStorage) Clear() error
- func (c *GORMStorage) Dump(n int64)
- func (c *GORMStorage) EntryCount() (int64, error)
- func (c *GORMStorage) ExpiredEntryCount(key string, config *KeyConfig, expireBeforeTime time.Time) (int64, error)
- func (c *GORMStorage) Get(key string, config *KeyConfig, params string, expireBeforeTime time.Time) ([]byte, error)
- func (c *GORMStorage) Set(key string, config *KeyConfig, params string, value []byte, ...) error
- type InMemoryStorage
- func (c *InMemoryStorage) Cleanup(key string, config *KeyConfig, expireBeforeTime time.Time) error
- func (c *InMemoryStorage) Clear() error
- func (c *InMemoryStorage) Dump(n int64)
- func (c *InMemoryStorage) EntryCount() (int64, error)
- func (c *InMemoryStorage) ExpiredEntryCount(key string, config *KeyConfig, expireBeforeTime time.Time) (int64, error)
- func (c *InMemoryStorage) Get(key string, config *KeyConfig, params string, expireBeforeTime time.Time) ([]byte, error)
- func (c *InMemoryStorage) Set(key string, config *KeyConfig, params string, value []byte, ...) error
- type InMemoryStorageEntry
- type KeyConfig
- func (kc *KeyConfig) DecompressAndUnmarshal(valueData []byte, value any) error
- func (kc *KeyConfig) GetBodyCodec() BodyCodec
- func (kc *KeyConfig) GetBodyCompression() Compression
- func (kc *KeyConfig) GetExpireTime(now time.Time) time.Time
- func (kc *KeyConfig) GetParamCodec() ParamCodec
- func (kc *KeyConfig) GetTimestamp(now time.Time) time.Time
- func (kc *KeyConfig) MarshalAndCompress(value any) ([]byte, error)
- func (kc *KeyConfig) MarshalJSON() ([]byte, error)
- func (kc *KeyConfig) UnmarshalJSON(data []byte) error
- type LazyLoad
- type ParamCodec
- type ParameterEncoder
Examples ¶
Constants ¶
const TTLEntryImmediatelyExpires = 0
TTLEntryImmediatelyExpires means that cached values immediately expire All cache Sets will do nothing and all non-fallback Gets return no result
const TTLEntryNeverExpires = -1
TTLEntryNeverExpires means that cached values will never expire
Variables ¶
var ( ErrEntryNotFound = errors.New("cache entry not found") ErrEntryExpired = errors.New("cache entry expired") )
var BrotliCompression = &brotliCompression{}
var DefaultBodyCodec = JSONCodec
var DefaultBodyCompression = ZstdCompression
var DefaultDiskStoragePather = SHA256HexPather
var DefaultKeyConfig = &KeyConfig{ TTL: 3600, TTLJitter: 300, FallbackToExpired: false, BodyCompression: DefaultBodyCompression, BodyCodec: DefaultBodyCodec, ParamCodec: DefaultParamCodec, }
DefaultKeyConfig holds the settings for any key that does not have config defined
var DefaultParamCodec = JSONParams
var GzipCompression = &gzipCompression{}
var JSONBase64Params = &jsonBase64Params{}
var JSONCodec = &jsonCodec{}
var JSONParams = &jsonParams{}
var MaxDate = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
MaxDate is the latest time that cachefunk uses (9999-01-01)
var MinDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
MinDate is the earliest time that cachefunk uses (1970-01-01)
var MsgPackCodec = &msgpackCodec{}
var NoCompression = &noCompression{}
var StringCodec = &stringCodec{}
var ZstdCompression = &zstdCompression{}
Functions ¶
func CacheWithContext ¶ added in v0.4.0
func CacheWithIgnore ¶ added in v0.4.1
func DisableWarnings ¶ added in v0.4.0
func DisableWarnings()
func EnableWarnings ¶ added in v0.4.0
func EnableWarnings()
func GetIgnoreCacheFromContext ¶ added in v0.4.1
func SHA256Base64Pather ¶ added in v0.4.0
func SHA256HexPather ¶ added in v0.4.0
func SetIgnoreCacheInContext ¶ added in v0.4.1
func SetWarningLog ¶ added in v0.4.0
SetWarningLog allows redirection of warning output
func Wrap ¶ added in v0.1.0
func Wrap[Params any, ResultType any]( cache *CacheFunk, key string, retrieveFunc func(Params) (ResultType, error), ) func(Params) (ResultType, error)
Wrap type functions These don't work with type methods unfortunately
func WrapWithContext ¶ added in v0.2.0
Types ¶
type CacheEntry ¶
type CacheEntry struct {
ID int64 `json:"id" gorm:"primaryKey"`
Timestamp time.Time `json:"timestamp" gorm:"not null"`
Key string `json:"key" gorm:"uniqueIndex:idx_key_params;not null"`
Params string `json:"params" gorm:"uniqueIndex:idx_key_params;not null"`
CompressionType string `json:"compression_type" gorm:"not null"`
Data []byte `json:"data" gorm:"not null"`
}
type CacheFunk ¶ added in v0.4.0
type CacheFunk struct {
Config *Config
Storage CacheStorage
}
func (*CacheFunk) EntryCount ¶ added in v0.4.0
func (*CacheFunk) ExpiredEntryCount ¶ added in v0.4.0
type CacheStorage ¶ added in v0.4.0
type CacheStorage interface {
// Get a value from the cache if it exists
Get(key string, config *KeyConfig, params string, expireBeforeTime time.Time) (value []byte, err error)
// Set a raw value for key in the cache
Set(key string, config *KeyConfig, params string, value []byte, timestamp time.Time) (err error)
// Get the number of entries in the cache
EntryCount() (count int64, err error)
// Get how many entries have expired in the cache compared to expireBeforeTime
ExpiredEntryCount(key string, config *KeyConfig, expireBeforeTime time.Time) (count int64, err error)
// Delete all entries in the cache
Clear() error
// Delete entries that have timestamps in cache before expireBeforeTime
Cleanup(key string, config *KeyConfig, expireBeforeTime time.Time) error
// Print all cached entries for test debugging purposes
Dump(n int64)
}
type Compression ¶ added in v0.4.0
type DiskStorage ¶ added in v0.4.0
type DiskStorage struct {
BasePath string
CalculatePath DiskStoragePather
}
DiskStorage stores cached items on disk in a tree of folders
Example ¶
type HelloWorldParams struct {
Name string
}
helloWorld := func(ignoreCache bool, params *HelloWorldParams) (string, error) {
return "Hello " + params.Name, nil
}
config := &Config{}
storage, err := NewDiskStorage("/path/to/cache", DefaultDiskStoragePather)
if err != nil {
fmt.Printf("Error while creating disk storage: %s\n", err)
return
}
cache := &CacheFunk{
Config: config,
Storage: storage,
}
HelloWorld := WrapWithIgnore(cache, "hello", helloWorld)
params := &HelloWorldParams{
Name: "bob",
}
// First call will get value from wrapped function
value, err := HelloWorld(false, params)
fmt.Println("First call:", value, err)
// Second call will get value from cache
value, err = HelloWorld(false, params)
fmt.Println("Second call:", value, err)
func NewDiskStorage ¶ added in v0.4.0
func NewDiskStorage(basePath string, pather DiskStoragePather) (*DiskStorage, error)
func (*DiskStorage) Cleanup ¶ added in v0.4.0
Cleanup will delete all cache entries that have expired
func (*DiskStorage) Clear ¶ added in v0.4.0
func (c *DiskStorage) Clear() error
Clear will delete all cache entries
func (*DiskStorage) Dump ¶ added in v0.4.0
func (c *DiskStorage) Dump(n int64)
Dump prints out a sample hexdump of cached content
func (*DiskStorage) EntryCount ¶ added in v0.4.0
func (c *DiskStorage) EntryCount() (int64, error)
func (*DiskStorage) ExpiredEntryCount ¶ added in v0.4.0
func (*DiskStorage) IterateFiles ¶ added in v0.4.0
func (c *DiskStorage) IterateFiles(basePath string, callback func(string, fs.DirEntry))
type DiskStoragePather ¶ added in v0.4.0
DiskStoragePather returns a path representing the key and params. The path is broken down into an array of directories to help limit file counts in folders when there are thousands of cache entries.
type GORMStorage ¶ added in v0.4.0
GORMStorage stores cached items in a database table
Example ¶
type HelloWorldParams struct {
Name string
}
helloWorld := func(ignoreCache bool, params *HelloWorldParams) (string, error) {
return "Hello " + params.Name, nil
}
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
config := &Config{}
storage, err := NewGORMStorage(db)
if err != nil {
panic("failed to setup gorm cache storage")
}
cache := &CacheFunk{
Config: config,
Storage: storage,
}
HelloWorld := WrapWithIgnore(cache, "hello", helloWorld)
params := &HelloWorldParams{
Name: "bob",
}
// First call will get value from wrapped function
value, err := HelloWorld(false, params)
fmt.Println("First call:", value, err)
// Second call will get value from cache
value, err = HelloWorld(false, params)
fmt.Println("Second call:", value, err)
func NewGORMStorage ¶ added in v0.4.0
func NewGORMStorage(db *gorm.DB) (*GORMStorage, error)
func (*GORMStorage) Cleanup ¶ added in v0.4.0
Cleanup will delete all cache entries that have expired
func (*GORMStorage) Clear ¶ added in v0.4.0
func (c *GORMStorage) Clear() error
Clear will delete all cache entries
func (*GORMStorage) Dump ¶ added in v0.4.0
func (c *GORMStorage) Dump(n int64)
func (*GORMStorage) EntryCount ¶ added in v0.4.0
func (c *GORMStorage) EntryCount() (int64, error)
func (*GORMStorage) ExpiredEntryCount ¶ added in v0.4.0
type InMemoryStorage ¶ added in v0.4.0
type InMemoryStorage struct {
// contains filtered or unexported fields
}
InMemoryStorage stores cache entries in a map
Example ¶
type HelloWorldParams struct {
Name string
}
helloWorld := func(ignoreCache bool, params *HelloWorldParams) (string, error) {
return "Hello " + params.Name, nil
}
config := &Config{}
cache := &CacheFunk{
Config: config,
Storage: NewInMemoryStorage(),
}
HelloWorld := WrapWithIgnore(cache, "hello", helloWorld)
params := &HelloWorldParams{
Name: "bob",
}
// First call will get value from wrapped function
value, err := HelloWorld(false, params)
fmt.Println("First call:", value, err)
// Second call will get value from cache
value, err = HelloWorld(false, params)
fmt.Println("Second call:", value, err)
func NewInMemoryStorage ¶ added in v0.4.0
func NewInMemoryStorage() *InMemoryStorage
func (*InMemoryStorage) Clear ¶ added in v0.4.0
func (c *InMemoryStorage) Clear() error
func (*InMemoryStorage) Dump ¶ added in v0.4.0
func (c *InMemoryStorage) Dump(n int64)
func (*InMemoryStorage) EntryCount ¶ added in v0.4.0
func (c *InMemoryStorage) EntryCount() (int64, error)
func (*InMemoryStorage) ExpiredEntryCount ¶ added in v0.4.0
type InMemoryStorageEntry ¶ added in v0.4.0
type KeyConfig ¶
type KeyConfig struct {
// TTL is time to live in seconds before the cache value can be deleted
// If TTL is 0, cache value will expire immediately
// If TTL is less than 0, cache value will "never" expire (not until year 9999)
// Some useful TTL values:
// - 3600: one hour
// - 86400: one day
// - 604800: one week
// - 2419200: four weeks
// - 31536000: one year
TTL int64
// When TTLJitter is > 0, a random value from 1 to TTLJitter will be added to TTL
// This spreads cache expire time to stop retrieval of fresh responses all at once
TTLJitter int64
// Use expired entries when a fresh value cannot be successfully retrieved
FallbackToExpired bool
// Settings for transformation of parameters
ParamCodec ParamCodec
// Settings for transformation of body
BodyCodec BodyCodec
BodyCompression Compression
}
KeyConfig is a set of specific settings for a cached endpoint
func (*KeyConfig) DecompressAndUnmarshal ¶ added in v0.4.1
func (*KeyConfig) GetBodyCodec ¶ added in v0.4.0
func (*KeyConfig) GetBodyCompression ¶ added in v0.4.0
func (kc *KeyConfig) GetBodyCompression() Compression
func (*KeyConfig) GetExpireTime ¶ added in v0.4.0
GetExpireTime calculates an expire time from the key config within reasonable bounds for modern filesystems (as of year 2017) any entry with timestamp before the expire time is said to have expired
func (*KeyConfig) GetParamCodec ¶ added in v0.4.0
func (kc *KeyConfig) GetParamCodec() ParamCodec
func (*KeyConfig) GetTimestamp ¶ added in v0.4.0
GetTimestamp the timestamp of a file with TTLJitter applied
func (*KeyConfig) MarshalAndCompress ¶ added in v0.4.1
func (*KeyConfig) MarshalJSON ¶ added in v0.4.0
MarshalJSON helps to marshal the different codecs and compression
func (*KeyConfig) UnmarshalJSON ¶ added in v0.4.0
UnmarshalJSON helps to unmarshal the different codecs and compression