fscache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

README

Build Status Go Reference Go Report Card codecov

fscache

A filesystem LRU cache in golang based on atime.

Features

  • Accessible from multiple threads.
  • LRU GC based on atime.
  • Provide throughout metrics by struct, you can easily wrap it into Prometheus metrics. (TODO)
  • All functions under one interface, easy to mock.

Usage

package main

import (
	"fmt"
	"io/ioutil"
	"time"

	"github.com/sequix/fscache"
)

func main() {
	cacheDir, err := ioutil.TempDir("", "fscache")
	if err != nil {
		panic(err)
	}
	gcStopCh := make(chan struct{})
	defer close(gcStopCh)

	cache, err := fscache.New(
		fscache.WithCacheDir(cacheDir),
		fscache.WithMaxBytes(10*1024*1024),
		fscache.WithGcInterval(5 * time.Minute),
		fscache.WithGcStopCh(gcStopCh),
	)
	if err != nil {
		panic(err)
	}

	val := []byte("achilles")
	cache.Set("key", val)

	valFromCache, err := cache.Get("key", nil)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(valFromCache))
}

FAQs

1.Will I see a file with half of the content I passed to the cache?

No, as long as the filesystem provide an atomic rename(3) function.

2.Can I use it from multiple processes?

Better not. Because each of the processes will do its own GC.

3.Why there is not a del()?

GC will take care of that. Having a del() will mess the code.

4.Why using []byte not io.Reader/io.Writer?

Personal needs, if you want that, add it yourself.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound will be returned when getting a key that not setting before.
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type Cache

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

Cache is a LRU filesystem cache based on atime.

func (*Cache) Get

func (f *Cache) Get(key string, dst []byte) ([]byte, error)

Get implements Interface.Get().

func (*Cache) Has

func (f *Cache) Has(key string) bool

Has implements Interface.Has().

func (*Cache) Set

func (f *Cache) Set(key string, src []byte) error

Set implements Interface.Set().

type Interface

type Interface interface {
	// Set sets the value of key as src.
	// Setting the same key multiple times, the last set call takes effect.
	Set(key string, src []byte) error
	// Get gets the value of key to dst, and returns dst no matter whether or not there is an error.
	Get(key string, dst []byte) ([]byte, error)
	// Has tells you if a key has been set or not.
	Has(key string) bool
}

Interface provides a set of general cache functions.

func New

func New(opts ...Option) (Interface, error)

New creates a LRU filesystem cache based on atime, and starts the GC goroutine.

type Logger

type Logger interface {
	Errorf(fmt string, args ...interface{})
}

Logger used by this package.

type Option

type Option func(fc *Cache)

Option can be passed to New() to tailor your needs.

func WithCacheDir

func WithCacheDir(cacheDir string) Option

WithCacheDir specifies where the cache holds.

func WithGcInterval

func WithGcInterval(interval time.Duration) Option

WithGcInterval specifies how often the GC performs.

func WithGcStopCh

func WithGcStopCh(stopCh <-chan struct{}) Option

WithGcStopCh receives a channel, when the channel close, gc will stop. By default, gc will not stop until the process exits.

func WithMaxBytes

func WithMaxBytes(bytes int64) Option

WithMaxBytes specifies how many space the cache could take up.

Jump to

Keyboard shortcuts

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