Skip to content
This repository was archived by the owner on Mar 19, 2025. It is now read-only.

hasansino/gcmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card Build Status

gcmap

Thread safe map with record TTL capability

Installation

~ $ go get -u github.com/hasansino/gcmap

Example usage

func main() {
	
    // create new instance
    st := gcmap.NewStorage(
        WithGCInterval(time.Minute),
        WithEntryTTL(time.Second * 30),
    )
    
    // create new key with value
    st.Store("key", "value")
    
    // retrieve value by key
    value, found := st.Load("key")
    if found {
    	fmt.Printf("Value is %v", value)
    }
    
    // update existing key
    st.StoreOrUpdate("key", "new_value", func(old, new interface{}) interface{} {
    	return old.(string) + new.(string) 
    })
    
    // delete key
    st.Delete("key")
}

Using StoreOrUpdate

type ComplexCounter struct {
	CounterA int64
	CounterB int64
}

func main() {
	
    st := gcmap.NewStorage()
    st.Store("key", ComplexCounter{CounterA: 5})
    
    // update existing complex structure
    
    st.StoreOrUpdate("key", ComplexCounter{CounterB: 10}, func(old, new interface{}) interface{} {
    	counter := old.(ComplexCounter)
    	counter.B = new.(ComplexCounter).B
    	return counter
    })
}

About

Thread safe map with record TTL capability

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages