2023-09-08 12:23:22 +00:00
|
|
|
// Package cache contains caching implementation for retrieved data
|
2016-06-29 13:13:26 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2016-06-29 13:13:26 +00:00
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
// ErrKeyNotFound signalized the key is not present in the cache
|
|
|
|
var ErrKeyNotFound = errors.New("requested key was not found in database")
|
2016-06-29 13:13:26 +00:00
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
// Cache describes an interface used to store generated data
|
2016-06-29 13:13:26 +00:00
|
|
|
type Cache interface {
|
|
|
|
Get(namespace, key string) (value string, err error)
|
|
|
|
Set(namespace, key, value string, ttl time.Duration) (err error)
|
|
|
|
Delete(namespace, key string) (err error)
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
// GetCacheByURI instantiates a new Cache by the given URI string
|
2016-06-29 13:13:26 +00:00
|
|
|
func GetCacheByURI(uri string) (Cache, error) {
|
2023-09-08 12:23:22 +00:00
|
|
|
u, err := url.Parse(uri)
|
2016-06-29 13:13:26 +00:00
|
|
|
if err != nil {
|
2023-09-08 12:23:22 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing uri")
|
2016-06-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 12:23:22 +00:00
|
|
|
switch u.Scheme {
|
2016-06-29 13:13:26 +00:00
|
|
|
case "mem":
|
|
|
|
return NewInMemCache(), nil
|
|
|
|
default:
|
2023-09-08 12:23:22 +00:00
|
|
|
return nil, errors.New("Invalid cache scheme: " + u.Scheme)
|
2016-06-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
}
|