Back to godoc.org

Package group

v0.3.3
Latest Go to latest

The latest major version is .

Published: Jan 20, 2020 | License: MIT | Module: github.com/bilibili/kratos

Overview

Package group provides a sample lazy load container. The group only creating a new object not until the object is needed by user. And it will cache all the objects to reduce the creation of object.

Index

Examples

type Group

type Group struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Group is a lazy load container.

func NewGroup

func NewGroup(new func() interface{}) *Group

NewGroup news a group container.

func (*Group) Clear

func (g *Group) Clear()

Clear deletes all objects.

func (*Group) Get

func (g *Group) Get(key string) interface{}

Get gets the object by the given key.

Example

Code:

new := func() interface{} {
	fmt.Println("Only Once")
	return &Counter{}
}
group := NewGroup(new)

// Create a new Counter
group.Get("pass").(*Counter).Incr()

// Get the created Counter again.
group.Get("pass").(*Counter).Incr()
Only Once

func (*Group) Reset

func (g *Group) Reset(new func() interface{})

Reset resets the new function and deletes all existing objects.

Example

Code:

new := func() interface{} {
	return &Counter{}
}
group := NewGroup(new)

newV2 := func() interface{} {
	fmt.Println("New V2")
	return &Counter{}
}
// Reset the new function and clear all created objects.
group.Reset(newV2)

// Create a new Counter
group.Get("pass").(*Counter).Incr()
New V2

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier