mcache

package module
Version: v0.0.0-...-1a8195b Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2020 License: MIT Imports: 5 Imported by: 6

README

MCache library

Build Status Go Report Card GoDoc

go-mcache - this is a fast key:value storage. Its major advantage is that, being essentially a thread-safe .

map[string]interface{}

with expiration times, it doesn't need to serialize, and quick removal of expired keys.

Installation

~ $ go get -u github.com/OrlovEvgeny/go-mcache

Example a Pointer value (vary fast method)

type User struct {
	Name string
	Age  uint
	Bio  string
}

func main() {
	//Start mcache instance
	MCache = mcache.New()

	//Create custom key
	key := "custom_key1"
	//Create example struct
	user := &User{
		Name: "John",
		Age:  20,
		Bio:  "gopher 80 lvl",
	}

	//args - key, &value, ttl (or you need never delete, set ttl is mcache.TTL_FOREVER)
	err := MCache.Set(key, user, time.Minute*20)
	if err != nil {
		log.Fatal(err)
	}

	if data, ok := MCache.Get(key); ok {
		objUser:= data.(*User)
		fmt.Printf("User name: %s, Age: %d, Bio: %s\n", objUser.Name, objUser.Age, objUser.Bio)			
	}
}
Performance Benchmarks
goos: darwin
goarch: amd64
BenchmarkWrite-4          200000              7991 ns/op 
BenchmarkRead-4          1000000              1716 ns/op 
BenchmarkRW-4             300000              9894 ns/op
What should be done
  • the possibility of closing
  • r/w benchmark statistics
  • rejection of channels in safeMap in favor of sync.Mutex (there is an opinion that it will be faster)

License:

MIT

Documentation

Index

Constants

View Source
const TTL_FOREVER = time.Hour * 87660

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheDriver

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

CacheDriver context struct

func New

func New() *CacheDriver

New - returns CacheDriver struct

func StartInstance deprecated

func StartInstance() *CacheDriver

Deprecated: use New instead.

func (*CacheDriver) Close

func (mc *CacheDriver) Close() map[string]interface{}

Close - close all MCache

func (*CacheDriver) GCBufferQueue

func (mc *CacheDriver) GCBufferQueue() int

GCBufferQueue - returns the current use len KeyChan chanel buffer

func (*CacheDriver) Get

func (mc *CacheDriver) Get(key string) (interface{}, bool)

Get - returns serialize data

func (*CacheDriver) GetPointer deprecated

func (mc *CacheDriver) GetPointer(key string) (interface{}, bool)

Deprecated: use Get instead

func (*CacheDriver) Len

func (mc *CacheDriver) Len() int

Len - returns current count storage

func (*CacheDriver) Remove

func (mc *CacheDriver) Remove(key string)

Remove - value by key

func (*CacheDriver) Set

func (mc *CacheDriver) Set(key string, value interface{}, ttl time.Duration) error

Set - add cache data value

func (*CacheDriver) SetPointer deprecated

func (mc *CacheDriver) SetPointer(key string, value interface{}, ttl time.Duration) error

Deprecated: use Set instead

func (*CacheDriver) Truncate

func (mc *CacheDriver) Truncate()

Truncate - clean cache storage

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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