Wednesday, March 22, 2023
HomeGolangShop in Go memory like localStorage Javascript? - Obtaining Aid

Shop in Go memory like localStorage Javascript? – Obtaining Aid


I have a semi-static food selection that ought to pack from a data source.

To make the tons of food selection quicker I ask yourself if there is a method to save this food selections as some kind of international variables? Consistent or otherwise.


You just require to produce a struct to map your information. Likewise you can make use of a basic cache collection like GitHub – patrickmn/go-cache: An in-memory secret: worth store/cache (comparable to Memcached) collection for Go, appropriate for single-machine applications.

I do not believe you require an exterior reliance for something this simple. Develop an international variable of some kind, guard it with a mutex so it’s thread-safe, after that upgrade it based upon some type of period. Something like:

// AppMenu shops the international application food selection things.
kind AppMenu struct {
Things [] string.
}

var (.
muAppMenu = && sync.RWMutex {}// Guards 'appMenu'.
appMenu AppMenu.
).

// GetAppMenu returns the existing application food selection.
func GetAppMenu() AppMenu {
muAppMenu.RLock().
food selection:= appMenu.
muAppMenu.RUnlock().
return food selection.
}

// SetAppMenu establishes the application food selection.
func SetAppMenu( food selection AppMenu) {
muAppMenu.Lock().
appMenu = food selection.
muAppMenu.Unlock().
}

You can after that someplace in your major feature rotate up a goroutine to upgrade your food selection thing building occasionally:

// Context for our goroutine.
ctx, terminate:= context.WithCancel( context.Background()).
postpone terminate().
// Rotate up goroutine to upgrade application food selection things every 30 secs.
go func() {
t:= time.NewTicker( 30 * time.Second).
postpone t.Stop().
for {
choose {
situation <

RELATED ARTICLES

Most Popular

Recent Comments