Giter Club home page Giter Club logo

go-weakref's Introduction

Weakref

Feature

  • Weakref for golang
  • Support weakref map (weak value)
  • Check if value available

Usage

type WeakRef

A weakref instance

func WeakRef.Ok

func (*WeakRef)Ok()(bool)

Check if this weakref is available

func WeakRef.Value

func (*WeakRef)Value()(interface{})

Get the value of the weakref

func NewWeakRef

func NewWeakRef(ptr interface{})(w *WeakRef)

Create a instance of weakref.WeakRef, ptr must be a type of pointer or unsafe.Pointer

type Map

A weak value map, if the value is not available, the map will auto remove the key
The map is theard safty

func Map.Len

func (*Map)Len()()

func Map.Has

func (*Map)Has(k interface{})(bool)

Check if key-value exists

func Map.Get

func (*Map)Get(k interface{})(interface{})

Get value of key

func Map.Set

func (*Map)Set(k interface{}, v interface{})(interface{})

Set value of key, and return the value

func Map.GetOrSet

func (*Map)GetOrSet(k interface{}, setter func()(interface{}))(interface{})

Get value of key, if key not exists, set the value returned by setter, and return the value

func Map.Pop

func (*Map)Pop(k interface{})(interface{})

Remove value of the key, and return the value

func Map.Reset

func (*Map)Reset()

Reset the map (clear all of the key-value)

func Map.AsMap

func (*Map)AsMap()(map[interface{}]interface{})

Create a new builtin.map, and copy all the key-value from weakref.Map

func NewMap

func NewMap()(*Map)

Create a new empty weakref.Map

Example for weakref.WeakRef:

import (
	weakref "github.com/zyxkad/weakref"
)

type T struct{
	id int
	name string
}

var global_cache_t *weakref.WeakRef

func GetT()(*T){
	if global_cache_t.Ok(){ // If this weakref is available
		return global_cache_t.Value().(*T) // type assertion
	}
	t := &T{
		id: 1,
		name: "name",
	}

	global_cache_t = weakref.NewWeakRef(t) // create a new weakref

	return t
}

// ...

func main(){
	for {
		// ...
		if true {
			t := GetT()
			do_something_with_t(t)
		}
		// ...
	}
}

Example for weakref.Map:

import (
	weakref "github.com/zyxkad/weakref"
)

type User struct{
	name string
	score int
}

var weak_cache *weakref.Map = weakref.NewMap()

func ReadUserFromFile(name string)(u *User){
	// ...
	return
}

func GetUser(name string)(*User){
	return weak_cache.GetOrSet(name, func()(interface{}){
		return ReadUserFromFile(name)
	}).(*User)
}


func main(){
	for {
		// ...
		{
			a := GetUser("a")
			a.score++
			SaveUser(a)
		}
		{
			b := GetUser("b")
			b.score--
			SaveUser(b)
		}
		// ...
}

go-weakref's People

Contributors

zyxkad avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.