Saturday, March 25, 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 must fill from a data source.

To make the tons of food selection much faster I ask yourself if there is a method to keep this food selections as some kind of international 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 secret: worth store/cache (comparable to Memcached) collection for Go, ideal for single-machine applications.

I do not believe you require an outside dependence 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 international application food selection things.
kind AppMenu struct {
Products [] string.

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

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

// SetAppMenu establishes the application food selection.
func SetAppMenu( food selection AppMenu) {
appMenu = food selection.

You might after that someplace in your primary feature rotate up a goroutine to upgrade your food selection thing residential property regularly:

// 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 {
instance <


Most Popular

Recent Comments