sacache

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

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

Go to latest
Published: Jun 17, 2021 License: MIT Imports: 4 Imported by: 0

README

SaCache

SaCache(a.k.a Sashi Cache) is a fast, concurrent in-memory cache service written in pure Go. It uses gRPC for communicating with clients and supports item expiration function. It is still under development. The goal of SaCache is reducing extra costs(GC) as much as possible and making it faster and faster.

Usage

Please read sacache_test.go for lib usage purpose and read server.go & client.go for C-S usage purpose.

Benchmark

goos: linux
goarch: amd64
pkg: github.com/sashirin/sacache
cpu: Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
BenchmarkBigCacheSet-6      	     237	   5311201 ns/op	  12.34 MB/s	 3968662 B/op	      47 allocs/op
BenchmarkBigCacheGet-6      	     379	   3199278 ns/op	  20.48 MB/s	 1412395 B/op	  131099 allocs/op
BenchmarkBigCacheSetGet-6   	     147	   8616085 ns/op	  15.21 MB/s	 2814039 B/op	  131141 allocs/op
BenchmarkSaCacheSet-6       	     136	  10319922 ns/op	   6.35 MB/s	 6685742 B/op	  131294 allocs/op
BenchmarkSaCacheGet-6       	     628	   1912921 ns/op	  34.26 MB/s	  284503 B/op	   65773 allocs/op
BenchmarkSaCacheSetGet-6    	     144	  14731054 ns/op	   8.90 MB/s	 6998752 B/op	  196820 allocs/op
BenchmarkStdMapSet-6        	     150	   7829785 ns/op	   8.37 MB/s	  345839 B/op	   65551 allocs/op
BenchmarkStdMapGet-6        	     412	   2840184 ns/op	  23.07 MB/s	   31084 B/op	     164 allocs/op
BenchmarkStdMapSetGet-6     	      81	  43329190 ns/op	   3.03 MB/s	  417120 B/op	   65565 allocs/op
BenchmarkSyncMapSet-6       	      74	  16229905 ns/op	   4.04 MB/s	 3533916 B/op	  263947 allocs/op
BenchmarkSyncMapGet-6       	    1533	    772224 ns/op	  84.87 MB/s	    8302 B/op	     258 allocs/op
BenchmarkSyncMapSetGet-6    	     334	   3408553 ns/op	  38.45 MB/s	 3435798 B/op	  262543 allocs/op

License

SaCache is released under the MIT license (see LICENSE)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means given key is not found in the cache.
	ErrNotFound = errors.New("given key not found")
	// ErrExpired means given item is expired.
	ErrExpired = errors.New("item expired")
	// ErrQueueEmpty means queue is empty.
	ErrQueueEmpty = errors.New("queue empty")
)

Functions

This section is empty.

Types

type CacheItem

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

CacheItem is the item inside of cache table.

func (*CacheItem) ExpireTime

func (item *CacheItem) ExpireTime() time.Time

ExpireTime returns expire time of cache item.

func (*CacheItem) Key

func (item *CacheItem) Key() string

Key returns key of cache item.

func (*CacheItem) Value

func (item *CacheItem) Value() string

Value returns value of cache item.

type CacheShard

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

type Config

type Config struct {
	// time duration for removing expired items.
	CleanDuration time.Duration
	// shards
	ShardNumber int
	// Hasher
	Hasher Hasher
}

func DefaultConfig

func DefaultConfig() Config

func NewConfig

func NewConfig(clean time.Duration, shard int) Config

type Hasher

type Hasher interface {
	Hash(string) uint64
}

type PriorityQueue

type PriorityQueue []*CacheItem

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

Len returns the number of elements of queue.

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

Less is comparetor for expire time

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

Pop pop the front of queue or nil when queue is empty.

func (*PriorityQueue) PopItem

func (pq *PriorityQueue) PopItem() (*CacheItem, error)

Pop item from pq and auto re-adjust

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(item interface{})

Push push a pointer of cache item.

func (*PriorityQueue) PushItem

func (pq *PriorityQueue) PushItem(item *CacheItem)

Push item to pq and auto re-adjust

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Swap

func (PriorityQueue) TopItem

func (pq PriorityQueue) TopItem() (*CacheItem, error)

Front returns the front element or nil when queue is empty.

type SaCache

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

SaCache is a in-memory cache service.

func NewSaCache

func NewSaCache(name string, config Config) *SaCache

NewSaCache returns the pointer of a newly created SaCache instance.

func (*SaCache) Count

func (c *SaCache) Count() uint64

Count returns total elements in cache.

func (*SaCache) Delete

func (c *SaCache) Delete(key string) error

Delete deletes value given key.

func (*SaCache) Get

func (c *SaCache) Get(key string) (*CacheItem, error)

Get returns the CacheItem pointer of given key.

func (*SaCache) Set

func (c *SaCache) Set(key string, val string, expire time.Time) error

Set adds new k-v pair in the cache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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