mem

package
v3.2.259 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCacheCapacity is the maximum size of the cache in bytes if not provided
	// at cache initialization. Equivalent to 1GiB
	DefaultCapacity = 1 << 30

	// MinCapacity is the minimum size of the cache. Equivalent to 256MiB
	MinCapacity = 256 << 20

	// MaxCapacity is the maximum size of the cache. Equivalent to 5GiB
	MaxCapacity = 5 << 30

	// DefaultMaxAge is the maximum age of cache files, if not provided
	// at cache initialization. 0 means no expiration. Equivalent to 12 hours.
	DefaultMaxAge = 12 * time.Hour

	// DefaultMaxFileCount is the maximum number of files in the cache if not provided
	// at cache initialization. 0 means unbounded.
	DefaultMaxFileCount = 1000

	// DefaultMaintenanceInterval is the interval at which cache maintenance tasks are performed,
	// if not provided at cache initialization.
	DefaultMaintenanceInterval = 5 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MemoryCache

type MemoryCache struct {
	Capacity            int64
	MaxFileCount        int64
	MaxAge              time.Duration
	MaintenanceInterval time.Duration
	// contains filtered or unexported fields
}

MemoryCache implements a simple in memory LRU cache for file data with an interface that roughly matches the FUSE Read/Write methods.

TODO:

  • allow concurrent reads/writes to different files in the cache by moving lock coordination out of the caller and lock by path within the cache with a RWMutex map. This will require maintaining a reservation of bytes so that concurrent goroutines can't exhaust memory that a different goroutine evicted for its Write operation.

func NewMemoryCache

func NewMemoryCache(opts ...Option) (*MemoryCache, error)

func (*MemoryCache) Delete

func (mc *MemoryCache) Delete(path string) bool

Delete removes the cached file at the given path from the cache.

func (*MemoryCache) Read

func (mc *MemoryCache) Read(path string, buff []byte, ofst int64) (n int, err error)

Read reads data from the cached file at the given path into buff starting at the provided offset.

func (*MemoryCache) StartMaintenance

func (mc *MemoryCache) StartMaintenance()

StartMaintenance starts the maintenance goroutine if it is not already running.

func (*MemoryCache) Stats

func (mc *MemoryCache) Stats() *cache.Stats

Stats returns the current cache statistics.

func (*MemoryCache) StopMaintenance

func (mc *MemoryCache) StopMaintenance()

StopMaintenance stops the maintenance goroutine if it is running.

func (*MemoryCache) Write

func (mc *MemoryCache) Write(path string, buff []byte, ofst int64) (int, error)

type Option

type Option func(*MemoryCache)

Option configures a MemoryCache.

func WithCapacityBytes

func WithCapacityBytes(n int64) Option

WithCapacityBytes sets the maximum total size (in bytes) for the cache.

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets the logger for the cache.

func WithMaintenanceInterval

func WithMaintenanceInterval(d time.Duration) Option

WithMaintenanceInterval sets the interval for cache maintenance operations.

func WithMaxAge

func WithMaxAge(d time.Duration) Option

WithMaxAge sets the maximum age for cache files.

func WithMaxFileCount

func WithMaxFileCount(n int64) Option

WithMaxFileCount sets the maximum number of files for the cache.

Jump to

Keyboard shortcuts

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