gcache

package module
v0.0.0-...-36f42ef Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

image

What is gcache?

gcache is a high-performance Go language implementation of a cache library. It is easy to use and understand

Design principle?

  • LRU elimination algorithm
  • Use singleflight to avoid cache pinging
  • Support custom load method
  • Support cache change listener

TODO

Map shard

How to install ?

go get -u github.com/wang1309/gcache

use case

init cache object

import (
	"sync"
	"time"
	"golang.org/x/sync/singleflight"
	"github.com/wang1309/gcache"
)

const maxItems = 100

func main() {
	// create cache object
	c := cache.NewCache(loader, maxItems)
}

func loader(key string) interface{} {
	// get data by key
	return "value"
}

Get

value := c.Get("key")

Put

c.Put("key", "value")

Benchmark

ENV

goos: darwin goarch: arm64

BenchmarkCache_GetParallel/Get-8                 3000000               314.4 ns/op
BenchmarkCache_Put-8     3000000               190.4 ns/op

Documentation

Index

Constants

View Source
const DefaultMaxCheckKeyNumber = 20

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Stats CacheStats

	Listeners []func(key string, value interface{})
	// contains filtered or unexported fields
}

func NewCache

func NewCache(loader func(key string) interface{}, maxItems int) *Cache

func (*Cache) AddListener

func (c *Cache) AddListener(fun func(key string, value interface{}))

func (*Cache) Clear

func (c *Cache) Clear()

func (*Cache) Get

func (c *Cache) Get(key string) interface{}

func (*Cache) Items

func (c *Cache) Items() map[string]CacheItem

func (*Cache) Put

func (c *Cache) Put(key string, value interface{})

type CacheItem

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

type CacheStats

type CacheStats struct {
	Hits   int64
	Misses int64
}

Jump to

Keyboard shortcuts

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