zcache

package
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2022 License: MIT Imports: 13 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 {
	Cache *lru.Cache

	Debounce time.Duration
	// contains filtered or unexported fields
}

func New

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

func (*Cache) Add added in v0.4.1

func (c *Cache) Add(key, val interface{}) (evicted bool)

func (*Cache) ContainsOrAdd added in v0.4.1

func (c *Cache) ContainsOrAdd(key, val interface{}) (existed, evicted bool)

func (*Cache) Get

func (c *Cache) Get(key interface{}) (value interface{}, exists bool)

func (*Cache) GetOldest added in v0.4.3

func (c *Cache) GetOldest() (key, value interface{}, exists bool)

func (*Cache) PeekOrAdd added in v0.4.1

func (c *Cache) PeekOrAdd(key, val interface{}) (prev interface{}, existed, evicted bool)

func (*Cache) Purge added in v0.4.1

func (c *Cache) Purge()

func (*Cache) Remove

func (c *Cache) Remove(key interface{}) (present bool)

func (*Cache) RemoveAll added in v0.4.1

func (c *Cache) RemoveAll() (key, val interface{}, removed bool)

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 FileCache added in v0.4.1

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

func NewFileCache added in v0.4.1

func NewFileCache(dir CacheFS, maxSize int) (c *FileCache, err error)

func (*FileCache) Contains added in v0.4.1

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

func (*FileCache) Get added in v0.4.1

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

func (*FileCache) Init added in v0.4.1

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

func (*FileCache) Open added in v0.4.1

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

func (*FileCache) OpenOrCreate added in v0.4.1

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

func (*FileCache) Remove added in v0.4.1

func (c *FileCache) Remove(id string) 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