Documentation ¶
Overview ¶
This library gives you wrapper functions to cache function output in golang
Index ¶
- Variables
- func CacheObject[Params any, ResultType any](cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error), ...) (ResultType, error)
- func CacheObjectWithContext[Params any, ResultType any](cache Cache, key string, ...) (ResultType, error)
- func CacheString[Params any, ResultType string | []byte](cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error), ...) (ResultType, error)
- func CacheStringWithContext[Params any, ResultType string | []byte](cache Cache, key string, ...) (ResultType, error)
- func DefaultCalculatePath(cacheKey string, params string) []string
- func RenderParameters(params interface{}) (string, error)
- func WrapObject[Params any, ResultType any](cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error)) func(bool, Params) (ResultType, error)
- func WrapObjectWithContext[Params any, ResultType any](cache Cache, key string, ...) func(context.Context, Params) (ResultType, error)
- func WrapString[Params any, ResultType string | []byte](cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error)) func(bool, Params) (ResultType, error)
- func WrapStringWithContext[Params any, ResultType string | []byte](cache Cache, key string, ...) func(context.Context, Params) (ResultType, error)
- type Cache
- type CacheEntry
- type CacheFunkConfig
- type CtxKey
- type DiskCache
- func (c *DiskCache) Cleanup()
- func (c *DiskCache) Clear()
- func (c *DiskCache) EntryCount() int64
- func (c *DiskCache) ExpiredEntryCount() int64
- func (c *DiskCache) Get(key string, params string) ([]byte, bool)
- func (c *DiskCache) GetIgnoreCacheCtxKey() CtxKey
- func (c *DiskCache) IterateFiles(basePath string, callback func(string, fs.DirEntry))
- func (c *DiskCache) Set(key string, params string, value []byte)
- func (c *DiskCache) SetConfig(config *CacheFunkConfig)
- func (c *DiskCache) SetRaw(key string, params string, value []byte, timestamp time.Time, ...)
- type GORMCache
- func (c *GORMCache) Cleanup()
- func (c *GORMCache) Clear()
- func (c *GORMCache) EntryCount() int64
- func (c *GORMCache) ExpiredEntryCount() int64
- func (c *GORMCache) Get(key string, params string) ([]byte, bool)
- func (c *GORMCache) GetIgnoreCacheCtxKey() CtxKey
- func (c *GORMCache) Set(key string, params string, value []byte)
- func (c *GORMCache) SetConfig(config *CacheFunkConfig)
- func (c *GORMCache) SetRaw(key string, params string, value []byte, timestamp time.Time, ...)
- type InMemoryCache
- func (c *InMemoryCache) Cleanup()
- func (c *InMemoryCache) Clear()
- func (c *InMemoryCache) EntryCount() int64
- func (c *InMemoryCache) ExpiredEntryCount() int64
- func (c *InMemoryCache) Get(key string, params string) ([]byte, bool)
- func (c *InMemoryCache) GetIgnoreCacheCtxKey() CtxKey
- func (c *InMemoryCache) Set(key string, params string, value []byte)
- func (c *InMemoryCache) SetConfig(config *CacheFunkConfig)
- func (c *InMemoryCache) SetRaw(key string, params string, value []byte, timestamp time.Time, ...)
- type InMemoryCacheEntry
- type KeyConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DEFAULT_KEYCONFIG = &KeyConfig{ TTL: 3600, TTLJitter: 300, UseCompression: true, }
Functions ¶
func CacheObject ¶
func CacheObject[Params any, ResultType any]( cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error), ignoreCache bool, params Params, ) (ResultType, error)
CacheObject is a function wrapper that caches responses of any json serializable type.
func CacheObjectWithContext ¶
func CacheObjectWithContext[Params any, ResultType any]( cache Cache, key string, retrieveFunc func(ctx context.Context, params Params) (ResultType, error), ctx context.Context, params Params, ) (ResultType, error)
CacheWithContext caches responses of any json serializable type.
func CacheString ¶
func CacheStringWithContext ¶
func CacheStringWithContext[Params any, ResultType string | []byte]( cache Cache, key string, retrieveFunc func(ctx context.Context, params Params) (ResultType, error), ctx context.Context, params Params, ) (ResultType, error)
CacheWithStringContext caches string or []byte responses.
func DefaultCalculatePath ¶
Returns the
func RenderParameters ¶
renderParameters returns a string representation of params
func WrapObject ¶
func WrapObject[Params any, ResultType any]( cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error), ) func(bool, Params) (ResultType, error)
WrapObjects is a function wrapper that caches responses of any json serializable type.
func WrapObjectWithContext ¶
func WrapObjectWithContext[Params any, ResultType any]( cache Cache, key string, retrieveFunc func(context.Context, Params) (ResultType, error), ) func(context.Context, Params) (ResultType, error)
WrapObjectWithContext is a function wrapper that caches responses of any json serializable type.
func WrapString ¶
func WrapString[Params any, ResultType string | []byte]( cache Cache, key string, retrieveFunc func(bool, Params) (ResultType, error), ) func(bool, Params) (ResultType, error)
WrapString is a function wrapper that caches string or []byte responses.
func WrapStringWithContext ¶
func WrapStringWithContext[Params any, ResultType string | []byte]( cache Cache, key string, retrieveFunc func(context.Context, Params) (ResultType, error), ) func(context.Context, Params) (ResultType, error)
WrapStringWithContext is a function wrapper that caches string or []byte responses.
Types ¶
type Cache ¶
type Cache interface { SetConfig(config *CacheFunkConfig) // Get a value from the cache if it exists Get(key string, params string) (value []byte, found bool) // Set a value in the cache Set(key string, params string, value []byte) // Set a raw value for key in the cache SetRaw(key string, params string, value []byte, timestamp time.Time, isCompressed bool) // Get the number of entries in the cache EntryCount() int64 // Get how many entries have expired in the cache compared to cutoff // entries expiry compared to utc now if cutoff is nil ExpiredEntryCount() int64 // Delete all entries in the cache Clear() // Delete entries that have timestamps in cache before cutoff // entries expiry compared to utc now if cutoff is nil Cleanup() // GetIgnoreCacheCtxKey returns Value key under which ignoreCache is stored GetIgnoreCacheCtxKey() CtxKey }
Cache is an interface that supports get/set of values by key
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"` IsCompressed bool `json:"is_compressed" gorm:"default:false;not null"` Data []byte `json:"data" gorm:"not null"` }
type CacheFunkConfig ¶
func (*CacheFunkConfig) Get ¶
func (c *CacheFunkConfig) Get(key string) *KeyConfig
type DiskCache ¶
type DiskCache struct { CacheConfig *CacheFunkConfig BasePath string CalculatePath func(cacheKey string, params string) []string IgnoreCacheCtxKey CtxKey }
Example ¶
package main import ( "fmt" "github.com/rohfle/cachefunk" ) func main() { type HelloWorldParams struct { Name string } helloWorld := func(ignoreCache bool, params *HelloWorldParams) (string, error) { return "Hello " + params.Name, nil } cache := cachefunk.NewDiskCache("/path/to/cache") HelloWorld := cachefunk.WrapString(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) }
Output:
func NewDiskCache ¶
func (*DiskCache) Cleanup ¶
func (c *DiskCache) Cleanup()
Cleanup will delete all cache entries that have expired
func (*DiskCache) EntryCount ¶
func (*DiskCache) ExpiredEntryCount ¶
func (*DiskCache) GetIgnoreCacheCtxKey ¶
func (*DiskCache) IterateFiles ¶
func (*DiskCache) SetConfig ¶
func (c *DiskCache) SetConfig(config *CacheFunkConfig)
type GORMCache ¶
type GORMCache struct { CacheConfig *CacheFunkConfig DB *gorm.DB IgnoreCacheCtxKey CtxKey }
Example ¶
package main import ( "fmt" "github.com/rohfle/cachefunk" "gorm.io/driver/sqlite" "gorm.io/gorm" ) func main() { 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") } cache := cachefunk.NewGORMCache(db) HelloWorld := cachefunk.WrapString(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) }
Output:
func NewGORMCache ¶
func (*GORMCache) Cleanup ¶
func (c *GORMCache) Cleanup()
Cleanup will delete all cache entries that have expired
func (*GORMCache) EntryCount ¶
func (*GORMCache) ExpiredEntryCount ¶
func (*GORMCache) GetIgnoreCacheCtxKey ¶
func (*GORMCache) SetConfig ¶
func (c *GORMCache) SetConfig(config *CacheFunkConfig)
type InMemoryCache ¶
type InMemoryCache struct { CacheConfig *CacheFunkConfig Store map[string]*InMemoryCacheEntry IgnoreCacheCtxKey CtxKey }
Example ¶
package main import ( "fmt" "github.com/rohfle/cachefunk" ) func main() { type HelloWorldParams struct { Name string } helloWorld := func(ignoreCache bool, params *HelloWorldParams) (string, error) { return "Hello " + params.Name, nil } cache := cachefunk.NewInMemoryCache() HelloWorld := cachefunk.WrapString(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) }
Output:
func NewInMemoryCache ¶
func NewInMemoryCache() *InMemoryCache
func (*InMemoryCache) Cleanup ¶
func (c *InMemoryCache) Cleanup()
func (*InMemoryCache) Clear ¶
func (c *InMemoryCache) Clear()
func (*InMemoryCache) EntryCount ¶
func (c *InMemoryCache) EntryCount() int64
func (*InMemoryCache) ExpiredEntryCount ¶
func (c *InMemoryCache) ExpiredEntryCount() int64
func (*InMemoryCache) GetIgnoreCacheCtxKey ¶
func (c *InMemoryCache) GetIgnoreCacheCtxKey() CtxKey
func (*InMemoryCache) SetConfig ¶
func (c *InMemoryCache) SetConfig(config *CacheFunkConfig)
type InMemoryCacheEntry ¶
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 // Use a very large TTL to make the cached value last a long time // (for instance 31536000 will cache for one year) TTL int64 // When TTLJitter is > 0, a random value from 1 to TTLJitter will be added to TTL // This spreads cache expiry out to stop getting fresh responses all at once TTLJitter int64 // Enable compression of data by gzip UseCompression bool }
Config is used to configure the caching wrapper functions