67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"log"
|
|
"sync"
|
|
|
|
"git.markbailey.dev/cerbervs/joist/internal/errors"
|
|
)
|
|
|
|
const suff = "route_cache"
|
|
|
|
type routeCache struct {
|
|
rs map[string]string
|
|
s *BadgerStore
|
|
}
|
|
|
|
var (
|
|
rcLock sync.Mutex
|
|
rcOnce sync.Once
|
|
c *routeCache
|
|
)
|
|
|
|
func NewRouteCache() (*routeCache, error) {
|
|
rcOnce.Do(func() {
|
|
store, err := NewBadgerStore(WithSubDir("route_cache"))
|
|
if err != nil {
|
|
log.Printf("failed to create badger store for route cache: %v", err)
|
|
c = nil
|
|
return
|
|
}
|
|
|
|
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 {
|
|
// TODO: implement
|
|
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))
|
|
}
|