2023-05-11 18:45:44 +00:00
|
|
|
package debian
|
|
|
|
|
|
|
|
import (
|
2023-05-15 18:37:09 +00:00
|
|
|
"errors"
|
2023-05-13 18:49:11 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-05-11 18:45:44 +00:00
|
|
|
"github.com/rapidloop/skv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
|
|
|
store *skv.KVStore
|
|
|
|
}
|
|
|
|
|
2023-05-13 18:49:11 +00:00
|
|
|
// cache is not thread-safe, so make sure there are only one user
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
2023-05-11 18:45:44 +00:00
|
|
|
func NewCache(path string) (c *Cache, err error) {
|
2023-05-13 18:49:11 +00:00
|
|
|
mu.Lock()
|
|
|
|
|
2023-05-11 18:45:44 +00:00
|
|
|
c = &Cache{}
|
|
|
|
c.store, err = skv.Open(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:37:09 +00:00
|
|
|
func (c Cache) Put(p []DebianKernel) error {
|
|
|
|
if len(p) == 0 {
|
|
|
|
return errors.New("empty slice")
|
|
|
|
}
|
|
|
|
return c.store.Put(p[0].Version.Package, p)
|
2023-05-11 18:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 18:37:09 +00:00
|
|
|
func (c Cache) Get(version string) (p []DebianKernel, err error) {
|
2023-05-11 18:45:44 +00:00
|
|
|
err = c.store.Get(version, &p)
|
2023-05-15 18:37:09 +00:00
|
|
|
if len(p) == 0 {
|
|
|
|
err = skv.ErrNotFound
|
|
|
|
}
|
2023-05-11 18:45:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 18:43:15 +00:00
|
|
|
func (c Cache) PutVersions(versions []string) error {
|
|
|
|
return c.store.Put("versions", versions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Cache) GetVersions() (versions []string, err error) {
|
|
|
|
err = c.store.Get("versions", &versions)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 18:49:11 +00:00
|
|
|
func (c Cache) Close() (err error) {
|
|
|
|
err = c.store.Close()
|
|
|
|
mu.Unlock()
|
|
|
|
return
|
2023-05-11 18:45:44 +00:00
|
|
|
}
|