Documentation
¶
Overview ¶
This library gives you wrapper functions to cache function output in golang
Index ¶
- func Wrap[Params any, ResultType any](retrieveFunc func(bool, Params) (ResultType, error), cache Cache, ...) func(bool, Params) (ResultType, error)
- func WrapString[Params any, ResultType string | []byte](retrieveFunc func(bool, Params) (ResultType, error), cache Cache, ...) func(bool, Params) (ResultType, error)
- func WrapStringWithContext[Params any, ResultType string | []byte](retrieveFunc func(context.Context, Params) (ResultType, error), cache Cache, ...) func(context.Context, Params) (ResultType, error)
- func WrapWithContext[Params any, ResultType any](retrieveFunc func(context.Context, Params) (ResultType, error), cache Cache, ...) func(context.Context, Params) (ResultType, error)
- type Cache
- type CacheEntry
- type Config
- type CtxKey
- type GORMCache
- func (c *GORMCache) Cleanup(cutoff *time.Time)
- func (c *GORMCache) Clear()
- func (c *GORMCache) EntryCount() int64
- func (c *GORMCache) ExpiredEntryCount(cutoff *time.Time) int64
- func (c *GORMCache) Get(config *Config, params string) ([]byte, bool)
- func (c *GORMCache) GetIgnoreCacheCtxKey() CtxKey
- func (c *GORMCache) Set(config *Config, params string, value []byte)
- type InMemoryCache
- func (c *InMemoryCache) Cleanup(cutoff *time.Time)
- func (c *InMemoryCache) Clear()
- func (c *InMemoryCache) EntryCount() int64
- func (c *InMemoryCache) ExpiredEntryCount(cutoff *time.Time) int64
- func (c *InMemoryCache) Get(config *Config, params string) ([]byte, bool)
- func (c *InMemoryCache) GetIgnoreCacheCtxKey() CtxKey
- func (c *InMemoryCache) Set(config *Config, params string, value []byte)
- type InMemoryCacheEntry
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Wrap ¶ added in v0.1.0
func Wrap[Params any, ResultType any]( retrieveFunc func(bool, Params) (ResultType, error), cache Cache, config Config, ) func(bool, Params) (ResultType, error)
Wrap is a function wrapper that caches responses of any json serializable type.
func WrapString ¶
func WrapString[Params any, ResultType string | []byte]( retrieveFunc func(bool, Params) (ResultType, error), cache Cache, config Config, ) 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]( retrieveFunc func(context.Context, Params) (ResultType, error), cache Cache, config Config, ) func(context.Context, Params) (ResultType, error)
WrapStringWithContext is a function wrapper that caches string or []byte responses.
func WrapWithContext ¶ added in v0.2.0
func WrapWithContext[Params any, ResultType any]( retrieveFunc func(context.Context, Params) (ResultType, error), cache Cache, config Config, ) func(context.Context, Params) (ResultType, error)
WrapWithContext is a function wrapper that caches responses of any json serializable type.
Types ¶
type Cache ¶
type Cache interface {
// Get a value from the cache if it exists
Get(config *Config, params string) (value []byte, found bool)
// Set a value in the cache
Set(config *Config, params string, value []byte)
// 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(cutoff *time.Time) int64
// Delete all entries in the cache
Clear()
// Delete entries that have expired in the cache compared to cutoff
// entries expiry compared to utc now if cutoff is nil
Cleanup(cutoff *time.Time)
// GetIgnoreCacheCtxKey returns the 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"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at"`
Key string `json:"key" gorm:"uniqueIndex:idx_key_params;not null"`
Params string `json:"params" gorm:"uniqueIndex:idx_key_params;not null"`
Data []byte `json:"data" gorm:"not null"`
}
type Config ¶ added in v0.1.0
type Config struct {
// Key is used with params to create an identifier to get / set cache values
// It should be set to a unique value for each function that is wrapped
Key string
// 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 -1, cache value will never expire
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
}
Config is used to configure the caching wrapper functions
type GORMCache ¶
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(helloWorld, cache, cachefunk.Config{
Key: "hello",
TTL: 3600,
})
// First call will get value from wrapped function
value, err := HelloWorld(false, &HelloWorldParams{
Name: "bob",
})
fmt.Println("First call:", value, err)
// Second call will get value from cache
value, err = HelloWorld(false, &HelloWorldParams{
Name: "bob",
})
fmt.Println("Second call:", value, err)
}
Output:
func NewGORMCache ¶
func (*GORMCache) EntryCount ¶
func (*GORMCache) ExpiredEntryCount ¶
func (*GORMCache) GetIgnoreCacheCtxKey ¶
type InMemoryCache ¶
type InMemoryCache struct {
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(helloWorld, cache, cachefunk.Config{
Key: "hello",
TTL: 3600,
})
// First call will retrieve value from given function
value, err := HelloWorld(false, &HelloWorldParams{
Name: "bob",
})
fmt.Println(value, err)
// Second call will retrieve value from cache
value, err = HelloWorld(false, &HelloWorldParams{
Name: "bob",
})
fmt.Println("Result:", value, err)
}
Output:
func NewInMemoryCache ¶
func NewInMemoryCache() *InMemoryCache
func (*InMemoryCache) Cleanup ¶
func (c *InMemoryCache) Cleanup(cutoff *time.Time)
func (*InMemoryCache) Clear ¶
func (c *InMemoryCache) Clear()
func (*InMemoryCache) EntryCount ¶
func (c *InMemoryCache) EntryCount() int64
func (*InMemoryCache) ExpiredEntryCount ¶
func (c *InMemoryCache) ExpiredEntryCount(cutoff *time.Time) int64
func (*InMemoryCache) Get ¶
func (c *InMemoryCache) Get(config *Config, params string) ([]byte, bool)
func (*InMemoryCache) GetIgnoreCacheCtxKey ¶
func (c *InMemoryCache) GetIgnoreCacheCtxKey() CtxKey
type InMemoryCacheEntry ¶
Click to show internal directories.
Click to hide internal directories.