go_mcache

package module
v1.0.0-...-f3c19c9 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2018 License: MIT Imports: 6 Imported by: 0

README

MCache library

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 github.com/OrlovEvgeny/go-mcache

Example a Pointer value (vary fast method)

var MCache *mcache.CacheDriver

type User {
	Name string
	Age uint
	Bio string
}

func main() {
	MCache = mcache.StartInstance()
	
	key := "key1"
	
	user := new(User)
	user.Name = "John"
	user.Age = 20
	user.Bio = "gopher"
	//args - key, &value, ttl
	MCache.SetPointer(key, user, time.Minute*20)
	if err != nil {
		log.Println("MCACHE SET ERROR: ", err)
	}
	
	if pointer, ok := mcache.GetPointer(key); ok {
		if objUser, ok := pointer.(*User); ok {
			fmt.Printf("User name: %s, Age: %d, Bio: %s\n", objUser.Name, objUser.Age, objUser,Bio)
		}
	} else {
		log.Println("Cache by key: %s not found", key)
	}
}

Example serialize and deserialize value

var MCache *mcache.CacheDriver

type User {
	Name string
	Age uint
	Bio string
}

func main() {
	MCache = mcache.StartInstance()
	
	key := "key1"
	
	userSet := new(User)
	userSet.Name = "John"
	userSet.Age = 20
	userSet.Bio = "gopher"
	
	//args - key, &value, ttl
	MCache.Set(key, userSet, time.Minute*20)
	if err != nil {
		log.Println("MCACHE SET ERROR: ", err)
	}
	
	
	var userGet User
	if ok := mcache.Get(key, &userGet); ok {
            fmt.Printf("User name: %s, Age: %d, Bio: %s\n", userGet.Name, userGet.Age, userGet,Bio)
    } else {
    	log.Println("Cache by key: %s not found", key)
    }
}

dependency use: msgpack

License:

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheDriver

type CacheDriver struct{}

func StartInstance

func StartInstance() *CacheDriver

func (*CacheDriver) Get

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

func (*CacheDriver) GetPointer

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

func (*CacheDriver) Len

func (mc *CacheDriver) Len() int

func (*CacheDriver) Remove

func (mc *CacheDriver) Remove(key string)

func (*CacheDriver) Set

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

func (*CacheDriver) SetPointer

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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