corfs

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 4 Imported by: 0

README

CorFS - Cache on Read FileSystem

Go Reference Go Report Card CI License

The corfs package implements a Cache-on-Read FileSystem that wraps two absfs.Filer implementations. It reads from a primary filesystem and caches content to a secondary filesystem on successful reads, providing a two-tier caching system.

Features

  • Two-tier caching: Reads from primary, caches to secondary
  • Transparent operation: Acts as a standard absfs.Filer
  • Best-effort caching: Cache failures don't affect primary operations

Install

go get github.com/absfs/corfs

Example Usage

package main

import (
    "os"

    "github.com/absfs/corfs"
    "github.com/absfs/memfs"
    "github.com/absfs/osfs"
)

func main() {
    // Create primary filesystem (e.g., slow remote storage)
    primary, _ := osfs.NewFS()

    // Create cache filesystem (e.g., fast local memory)
    cache, _ := memfs.NewFS()

    // Create cache-on-read filesystem
    fs := corfs.New(primary, cache)

    // First read comes from primary and caches to secondary
    f, _ := fs.OpenFile("/data/file.txt", os.O_RDONLY, 0)
    defer f.Close()

    // Subsequent reads can be served from cache
    // (if using a separate instance of the same cache)
}

absfs

Check out the absfs repo for more information about the abstract filesystem interface and features like filesystem composition.

LICENSE

This project is governed by the MIT License. See LICENSE

Documentation

Overview

Package corfs implements a Cache-on-Read FileSystem that wraps two absfs.Filer implementations. It reads from the primary filesystem and caches content to the secondary filesystem on successful reads, providing a two-tier caching system.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotDir = os.ErrInvalid

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File wraps files from both primary and cache filesystems.

func (*File) Close

func (f *File) Close() error

Close closes both file handles.

func (*File) Name

func (f *File) Name() string

Name returns the name of the file.

func (*File) Read

func (f *File) Read(b []byte) (int, error)

Read reads from the primary file and caches content to the cache file.

func (*File) ReadAt

func (f *File) ReadAt(b []byte, off int64) (int, error)

ReadAt reads from the primary file at a specific offset.

func (*File) ReadDir

func (f *File) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir reads directory entries from the primary file.

func (*File) Readdir

func (f *File) Readdir(n int) ([]os.FileInfo, error)

Readdir reads directory entries from the primary file.

func (*File) Readdirnames

func (f *File) Readdirnames(n int) ([]string, error)

Readdirnames reads directory entry names from the primary file.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek seeks in the primary file.

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat returns file info from the primary file.

func (*File) Sync

func (f *File) Sync() error

Sync syncs both files.

func (*File) Truncate

func (f *File) Truncate(size int64) error

Truncate truncates both files.

func (*File) Write

func (f *File) Write(b []byte) (int, error)

Write writes to both primary and cache files.

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (int, error)

WriteAt writes to both files at a specific offset.

func (*File) WriteString

func (f *File) WriteString(s string) (int, error)

WriteString writes a string to both files.

type FileSystem

type FileSystem struct {
	// contains filtered or unexported fields
}

FileSystem implements absfs.Filer with cache-on-read semantics. Reads are performed from the primary filesystem, with successful reads being cached to the secondary filesystem for future access.

func New

func New(primary, cache absfs.Filer) *FileSystem

New creates a new CorFS that reads from primary and caches to cache.

func (*FileSystem) Chmod

func (fs *FileSystem) Chmod(name string, mode os.FileMode) error

Chmod changes the mode in both filesystems.

func (*FileSystem) Chown

func (fs *FileSystem) Chown(name string, uid, gid int) error

Chown changes the owner and group in both filesystems.

func (*FileSystem) Chtimes

func (fs *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times in both filesystems.

func (*FileSystem) Mkdir

func (fs *FileSystem) Mkdir(name string, perm os.FileMode) error

Mkdir creates a directory in both filesystems.

func (*FileSystem) OpenFile

func (fs *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (absfs.File, error)

OpenFile opens a file from the primary filesystem and caches it to the cache filesystem on successful read operations.

func (*FileSystem) ReadDir

func (fs *FileSystem) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the named directory and returns a list of directory entries.

func (*FileSystem) ReadFile

func (fs *FileSystem) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents.

func (*FileSystem) Remove

func (fs *FileSystem) Remove(name string) error

Remove removes a file from both filesystems.

func (*FileSystem) RemoveAll

func (fs *FileSystem) RemoveAll(path string) error

RemoveAll removes a path and any children it contains in both filesystems.

func (*FileSystem) Rename

func (fs *FileSystem) Rename(oldpath, newpath string) error

Rename renames a file in both filesystems.

func (*FileSystem) Stat

func (fs *FileSystem) Stat(name string) (os.FileInfo, error)

Stat returns file info from the primary filesystem.

func (*FileSystem) Sub

func (fs *FileSystem) Sub(dir string) (fs.FS, error)

Sub returns an fs.FS corresponding to the subtree rooted at dir.

func (*FileSystem) Truncate

func (fs *FileSystem) Truncate(name string, size int64) error

Truncate truncates a file to the specified size in both filesystems.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL