75 lines
1.2 KiB
Go
75 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"log"
|
|
"sync"
|
|
|
|
"git.markbailey.dev/cerbervs/joist/internal/errors"
|
|
)
|
|
|
|
const suff = "route_cache"
|
|
|
|
type RouteCacher interface {
|
|
Store(string, string) error
|
|
GetPath(string) (string, error)
|
|
All() (map[string]string, error)
|
|
}
|
|
|
|
type routeCache struct {
|
|
rs map[string]string
|
|
s Storer
|
|
}
|
|
|
|
var (
|
|
rcLock sync.Mutex
|
|
rcOnce sync.Once
|
|
c *routeCache
|
|
)
|
|
|
|
func NewRouteCache(store Storer) (*routeCache, error) {
|
|
rcOnce.Do(func() {
|
|
if store == nil {
|
|
st, err := NewBadgerStore(WithSubDir("route_cache"))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
store = st
|
|
}
|
|
|
|
c = &routeCache{
|
|
rs: make(map[string]string),
|
|
s: store,
|
|
}
|
|
})
|
|
|
|
if c == nil {
|
|
return nil, errors.ErrCacheUninitialized
|
|
}
|
|
|
|
rcLock.Lock()
|
|
defer rcLock.Unlock()
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func (r *routeCache) All() (map[string]string, error) {
|
|
if r.rs == nil {
|
|
return nil, errors.ErrCacheUninitialized
|
|
}
|
|
return r.rs, nil
|
|
}
|
|
|
|
func (r *routeCache) Store(name string, path string) error {
|
|
return nil
|
|
}
|
|
|
|
func (r *routeCache) GetPath(name string) (string, error) {
|
|
if path, ok := r.rs[name]; ok {
|
|
return path, nil
|
|
}
|
|
|
|
return string(""), errors.NewErrRouteNotFound(string(name))
|
|
}
|