Skip to content

Memoize functions - A go utility to cache and boost performance of subsequent functions calls with identical input.

License

Notifications You must be signed in to change notification settings

aesrael/go-memo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-memo

A simple go package to memoize go functions with a cache key for a specified time.

Installation

Install go

clone repo

go install

Usage

import (
	memoize "github.com/israeladura/go-memo"
)

func main() {
	// memoize for 60secs
	foo := memoize.Memoize("cache-foo", func() interface{} {
		return foobar("foo")
	}, 60)

	// memoize for 20secs
	bar := memoize.Memoize("cache-bar", func() interface{} {
		return foobar("bar")
	}, 20)

	fmt.Println(foo) // foo
	fmt.Println(foo) // prints cached result //foo

	fmt.Println(bar) // bar
	fmt.Println(bar) // prints cached result //bar

	result := memoize.Get("cache-foo")
	fmt.Println(result) // foo

	memoize.UnMemoize("cache-foo")
	foo = memoize.Get("cache-foo")
	bar = memoize.Get("cache-bar")
	fmt.Println(foo) // nil
	fmt.Println(bar) // bar

	memoize.ClearCache()
	foo = memoize.Get("cache-foo")
	bar = memoize.Get("cache-bar")
	fmt.Println(foo) // nil
	fmt.Println(bar) // nil
}

func foobar(x string) string {
	return x
}

Contributing

Pull requests are welcome.

TODO

  • Tests
  • Add more features.
  • Improve docs
  • Add example directory.

License

MIT

About

Memoize functions - A go utility to cache and boost performance of subsequent functions calls with identical input.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages