Thursday, March 23, 2023
HomeGolangShop in Go memory like localStorage Javascript? - Obtaining Assistance

Shop in Go memory like localStorage Javascript? – Obtaining Assistance


I have a semi-static food selection that must fill from a data source.

To make the tons of food selection much faster I question if there is a means to save 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 make use of a basic cache collection like GitHub – patrickmn/go-cache: An in-memory trick: worth store/cache (comparable to Memcached) collection for Go, appropriate for single-machine applications.

I do not believe you require an outside 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 kind 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 major feature rotate up a goroutine to upgrade your food selection thing building occasionally:

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

RELATED ARTICLES

Most Popular

Recent Comments