0
vendor/github.com/kofalt/go-memoize/.gitignore
generated
vendored
Normal file
0
vendor/github.com/kofalt/go-memoize/.gitignore
generated
vendored
Normal file
21
vendor/github.com/kofalt/go-memoize/LICENSE.txt
generated
vendored
Normal file
21
vendor/github.com/kofalt/go-memoize/LICENSE.txt
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Nathaniel Kofalt
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
87
vendor/github.com/kofalt/go-memoize/memoize.go
generated
vendored
Normal file
87
vendor/github.com/kofalt/go-memoize/memoize.go
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
package memoize
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/patrickmn/go-cache"
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
// Memoizer allows you to memoize function calls. Memoizer is safe for concurrent use by multiple goroutines.
|
||||
type Memoizer struct {
|
||||
|
||||
// Storage exposes the underlying cache of memoized results to manipulate as desired - for example, to Flush().
|
||||
Storage *cache.Cache
|
||||
|
||||
group singleflight.Group
|
||||
}
|
||||
|
||||
// NewMemoizer creates a new Memoizer with the configured expiry and cleanup policies.
|
||||
// If desired, use cache.NoExpiration to cache values forever.
|
||||
func NewMemoizer(defaultExpiration, cleanupInterval time.Duration) *Memoizer {
|
||||
return &Memoizer{
|
||||
Storage: cache.New(defaultExpiration, cleanupInterval),
|
||||
group: singleflight.Group{},
|
||||
}
|
||||
}
|
||||
|
||||
// Memoize executes and returns the results of the given function, unless there was a cached value of the same key.
|
||||
// Only one execution is in-flight for a given key at a time.
|
||||
// The boolean return value indicates whether v was previously stored.
|
||||
func (m *Memoizer) Memoize(key string, fn func() (interface{}, error)) (interface{}, error, bool) {
|
||||
// Check cache
|
||||
value, found := m.Storage.Get(key)
|
||||
if found {
|
||||
return value, nil, true
|
||||
}
|
||||
|
||||
// Combine memoized function with a cache store
|
||||
value, err, _ := m.group.Do(key, func() (interface{}, error) {
|
||||
data, innerErr := fn()
|
||||
|
||||
if innerErr == nil {
|
||||
m.Storage.Set(key, data, cache.DefaultExpiration)
|
||||
}
|
||||
|
||||
return data, innerErr
|
||||
})
|
||||
return value, err, false
|
||||
}
|
||||
|
||||
// ErrMismatchedType if data returned from the cache does not match the expected type.
|
||||
var ErrMismatchedType = errors.New("data returned does not match expected type")
|
||||
|
||||
// MemoizedFunction the expensive function to be called.
|
||||
type MemoizedFunction[T any] func() (T, error)
|
||||
|
||||
// Call executes and returns the results of the given function, unless there was a cached value of the same key.
|
||||
// Only one execution is in-flight for a given key at a time.
|
||||
// The boolean return value indicates whether v was previously stored.
|
||||
func Call[T any](m *Memoizer, key string, fn MemoizedFunction[T]) (T, error, bool) {
|
||||
// Check cache
|
||||
value, found := m.Storage.Get(key)
|
||||
if found {
|
||||
v, ok := value.(T)
|
||||
if !ok {
|
||||
return v, ErrMismatchedType, true
|
||||
}
|
||||
return v, nil, true
|
||||
}
|
||||
|
||||
// Combine memoized function with a cache store
|
||||
value, err, _ := m.group.Do(key, func() (any, error) {
|
||||
data, innerErr := fn()
|
||||
|
||||
if innerErr == nil {
|
||||
m.Storage.Set(key, data, cache.DefaultExpiration)
|
||||
}
|
||||
|
||||
return data, innerErr
|
||||
})
|
||||
v, ok := value.(T)
|
||||
if !ok {
|
||||
return v, ErrMismatchedType, false
|
||||
}
|
||||
return v, err, false
|
||||
}
|
83
vendor/github.com/kofalt/go-memoize/readme.md
generated
vendored
Normal file
83
vendor/github.com/kofalt/go-memoize/readme.md
generated
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
# go-memoize
|
||||
|
||||
There wasn't a decent [memoizer](https://wikipedia.org/wiki/Memoization) for Golang out there, so I lashed two nice libraries together and made one.
|
||||
|
||||
Dead-simple. Safe for concurrent use.
|
||||
|
||||
[](https://pkg.go.dev/github.com/kofalt/go-memoize)
|
||||
[](https://goreportcard.com/report/github.com/kofalt/go-memoize)
|
||||
[](https://github.com/kofalt/go-memoize/actions)
|
||||
|
||||
## Project status
|
||||
|
||||
**Complete.** Latest commit timestamp might be old - that's okay.
|
||||
|
||||
Go-memoize has been in production for a few years, and has yet to burn the house down.
|
||||
|
||||
## Usage
|
||||
|
||||
Cache expensive function calls in memory, with a configurable timeout and purge interval:
|
||||
|
||||
```golang
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kofalt/go-memoize"
|
||||
)
|
||||
|
||||
// Any expensive call that you wish to cache
|
||||
expensive := func() (any, error) {
|
||||
time.Sleep(3 * time.Second)
|
||||
return "some data", nil
|
||||
}
|
||||
|
||||
// Cache expensive calls in memory for 90 seconds, purging old entries every 10 minutes.
|
||||
cache := memoize.NewMemoizer(90*time.Second, 10*time.Minute)
|
||||
|
||||
// This will call the expensive func
|
||||
result, err, cached := cache.Memoize("key1", expensive)
|
||||
|
||||
// This will be cached
|
||||
result, err, cached = cache.Memoize("key1", expensive)
|
||||
|
||||
// This uses a new cache key, so expensive is called again
|
||||
result, err, cached = cache.Memoize("key2", expensive)
|
||||
```
|
||||
|
||||
In the example above, `result` is:
|
||||
1. the return value from your function if `cached` is false, or
|
||||
1. a previously stored value if `cached` is true.
|
||||
|
||||
All the hard stuff is punted to [go-cache](https://github.com/patrickmn/go-cache) and [sync/singleflight](https://github.com/golang/sync), I just lashed them together.<br/>
|
||||
Note that `cache.Storage` is exported, so you can use underlying features such as [Flush](https://godoc.org/github.com/patrickmn/go-cache#Cache.Flush) or [SaveFile](https://godoc.org/github.com/patrickmn/go-cache#Cache.SaveFile).
|
||||
|
||||
### Type safety
|
||||
|
||||
The default usage stores and returns an `any` type.<br/>
|
||||
If you wants to store & retrieve a specific type, use `Call` instead:
|
||||
|
||||
```golang
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kofalt/go-memoize"
|
||||
)
|
||||
|
||||
// Same example as above, but this func returns a string!
|
||||
expensive := func() (string, error) {
|
||||
time.Sleep(3 * time.Second)
|
||||
return "some data", nil
|
||||
}
|
||||
|
||||
// Same as before
|
||||
cache := memoize.NewMemoizer(90*time.Second, 10*time.Minute)
|
||||
|
||||
// This will call the expensive func, and return a string.
|
||||
result, err, cached := memoize.Call(cache, "key1", expensive)
|
||||
|
||||
// This will be cached
|
||||
result, err, cached = memoize.Call(cache, "key1", expensive)
|
||||
|
||||
// This uses a new cache key, so expensive is called again
|
||||
result, err, cached = memoize.Call(cache, "key2", expensive)
|
||||
```
|
Reference in New Issue
Block a user