feat: add configurations for memory only, disk only, and memory & disk modes
All checks were successful
Release Tag / release (push) Successful in 14s

This commit is contained in:
2025-01-22 19:28:45 -06:00
parent ca069a20ee
commit 7401c040dc
8 changed files with 156 additions and 50 deletions

25
vfs/cache/cache.go vendored
View File

@@ -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 {

View File

@@ -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")

View File

@@ -27,6 +27,24 @@ type DiskFS struct {
// New creates a new DiskFS.
func new(root string, capacity int64, skipinit bool) *DiskFS {
if capacity <= 0 {
panic("disk capacity must be greater than 0") // panic if the capacity is less than or equal to 0
}
if root == "" {
panic("disk root must not be empty") // panic if the root is empty
}
fi, err := os.Stat(root)
if err != nil {
if !os.IsNotExist(err) {
panic(err) // panic if the error is something other than not found
}
}
if !fi.IsDir() {
panic("disk root must be a directory") // panic if the root is not a directory
}
dfs := &DiskFS{
root: root,
info: make(map[string]*vfs.FileInfo),

View File

@@ -20,6 +20,9 @@ type GCFS struct {
type GCHandlerFunc func(vfs vfs.VFS, size int)
func New(vfs vfs.VFS, multiplier int, gcHandlerFunc GCHandlerFunc) *GCFS {
if multiplier <= 0 {
multiplier = 1 // if the multiplier is less than or equal to 0 set it to 1 will be slow but the user can set it to a higher value if they want
}
return &GCFS{
VFS: vfs,
multiplier: multiplier,

View File

@@ -25,6 +25,10 @@ type MemoryFS struct {
// New creates a new MemoryFS.
func New(capacity int64) *MemoryFS {
if capacity <= 0 {
panic("memory capacity must be greater than 0") // panic if the capacity is less than or equal to 0
}
return &MemoryFS{
files: make(map[string]*file),
capacity: capacity,