UCache

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2022 License: MIT Imports: 4 Imported by: 4

README

UCache

high-speed thread-safe key-value all data in memory not-persistent auto recycling

usage

//import
import (
    "github.com/universe-30/UCache"
)
example
package main

import (
	"github.com/universe-30/UCache"
	"log"
)

type Person struct {
	Name     string
	Age      int
	Location string
}

func main() {
	 
	lc := UCache.New()  //new a UCache instance with default config
	lc.SetMaxRecords(10000)

	//set
	lc.Set("foo", "bar", 300)
	lc.Set("a", 1, 300)
	lc.Set("b", Person{"Jack", 18, "London"}, 300)
	lc.Set("b*", &Person{"Jack", 18, "London"}, 300)
	lc.Set("c", true, 100)

	//get
	value, ttlLeft, exist := lc.Get("foo")
	if exist {
		//value type is interface{}, please convert to the right type before usage
		valueStr, ok := value.(string)
		if ok {
			log.Println("key:foo, value:", valueStr)
		}
		log.Println("key:foo, ttl:", ttlLeft)
	}

	//get
	log.Println("---get---")
	log.Println(lc.Get("foo"))
	log.Println(lc.Get("a"))
	log.Println(lc.Get("b"))
	log.Println(lc.Get("b*"))
	log.Println(lc.Get("c"))

	//overwrite
	log.Println("---set overwrite---")
	log.Println(lc.Get("c"))
	lc.Set("c", false, 60)
	log.Println(lc.Get("c"))
}
default config
MaxRecords(*)         = 1000000
MinRecords            = 10000
MaxTTLSecs            = 7200
RecycleIntervalSecs   = 5
RecycleOverLimitRatio = 0.15
(* : configurable)
auto recycling

RecycleOverLimitRatio of records will be recycled automatically if MaxRecords is reached.

custom config
//new instance
lc,err := UCache.New()
if err != nil {
    panic(err.Error())
}
lc.SetMaxRecords(10000) //custom the max key-value pairs that can be kept in memory

Benchmark

set
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkLocalCache_SetPointer
BenchmarkLocalCache_SetPointer-8   	 1000000	      1618 ns/op	     379 B/op	      10 allocs/op
PASS
get
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkLocalCache_GetPointer
BenchmarkLocalCache_GetPointer-8   	 9931429	       129.7 ns/op	       0 B/op	       0 allocs/op
PASS

Documentation

Index

Constants

View Source
const (
	MaxRecords            = 1000000
	MinRecords            = 10000
	MaxTTLSecs            = 7200
	RecycleIntervalSecs   = 5
	RecycleOverLimitRatio = 0.15
)

Variables

This section is empty.

Functions

func GenRandStr added in v0.1.3

func GenRandStr(n int) string

Types

type Cache

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

func New

func New() *Cache

func (*Cache) Delete

func (lc *Cache) Delete(key string)

func (*Cache) Get

func (lc *Cache) Get(key string) (value interface{}, ttl int64, exist bool)

func (*Cache) GetLen

func (lc *Cache) GetLen() int64

func (*Cache) GetRand

func (lc *Cache) GetRand(key string) string

func (*Cache) Recycle added in v0.1.3

func (lc *Cache) Recycle()

func (*Cache) Set

func (lc *Cache) Set(key string, value interface{}, ttlSecond int64)

if ttl < 0 just return and nothing changes ttl is set to MaxTTLSecs if ttl > MaxTTLSecs if record exist , "0" ttl changes nothing if record not exist, "0" ttl is equal to "30" seconds

func (*Cache) SetMaxRecords added in v0.1.3

func (lc *Cache) SetMaxRecords(limit int64)

RecycleOverLimitRatio of records will be recycled if the number of total keys exceeds this limit

func (*Cache) SetRand

func (lc *Cache) SetRand(key string, ttlSecond int64) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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