Categorygithub.com/fanjindong/go-cache
modulepackage
0.0.5
Repository: https://github.com/fanjindong/go-cache.git
Documentation: pkg.go.dev

# README

go-cache

CI GoDoc

中文文档

image

Provides a memory-based cache package for Gopher.

Install

go get -u github.com/fanjindong/go-cache

Fast Start

import "github.com/fanjindong/go-cache"

func main() {
    c := cache.NewMemCache()
    c.Set("a", 1)
    c.Set("b", 1, cache.WithEx(1*time.Second))
    time.sleep(1*time.Second)
    c.Get("a") // 1, true
    c.Get("b") // nil, false
}

Performance Benchmark

In the concurrent scenario, it has three times the performance improvement compared to github.com/patrickmn/go-cache.

benchmark

BenchmarkGoCacheConcurrentSetWithEx-8            	 3040422	       371 ns/op
BenchmarkPatrickmnGoCacheConcurrentSetWithEx-8   	 1000000	      1214 ns/op
BenchmarkGoCacheConcurrentSet-8                  	 2634070	       440 ns/op
BenchmarkPatrickmnGoCacheConcurrentSet-8         	 1000000	      1204 ns/op

Advanced

Sharding

You can define the size of the cache object's storage sharding set as needed. The default is 1024. When the amount of data is small, define a small sharding set size, you can get memory improvement. When the data volume is large, you can define a large sharding set size to further improve performance.

cache.NewMemCache(cache.WithShards(8))

ExpiredCallback

You can define a callback function func(k string, v interface{}) error that will be executed when a key-value expires (only expiration triggers, delete or override does not trigger).

import (
	"fmt"
	"github.com/fanjindong/go-cache"
)

func main() {
    f := func(k string, v interface{}) error{
        fmt.Println("ExpiredCallback", k, v)
        return nil
    }
    c := cache.NewMemCache(cache.WithExpiredCallback(f))
    c.Set("k", 1)
    c.Set("kWithEx", 1, cache.WithEx(1*time.Second))
    time.sleep(1 * time.Second)
    c.Get("k")       // 1, true
    c.Get("kWithEx") // nil, false
    // output: ExpiredCallback kWithEx, 1
}

ClearInterval

go-cache clears expired cache objects periodically. The default interval is 1 second. Depending on your business scenario, choosing an appropriate cleanup interval can further improve performance.

import "github.com/fanjindong/go-cache"

func main() {
    c := cache.NewMemCache(cache.WithClearInterval(1*time.Minute))
}

# Functions

No description provided by the author
No description provided by the author
WithClearInterval set custom clear interval.Interval for clearing expired key-value pairs.
WithEx Set the specified expire time, in time.Duration.
WithExAt Set the specified expire deadline, in time.Time.
WithExpiredCallback set custom expired callback functionThis callback function is called when the key-value pair expires.
WithHash set custom hash key function.
WithShards set custom size of sharding.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
IHash is responsible for generating unsigned, 64-bit hash of provided string.
No description provided by the author

# Type aliases

ExpiredCallback Callback the function when the key-value pair expires Note that it is executed after expiration.
ICacheOption The option used to create the cache object.
SetIOption The option used to cache set.