filecache

package module
v0.0.0-...-6898a22 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2019 License: Apache-2.0 Imports: 7 Imported by: 1

README

filecache

cache library that store data in local file.

usage

func Example_Cache() {
	cache := filecache.New("./cache.data")
	defer os.Remove("./cache.data")

	_, err := cache.Get("not-exist")
	fmt.Println(err)

	fmt.Println(cache.Set("k", "v", time.Minute))

	v, err := cache.Get("k")
	fmt.Println(v, err)

	ttl, err := cache.TTL("k")
	fmt.Println(int(math.Ceil(ttl.Seconds())), err)

	time.Sleep(time.Second)

	ttl, err = cache.TTL("k")
	fmt.Println(int(math.Ceil(ttl.Seconds())), err)

	fmt.Println(cache.Del("k"))

	_, err = cache.Get("k")
	fmt.Println(err)

	// output:
	// not found
	// <nil>
	// v <nil>
	// 60 <nil>
	// 59 <nil>
	// <nil>
	// not found
}

benchmark

chyroc			"github.com/Chyroc/filecache"
dannyBen		"github.com/DannyBen/filecache"
fabiorphp		"github.com/fabiorphp/cachego"
gadelkareem		"github.com/gadelkareem/cachita"
gookit			"github.com/gookit/cache"
huntsman		"github.com/huntsman-li/go-cache"
miguelmota		"github.com/miguelmota/go-filecache"
pkg: github.com/Chyroc/filecache-benchmark
BenchmarkFileCache/chyroc-8         	     200	   7135164 ns/op
BenchmarkFileCache/huntsman-8       	       2	 537270751 ns/op
BenchmarkFileCache/fabiorphp-8      	       3	 405211720 ns/op
BenchmarkFileCache/dannyBen-8       	       3	 385529791 ns/op
BenchmarkFileCache/gadelkareem-8    	       3	 403604442 ns/op
BenchmarkFileCache/miguelmota-8     	       1	3222506519 ns/op

Documentation

Index

Constants

View Source
const MaxLengthKey = 244
View Source
const MaxLengthValue = 1024

Variables

View Source
var FileSizeTooLarge = errors.New("file size too large(>100M)")
View Source
var HashConflict = errors.New("hash conflict")
View Source
var InvalidFileSize = errors.New("invalid file size")
View Source
var KeyTooLong = errors.New("key too long")
View Source
var KeyTooShort = errors.New("key too short")
View Source
var NotFound = errors.New("not found")
View Source
var ValueTooLong = errors.New("value too long")
View Source
var ValueTooShort = errors.New("value too short")

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(key string) (string, error)
	Set(key, val string, ttl time.Duration) error
	TTL(key string) (time.Duration, error)
	Expire(key string, ttl time.Duration) error
	Del(key string) error
	Range() ([]*KV, error)
}

func New

func New(filepath string) Cache

type CacheImpl

type CacheImpl struct {
	CurrentSize int
	// contains filtered or unexported fields
}

func (*CacheImpl) Del

func (r *CacheImpl) Del(key string) error

func (*CacheImpl) Expire

func (r *CacheImpl) Expire(key string, ttl time.Duration) error

func (*CacheImpl) Get

func (r *CacheImpl) Get(key string) (string, error)

func (*CacheImpl) Range

func (r *CacheImpl) Range() ([]*KV, error)

func (*CacheImpl) Set

func (r *CacheImpl) Set(key, val string, ttl time.Duration) error

func (*CacheImpl) TTL

func (r *CacheImpl) TTL(key string) (time.Duration, error)

type KV

type KV struct {
	Key string
	Val string
	TTL time.Duration
}

Directories

Path Synopsis
cmd
internal
gommap
Package gommap allows mapping files into memory.
Package gommap allows mapping files into memory.

Jump to

Keyboard shortcuts

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