feat: add configurations for memory only, disk only, and memory & disk modes
All checks were successful
Release Tag / release (push) Successful in 14s
All checks were successful
Release Tag / release (push) Successful in 14s
This commit is contained in:
25
vfs/cache/cache.go
vendored
25
vfs/cache/cache.go
vendored
@@ -21,23 +21,24 @@ type CacheFS struct {
|
||||
type CacheHandler func(*vfs.FileInfo, cachestate.CacheState) bool
|
||||
|
||||
// New creates a new CacheFS. fast is used for caching, and slow is used for storage. fast should obviously be faster than slow.
|
||||
func New(fast, slow vfs.VFS, cacheHandler CacheHandler) *CacheFS {
|
||||
if slow == nil {
|
||||
panic("slow is nil")
|
||||
}
|
||||
|
||||
if fast == slow {
|
||||
panic("fast and slow are the same")
|
||||
}
|
||||
|
||||
func New(cacheHandler CacheHandler) *CacheFS {
|
||||
return &CacheFS{
|
||||
fast: fast,
|
||||
slow: slow,
|
||||
|
||||
cacheHandler: cacheHandler,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CacheFS) SetSlow(vfs vfs.VFS) {
|
||||
if vfs == nil {
|
||||
panic("vfs is nil") // panic if the vfs is nil
|
||||
}
|
||||
|
||||
c.slow = vfs
|
||||
}
|
||||
|
||||
func (c *CacheFS) SetFast(vfs vfs.VFS) {
|
||||
c.fast = vfs
|
||||
}
|
||||
|
||||
// cacheState returns the state of the file at key.
|
||||
func (c *CacheFS) cacheState(key string) cachestate.CacheState {
|
||||
if c.fast != nil {
|
||||
|
||||
31
vfs/cache/cache_test.go
vendored
31
vfs/cache/cache_test.go
vendored
@@ -20,7 +20,9 @@ func TestNew(t *testing.T) {
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
|
||||
cache := New(fast, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
if cache == nil {
|
||||
t.Fatal("expected cache to be non-nil")
|
||||
}
|
||||
@@ -35,7 +37,9 @@ func TestNewPanics(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
New(nil, nil, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(nil)
|
||||
cache.SetSlow(nil)
|
||||
}
|
||||
|
||||
func TestSetAndGet(t *testing.T) {
|
||||
@@ -43,7 +47,9 @@ func TestSetAndGet(t *testing.T) {
|
||||
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
cache := New(fast, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
key := "test"
|
||||
value := []byte("value")
|
||||
@@ -66,7 +72,8 @@ func TestSetAndGetNoFast(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
slow := testMemory()
|
||||
cache := New(nil, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
key := "test"
|
||||
value := []byte("value")
|
||||
@@ -89,9 +96,11 @@ func TestCaching(t *testing.T) {
|
||||
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
cache := New(fast, slow, func(fi *vfs.FileInfo, cs cachestate.CacheState) bool {
|
||||
cache := New(func(fi *vfs.FileInfo, cs cachestate.CacheState) bool {
|
||||
return true
|
||||
})
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
key := "test"
|
||||
value := []byte("value")
|
||||
@@ -148,7 +157,9 @@ func TestGetNotFound(t *testing.T) {
|
||||
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
cache := New(fast, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
_, err := cache.Get("nonexistent")
|
||||
if !errors.Is(err, vfserror.ErrNotFound) {
|
||||
@@ -161,7 +172,9 @@ func TestDelete(t *testing.T) {
|
||||
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
cache := New(fast, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
key := "test"
|
||||
value := []byte("value")
|
||||
@@ -185,7 +198,9 @@ func TestStat(t *testing.T) {
|
||||
|
||||
fast := testMemory()
|
||||
slow := testMemory()
|
||||
cache := New(fast, slow, nil)
|
||||
cache := New(nil)
|
||||
cache.SetFast(fast)
|
||||
cache.SetSlow(slow)
|
||||
|
||||
key := "test"
|
||||
value := []byte("value")
|
||||
|
||||
Reference in New Issue
Block a user