Skip to content

fengyoulin/gls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gls

A Goroutine Local Storage implementation base on the runtime unique ID of each goroutine.

$ go get github.com/fengyoulin/gls

Usage

import "github.com/fengyoulin/gls"

func All

func All() (kvs map[string]interface{}, ok bool)

All returns all the key/values in current goroutine's local storage

func Clr

func Clr()

Clr clears the current goroutine's local storage

func Del

func Del(key string)

Del deletes a key in current goroutine's local storage

func Get

func Get(key string) (val interface{}, ok bool)

Get get the value of a key in current goroutine's local storage

func Put

func Put(kvs map[string]interface{})

Put puts all the key/values into current goroutine's local storage

func Set

func Set(key string, val interface{})

Set set the value to a key in current goroutine's local storage

func Go

func Go(fn func(), c Cache)

Go start a new goroutine, inherit current goroutine's local storage

func GoWith

func GoWith(fn func(), c Cache, ls map[string]interface{})

GoWith start a new goroutine, with the given local storage

type Cache

type Cache interface {
	// All returns all the key/values in current goroutine's local storage
	All() (kvs map[string]interface{}, ok bool)
	// Put puts all the key/values into current goroutine's local storage
	Put(kvs map[string]interface{})
	// Clr clears the current goroutine's local storage
	Clr()
	// Del deletes a key in current goroutine's local storage
	Del(key string)
	// Get get the value of a key in current goroutine's local storage
	Get(key string) (val interface{}, ok bool)
	// Set set the value to a key in current goroutine's local storage
	Set(key string, val interface{})
}

Cache is the gls cache interface

func New

func New(shardingMode bool) Cache

New creates a goroutine local storage cache

Example

package main

import (
	"fmt"
	"github.com/fengyoulin/gls"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	gls.Set("gls", "v0.4.0")

	for i := 0; i < 10; i++ {
		wg.Add(1)
		i := i
		gls.Go(func() {
			defer wg.Done()
			if ls, ok := gls.All(); ok {
				fmt.Printf("%d, %v\n", i, ls)
			}
		}, nil)
	}
	wg.Wait()

	for i := 0; i < 10; i++ {
		wg.Add(1)
		gls.GoWith(func() {
			defer wg.Done()
			if ls, ok := gls.All(); ok {
				fmt.Printf("%v\n", ls)
			}
		}, nil, map[string]interface{}{"i": i})
	}
	wg.Wait()
}

The example before uses the default gls cache, you can use the gls.New() function to allocate a new gls cache for your purpose.

Attention

You should always try to use context.Context as google suggested. If you prefer to use this gls module, remember to call the Clr() at end of your goroutine, it will protect you from a memory leak.

About

A Goroutine Local Storage implementation base on the runtime unique ID of each goroutine.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages