pcache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: MIT Imports: 1 Imported by: 1

README

PCache

PCache is goroutine local cache that is safe for concurrent use.

PCache does its best to cache items inside and do as little synchronization as possible, but since it is cache, there is no guarantee that PCache won't evict your item after Store.

PCache eviction policy is based on GC cycles, so it can evict all items from time to time.

You can limit the size of the cache per goroutine, and PCache evicts random items if I goroutine local cache achieves maxSizePerGoroutine size.

You can also use PCache as a superfast, tiny cache in front of another globally synchronized cache.

How to use it

You can import it as a library via Go modules or copy-paste pcache.go file it to your project and specify exact types that you need for your cache to achieve even better performance.

Benches

Screenshot 2020-09-14 at 23 25 26

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PCache

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

PCache is safe for concurrent use, goroutine local cache. All operations run in amortized constant time. PCache does its best to cache items inside and do as little synchronization as possible but since it is cache, there is no guarantee that PCache won't evict your item after Store.

PCache evicts random items if I goroutine local cache achieves maxSizePerGoroutine size. PCache cleans itself entirely from time to time.

The zero PCache is invalid. Use NewPCache method to create PCache.

func NewPCache

func NewPCache(maxSizePerGoroutine uint) *PCache

NewPCache creates PCache with maxSizePerGoroutine.

func (*PCache) Load

func (p *PCache) Load(key string) (interface{}, bool)

Load fetches (value, true) from cache associated with key or (nil, false) if it is not present.

func (*PCache) Store

func (p *PCache) Store(key string, value interface{})

Store stores value for a key in cache.

Jump to

Keyboard shortcuts

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