mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 13:20:02 +00:00
34 lines
877 B
Go
34 lines
877 B
Go
// Package cache contains caching implementation for retrieved data
|
|
package cache
|
|
|
|
import (
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// ErrKeyNotFound signalized the key is not present in the cache
|
|
var ErrKeyNotFound = errors.New("requested key was not found in database")
|
|
|
|
// Cache describes an interface used to store generated data
|
|
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)
|
|
}
|
|
|
|
// GetCacheByURI instantiates a new Cache by the given URI string
|
|
func GetCacheByURI(uri string) (Cache, error) {
|
|
u, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "parsing uri")
|
|
}
|
|
|
|
switch u.Scheme {
|
|
case "mem":
|
|
return NewInMemCache(), nil
|
|
default:
|
|
return nil, errors.New("Invalid cache scheme: " + u.Scheme)
|
|
}
|
|
}
|