Friday, May 3, 2024
HomeGolangRetailer in Go reminiscence like localStorage Javascript? - Getting Assist

Retailer in Go reminiscence like localStorage Javascript? – Getting Assist


I’ve a semi-static menu that ought to load from a database.

To make the load of menu quicker I’m wondering if there’s a technique to retailer this menus as some kind of world variables? Persistent or not.

You solely must create a struct to map your knowledge. Additionally you should utilize a basic cache library like GitHub – patrickmn/go-cache: An in-memory key:worth retailer/cache (much like Memcached) library for Go, appropriate for single-machine functions.

I don’t assume you want an exterior dependency for one thing this easy. Create a worldwide variable of some variety, guard it with a mutex so it’s thread-safe, then replace it based mostly on some form of interval. One thing like:

// AppMenu shops the worldwide app menu gadgets
kind AppMenu struct {
	Objects []string
}

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

// GetAppMenu returns the present app menu.
func GetAppMenu() AppMenu {
	muAppMenu.RLock()
	menu := appMenu
	muAppMenu.RUnlock()
	return menu
}

// SetAppMenu units the app menu.
func SetAppMenu(menu AppMenu) {
	muAppMenu.Lock()
	appMenu = menu
	muAppMenu.Unlock()
}

You possibly can then someplace in your primary perform spin up a goroutine to replace your menu merchandise property periodically:

// Context for our goroutine
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Spin up goroutine to replace app menu gadgets each 30 seconds
go func() {
	t := time.NewTicker(30 * time.Second)
	defer t.Cease()
	for {
		choose {
		case <-ctx.Accomplished():
			return // exit goroutine
		case <-t.C:
			menuItems := getMenuItemsFromDBOrWhatever()
			SetAppMenu(menuItems)
		}
	}
}()

Once more, you COULD add a dependency, however I don’t assume you want one. Additionally word that you’d presumably must populate the menu as soon as earlier than the ticker’s first tick. For extra particulars as to why:

I do hope I can keep away from dependencies. However there isn’t a want for redraw AFIAK. The menu must be static and tailor-made dynamically for every person. My aim is to interchange this Javascript code with Go if potential. Transfer this menu creation from browser to Go (Go templates) as an alternative

Is there a great possibility for caching that’s not +incompatible? Preferable in a position to retailer JSON as worth.

Sounds easy, however I don’t get this to work. And I don’t want any updating. It will likely be a dozen of static menus I can swap dynamically between. Saved in reminiscence hopefully.

package deal primary

import (
	"fmt"
	"sync"
)

kind AppMenu struct {
	Objects []map
}

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

// GetAppMenu returns the present app menu.
func GetAppMenu() AppMenu {
	muAppMenu.RLock()
	menu := appMenu
	muAppMenu.RUnlock()
	return menu
}

// SetAppMenu units the app menu.
func SetAppMenu(menu AppMenu) {
	muAppMenu.Lock()
	appMenu = menu
	muAppMenu.Unlock()
}

// Context for our goroutine
func primary() {
	menuItems := ("menu",`[{"key1":"value1"},{"key2":"value2"}]`)
	SetAppMenu(menuItems)
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments