filecache

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: Apache-2.0, MIT Imports: 3 Imported by: 0

Documentation

Overview

Package filecache provides an LRU cache of opened files. If the same files are frequently opened and closed this is useful for reducing the number of syscalls for opening and closing the files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileCache

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

FileCache maintains a LRU cache of opened files. Its methods are safe to call concurrently.

func New

func New(capacity int) *FileCache

New creates a new FileCache that can hold up to specified capacity of open files. If capacity is 0, then there is no limit. Files are opened read-only. If other open flags and permissions are needed, use NewOpenFile.

func NewOpenFile

func NewOpenFile(capacity int, openFlag int, openPerm os.FileMode) *FileCache

NewOpenFile created a new FileCache that opens files using the specified arguments to os.OpenFile.

func (*FileCache) Cap

func (c *FileCache) Cap() int

Capacity return the capacity of the FileCache.

func (*FileCache) Clear

func (c *FileCache) Clear()

Clear removes all files in the FileCache and closes those that have a zero reference count.

func (*FileCache) Close

func (c *FileCache) Close(file *os.File) error

Close decrements the reference count on the file. If the file has been removed from the cache and the reference count is zero, then the file is closed.

func (*FileCache) Len

func (c *FileCache) Len() int

Len return the number of open files in the FileCache.

func (*FileCache) Open

func (c *FileCache) Open(name string) (*os.File, error)

Open returns the already opened file, or opens the named file and returns that. The file is subsequently retrievable without opening it again, unless it has been removed from the FileCache.

All returned os.File instances are shared, so opeartions on these files must use methods that do not depend on the current file position.

Every call to Open must be accompanied by a call to Close. Otherwise, reference counts will not be adjusted correctly and file handles will leak.

func (*FileCache) Remove

func (c *FileCache) Remove(name string)

Remove removes the named file from the cache and closes it if it has a zero reference count.

func (*FileCache) SetCacheSize

func (c *FileCache) SetCacheSize(capacity int)

SetCacheSize sets the capacity of the FileCache. If the change reduces the capacity to fewer items than are currently in the cache, then the oldest items are removed until the number of items in the cache is equal to the new capacity.

func (*FileCache) SetOnEvicted

func (c *FileCache) SetOnEvicted(f func(*os.File, int))

SetOnEvicted specifies a function to call when a file is removed from cache.

func (*FileCache) Stats

func (c *FileCache) Stats() (int, int, int, int)

Stats returns hit count, miss count, items in cache, and cache capacity.

Jump to

Keyboard shortcuts

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