1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-11-08 13:20:02 +00:00
badge-gen/cache/cache.go

35 lines
877 B
Go
Raw Normal View History

// Package cache contains caching implementation for retrieved data
2016-06-29 13:13:26 +00:00
package cache
import (
"net/url"
"time"
"github.com/pkg/errors"
)
2016-06-29 13:13:26 +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
// 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)
}
// GetCacheByURI instantiates a new Cache by the given URI string
2016-06-29 13:13:26 +00:00
func GetCacheByURI(uri string) (Cache, error) {
u, err := url.Parse(uri)
2016-06-29 13:13:26 +00:00
if err != nil {
return nil, errors.Wrap(err, "parsing uri")
2016-06-29 13:13:26 +00:00
}
switch u.Scheme {
2016-06-29 13:13:26 +00:00
case "mem":
return NewInMemCache(), nil
default:
return nil, errors.New("Invalid cache scheme: " + u.Scheme)
2016-06-29 13:13:26 +00:00
}
}