tinykv

package module
v4.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2018 License: MIT Imports: 4 Imported by: 4

README

GoDoc Go Report Card

tinykv

tiny in-memory single-app kv (cache) with explicit and sliding expiration

v4

  • Heap-based expiration strategy

Get it using:

$ go get gopkg.in/dc0d/tinykv.v4

v3

  • Using only string keys,
  • Simplifying the interface,

Get it using:

$ go get gopkg.in/dc0d/tinykv.v3

v2

Get it using:

$ go get gopkg.in/dc0d/tinykv.v2

v1

Get it using:

$ go get gopkg.in/dc0d/tinykv.v1

Benchmarks:

$ go test -bench=.
BenchmarkGetNoValue-8          	20000000	        72.6 ns/op
BenchmarkGetValue-8            	20000000	        78.3 ns/op
BenchmarkGetSlidingTimeout-8   	10000000	       128 ns/op
BenchmarkPutOne-8              	10000000	       219 ns/op
BenchmarkPutN-8                	 2000000	       814 ns/op
BenchmarkPutExpire-8           	 2000000	       882 ns/op
BenchmarkCASTrue-8             	 5000000	       268 ns/op
BenchmarkCASFalse-8            	10000000	       260 ns/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCASCond = errorf("CAS COND FAILED")
)

errors

Functions

This section is empty.

Types

type KV

type KV interface {
	Delete(k string)
	Get(k string) (v interface{}, ok bool)
	Put(k string, v interface{}, options ...PutOption) error
	Take(k string) (v interface{}, ok bool)
	Stop()
}

KV is a registry for values (like/is a concurrent map) with timeout and sliding timeout

func New

func New(expirationInterval time.Duration, onExpire ...func(k string, v interface{})) KV

New creates a new *store, onExpire is for notification (must be fast).

Example
key := "KEY"
value := "VALUE"

kv := New(time.Millisecond * 10)
defer kv.Stop()
kv.Put(key, value)
v, ok := kv.Get(key)
if !ok {
	// ...
}
fmt.Println(key, v)
kv.Delete(key)
_, ok = kv.Get(key)
fmt.Println(ok)
Output:

KEY VALUE
false

type PutOption

type PutOption func(*putOpt)

PutOption extra options for put

func CAS

func CAS(cas func(oldValue interface{}, found bool) bool) PutOption

CAS for performing a compare and swap

func ExpiresAfter

func ExpiresAfter(expiresAfter time.Duration) PutOption

ExpiresAfter entry will expire after this time

func IsSliding

func IsSliding(isSliding bool) PutOption

IsSliding sets if the entry would get expired in a sliding manner

Directories

Path Synopsis
pkg
heap
Package heap provides heap operations for any type that implements heap.Interface.
Package heap provides heap operations for any type that implements heap.Interface.

Jump to

Keyboard shortcuts

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