Thursday, March 16, 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 lots of food selection quicker I question if there is a method to keep this food selections as some sort of worldwide variables? Consistent or otherwise.


You just require to produce a struct to map your information. Additionally you can utilize 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. Produce 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 worldwide application food selection products.
kind AppMenu struct {
Things [] string.
}

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

// GetAppMenu returns the present 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 primary feature rotate up a goroutine to upgrade your food selection product residential property occasionally:

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

RELATED ARTICLES

Most Popular

Recent Comments