cachefs

package
v0.0.0-...-26a831c Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Package cloudeng.io/file/cachefs

import cloudeng.io/file/cachefs

Package cachefs provides a caching and related wrappers for ReadFileFS implementations.

Constants

DefaultTTL, DefaultCleanupInterval, DefaultSingleFlight
DefaultTTL = 24 * time.Hour
DefaultCleanupInterval = 1 * time.Hour
DefaultSingleFlight = false

Types

Type CachingReadFileFS
type CachingReadFileFS struct {
	// contains filtered or unexported fields
}

CachingReadFileFS implements a caching layer over a ReadFileFS that is suitable for a small numbers of small files that can be readily kept in memory.

Functions
func NewCachingReadFileFS(fs file.ReadFileFS, opts ...Option) *CachingReadFileFS

NewCachingReadFileFS creates a new CachingReadFileFS with the specified TTL and cleanup interval. It starts a background goroutine to periodically clear out expired cache entries. Call Stop to stop the background goroutine.

Methods
func (c *CachingReadFileFS) Forget(name string)

Forget removes the named file from the cache.

func (c *CachingReadFileFS) ReadFile(name string) ([]byte, error)

ReadFile reads the named file, utilizing the cache if the entry is fresh.

func (c *CachingReadFileFS) ReadFileCtx(ctx context.Context, name string) ([]byte, error)

ReadFileCtx reads the named file using the provided context, utilizing the cache if fresh.

func (c *CachingReadFileFS) Stop(ctx context.Context) error

Stop stops the background cleanup goroutine.

Type Option
type Option func(*options)
Functions
func WithCleanupInterval(d time.Duration) Option

WithCleanupInterval specifies the interval for periodic background cleanup of expired entries. The default is DefaultCleanupInterval. A value of 0 disables periodic cleanup, with expired entries being overwritten on access.

func WithSingleFlight(v bool) Option

WithSingleFlight enables single-flight behavior for concurrent calls to ReadFileCtx with the same name. The default is false.

func WithTTL(d time.Duration) Option

WithTTL specifies the time-to-live for cache entries. The default is DefaultTTL.

Type SingleFlightReadFileFS
type SingleFlightReadFileFS struct {
	// contains filtered or unexported fields
}

SingleFlightReadFileFS is a wrapper around a ReadFileFS that provides single-flight behavior for concurrent calls to ReadFileCtx and ReadFile with the same name. This can be used in conjunction with CachingReadFileFS to prevent thundering herd issues on cache misses.

Functions
func NewSingleFlightReadFileFS(fs file.ReadFileFS) *SingleFlightReadFileFS
Methods
func (s *SingleFlightReadFileFS) ReadFile(name string) ([]byte, error)
func (s *SingleFlightReadFileFS) ReadFileCtx(ctx context.Context, name string) ([]byte, error)

Documentation

Overview

Package cachefs provides a caching and related wrappers for ReadFileFS implementations.

Index

Constants

View Source
const (
	DefaultTTL             = 24 * time.Hour
	DefaultCleanupInterval = 1 * time.Hour
	DefaultSingleFlight    = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CachingReadFileFS

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

CachingReadFileFS implements a caching layer over a ReadFileFS that is suitable for a small numbers of small files that can be readily kept in memory.

func NewCachingReadFileFS

func NewCachingReadFileFS(fs file.ReadFileFS, opts ...Option) *CachingReadFileFS

NewCachingReadFileFS creates a new CachingReadFileFS with the specified TTL and cleanup interval. It starts a background goroutine to periodically clear out expired cache entries. Call Stop to stop the background goroutine.

func (*CachingReadFileFS) Forget

func (c *CachingReadFileFS) Forget(name string)

Forget removes the named file from the cache.

func (*CachingReadFileFS) ReadFile

func (c *CachingReadFileFS) ReadFile(name string) ([]byte, error)

ReadFile reads the named file, utilizing the cache if the entry is fresh.

func (*CachingReadFileFS) ReadFileCtx

func (c *CachingReadFileFS) ReadFileCtx(ctx context.Context, name string) ([]byte, error)

ReadFileCtx reads the named file using the provided context, utilizing the cache if fresh.

func (*CachingReadFileFS) Stop

func (c *CachingReadFileFS) Stop(ctx context.Context) error

Stop stops the background cleanup goroutine.

type Option

type Option func(*options)

func WithCleanupInterval

func WithCleanupInterval(d time.Duration) Option

WithCleanupInterval specifies the interval for periodic background cleanup of expired entries. The default is DefaultCleanupInterval. A value of 0 disables periodic cleanup, with expired entries being overwritten on access.

func WithSingleFlight

func WithSingleFlight(v bool) Option

WithSingleFlight enables single-flight behavior for concurrent calls to ReadFileCtx with the same name. The default is false.

func WithTTL

func WithTTL(d time.Duration) Option

WithTTL specifies the time-to-live for cache entries. The default is DefaultTTL.

type SingleFlightReadFileFS

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

SingleFlightReadFileFS is a wrapper around a ReadFileFS that provides single-flight behavior for concurrent calls to ReadFileCtx and ReadFile with the same name. This can be used in conjunction with CachingReadFileFS to prevent thundering herd issues on cache misses.

func NewSingleFlightReadFileFS

func NewSingleFlightReadFileFS(fs file.ReadFileFS) *SingleFlightReadFileFS

func (*SingleFlightReadFileFS) ReadFile

func (s *SingleFlightReadFileFS) ReadFile(name string) ([]byte, error)

func (*SingleFlightReadFileFS) ReadFileCtx

func (s *SingleFlightReadFileFS) ReadFileCtx(ctx context.Context, name string) ([]byte, error)

Jump to

Keyboard shortcuts

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