fastcache

package module
v0.0.0-...-ebf01de Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

Introduction

This is a high-performance, multi-process shared memory caching system implemented in Go. It supports Get, Set, and Delete operations, and is designed for multi-thread safety, high performance, and low latency. The cache system can be configured to use various memory allocation strategies, including Go memory, SHM, and MMap, and features zero GC.

Features

  • support Multi-process shared memory caching
  • High performance and low latency
  • Supports various memory allocation strategies (Go memory, SHM, MMap, etc.)
  • Zero GC
  • support LRU

Usage

cache, err := fastcache.NewCache(fastcache.GB, &fastcache.Config{
    Shards:     sharding,
    MemoryType: fastcache.SHM, // fastcache.GO fastcache.MMAP
    MemoryKey:  "/tmp/BenchmarkFastCache_Set",
})
if err != nil {
    panic(err)
}

err = cache.Set("k1", []byte("v1"))
fmt.Println("set err: ", err)

value, err := cache.Get("k1")
fmt.Println("value: ", value, "err: ", err)

Benchmark

goos: windows
goarch: amd64
pkg: github.com/leslie-fei/fastcache/benchmark
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkFastCache_Set
BenchmarkFastCache_Set-8         	10273144	       113.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastCache_Get
BenchmarkFastCache_Get-8         	10430319	       113.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastCache_SetAndGet
BenchmarkFastCache_SetAndGet-8   	10952601	       112.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkBigCache_Set
BenchmarkBigCache_Set-8          	 4479901	       232.1 ns/op	    1465 B/op	       0 allocs/op
BenchmarkBigCache_Get
BenchmarkBigCache_Get-8          	12057295	       105.2 ns/op	     568 B/op	       2 allocs/op
BenchmarkBigCache_SetAndGet
BenchmarkBigCache_SetAndGet-8    	14535200	       111.0 ns/op	     638 B/op	       1 allocs/op
BenchmarkRistretto_Set
BenchmarkRistretto_Set-8         	11762238	       201.1 ns/op	     141 B/op	       3 allocs/op
BenchmarkRistretto_Get
BenchmarkRistretto_Get-8         	26380351	        64.25 ns/op	      17 B/op	       1 allocs/op
BenchmarkRistretto_SetAndGet
BenchmarkRistretto_SetAndGet-8   	21141976	        55.83 ns/op	      35 B/op	       1 allocs/op
BenchmarkTheine_Set
BenchmarkTheine_Set-8            	 2599737	       496.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkTheine_Get
BenchmarkTheine_Get-8            	34772025	        30.95 ns/op	       0 B/op	       0 allocs/op
BenchmarkTheine_SetAndGet
BenchmarkTheine_SetAndGet-8      	12663650	       107.9 ns/op	       1 B/op	       0 allocs/op
PASS

Documentation

Index

Constants

View Source
const (
	GO   MemoryType = 1
	SHM             = 2
	MMAP            = 3
)
View Source
const (
	KB = 1024
	MB = 1024 * KB
	GB = 1024 * MB
)

Variables

View Source
var (
	ErrNoSpace            = errors.New("memory no space")
	ErrMemorySizeTooSmall = errors.New("memory size too small")
	ErrNotFound           = errors.New("key not found")
	ErrIndexOutOfRange    = errors.New("index out of range")
	ErrFreeListIsEmpty    = errors.New("free list is empty")
	ErrLRUListIsEmpty     = errors.New("lru list is empty")
	ErrCacheClosed        = errors.New("cache closed")
	ErrCloseTimeout       = errors.New("cache close timeout")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Has check if the key exists in the cache.
	Has(key []byte) bool
	// HasWithCounter check if the key exists in the cache and return with counter
	HasWithCounter(key []byte) (uint8, bool)
	// GetWithCounter get key in the cache and return with counter
	GetWithCounter(key []byte) ([]byte, uint8, error)
	// GetBufferWithCounter get key with buffer and return with counter
	GetBufferWithCounter(key []byte, buffer io.Writer) (uint8, error)
	// Get value for the key, it returns ErrNotFound when key not exists
	// and LRU move to front
	Get(key []byte) ([]byte, error)
	// GetWithBuffer write value into buffer, it returns ErrNotFound when key not exists
	// and LRU move to front
	GetWithBuffer(key []byte, buffer io.Writer) error
	// Set key and value
	Set(key []byte, value []byte) error
	// Peek value for key, but it will not move LRU
	Peek(key []byte) ([]byte, error)
	// PeekWithBuffer write value into buffer, but it will not move LRU
	PeekWithBuffer(key []byte, buffer io.Writer) error
	// Delete value for key
	Delete(key []byte) error
	// Close the cache wait for the ongoing operations to complete, and return ErrCloseTimeout if timeout
	Close() error
}

func NewCache

func NewCache(size int, c *Config) (Cache, error)

type Config

type Config struct {
	// memory type in GO SHM MMAP
	MemoryType MemoryType
	// shard memory key
	MemoryKey string
	// 支持存储的最大数量, 超过将会触发LRU
	MaxElementLen uint64
	// 大数据块的最大数量, 超过这个数量将会触发淘汰, 并且这个数值将会用来初始化大数据块的定长Hashmap
	MaxBigDataLen uint64
	// 定义多少字节为大数据块
	BigDataSize uint32
	// 当每个分片中的空闲内存不足时会去总内存申请, 分片每次申请内存大小
	ShardPerAllocSize uint64
	// 分片数量
	Shards uint32
	// hash算法
	Hasher HashFunc `json:"-"`
}

func DefaultConfig

func DefaultConfig() *Config

type HashFunc

type HashFunc func(s []byte) uint64

type Locker

type Locker interface {
	sync.Locker
}

type Memory

type Memory interface {
	// Attach attach memory
	Attach() error
	// Detach detach memory
	Detach() error
	// Ptr first ptr
	Ptr() unsafe.Pointer
	// Size memory total size
	Size() uint64
	// PtrOffset offset Get ptr
	PtrOffset(offset uint64) unsafe.Pointer
	// Travel memory
	Travel(skipOffset uint64, fn func(ptr unsafe.Pointer, size uint64) uint64)
}

Memory 内存块抽象

type MemoryType

type MemoryType int

type StringKeyCache

type StringKeyCache interface {
	Cache
	GetStringKey(key string) ([]byte, error)
	GetStringKeyWithBuffer(key string, buffer io.Writer) error
	SetStringKey(key string, value []byte) error
	PeekStringKey(key string) ([]byte, error)
	PeekStringKeyWithBuffer(key string, buffer io.Writer) error
	DeleteStringKey(key string) error
}

Jump to

Keyboard shortcuts

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