zcache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 12 Imported by: 0

README

zcache

zcache is a naïve LRU file caching solution w/ optional support for file compression.

  cache, err := zcache.New(zcache.DirFs("data"), 5000)
  if err != nil {
    panic(err)
  }
  cache.Compressed = true // set before init
  if err = cache.Init(); err != nil {
    panic(err)
  }
  reader, item, _ := cache.OpenOrCreate("my-key", func (w io.Writer) error {
    _, err := w.Write([]byte("helloski\n"))
    return err
  })
  msg, _ := io.ReadAll(reader)
  fmt.Println(string(msg), item)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirFS

func DirFS(root string) *osDirFS

Types

type Cache

type Cache struct {
	Compressed bool
	// contains filtered or unexported fields
}

func New

func New(dir CacheFS, maxSize int) (c *Cache, err error)

func (*Cache) Contains

func (c *Cache) Contains(id string) (exists bool)

func (*Cache) Get

func (c *Cache) Get(id string) (item Item, exists bool)

func (*Cache) Init

func (c *Cache) Init() (err error)

func (*Cache) Open

func (c *Cache) Open(id string) (file io.ReadSeekCloser, item Item, err error)

func (*Cache) OpenOrCreate

func (c *Cache) OpenOrCreate(id string, create func(w io.Writer) error) (res io.ReadSeekCloser, item Item, err error)

func (*Cache) Remove

func (c *Cache) Remove(id string) error

type CacheFS

type CacheFS interface {
	Open(path string) (io.ReadSeekCloser, error)
	Create(path string) (io.WriteCloser, error)
	Remove(path string) error
	RemoveAll(path string) error
	Rename(from, to string) error
	MkdirAll(dir string, perm os.FileMode) error
	ReadDir(dir string) ([]os.DirEntry, error)
}

type Item

type Item struct {
	Id         string
	AccessedAt time.Time
	CreatedAt  time.Time
}

type MutexMap

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

func NewMutexMap

func NewMutexMap() *MutexMap

func (*MutexMap) Lock

func (m *MutexMap) Lock(id string) sync.Locker

func (*MutexMap) Unlock

func (m *MutexMap) Unlock(id string)

Jump to

Keyboard shortcuts

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