feat: Add support for on-disk and in-memory caches with configurable duration
This commit is contained in:
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/config"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func Cache(
|
||||
base afero.Fs,
|
||||
root string,
|
||||
cacheType string,
|
||||
ttl time.Duration,
|
||||
cacheDir string,
|
||||
) (afero.Fs, error) {
|
||||
switch cacheType {
|
||||
case CacheTypeMemory:
|
||||
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewMemMapFs(), ttl), nil
|
||||
case CacheTypeDir:
|
||||
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewBasePathFs(afero.NewOsFs(), cacheDir), ttl), nil
|
||||
case config.NoneKey:
|
||||
return afero.NewBasePathFs(base, root), nil
|
||||
default:
|
||||
return nil, ErrCacheTypeUnsupported
|
||||
}
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
package cache
|
||||
|
||||
func CheckCacheType(cacheType string) error {
|
||||
cacheTypeIsKnown := false
|
||||
|
||||
for _, candidate := range KnownCacheTypes {
|
||||
if cacheType == candidate {
|
||||
cacheTypeIsKnown = true
|
||||
}
|
||||
}
|
||||
|
||||
if !cacheTypeIsKnown {
|
||||
return ErrCacheTypeUnknown
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/pojntfx/stfs/pkg/config"
|
||||
)
|
||||
|
||||
const (
|
||||
CacheTypeMemory = "memory"
|
||||
CacheTypeDir = "dir"
|
||||
)
|
||||
|
||||
var (
|
||||
KnownCacheTypes = []string{config.NoneKey, CacheTypeMemory, CacheTypeDir}
|
||||
)
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
package cache
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrCacheTypeUnsupported = errors.New("cache type unsupported")
|
||||
ErrCacheTypeUnknown = errors.New("cache type unknown")
|
||||
)
|
||||
Reference in New Issue
Block a user