简体   繁体   中英

How to create a struct with auto scheduler?

My goal is to clean the map[string]time.Time from expired keys automatically instead of manual loop check. The expected result is that the key automatically delete itself instead of getting checked and deleted every 1 minute.

The current solution is do a loop and check it one by one :

  1. create the var addrs = map[string]time.Time{}
  2. start a goroutine and rely on for {} and time.Sleep() .
var addrs = map[string]time.Time{}

func StartCleanExpiredAddrs() {
    for {
        time.Sleep(1 * time.Minute)
        for addr, val := range addrs {
            if time.Now().After(val) {
                delete(addrs, addr)
            }
        }
    }
}
func main() {
  go StartCleanExpiredAddrs()
}

One thing that you could do is set up a timer that will tick when you want to expire the item. You should avoid this though if you are planning to store many items because it will need to consume more memory to store the goroutine that will be waiting for the timer Tick.

you can find the simple example I created here: https://go.dev/play/p/cXdTXKUwJcf

package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {
    // init cache
    cache := NewCacheWithExpiry()
    cache.addItem("key1", time.Now().Add(time.Second*5))
    cache.addItem("key2", time.Now().Add(time.Second*3))

    time.Sleep(time.Second*10)
}

type customCacheWithExpiry struct {
    addrs sync.Map
}

func NewCacheWithExpiry() *customCacheWithExpiry{
    return &customCacheWithExpiry{
        addrs: sync.Map{},
    }
}

func (c *customCacheWithExpiry) addItem(key string, val time.Time){
    fmt.Printf("storing key %s which expires at %s\n",key,val)
    c.addrs.Store(key, val)

    ticker := time.NewTicker(val.Sub(time.Now()))
    go func() {
        <-ticker.C
        fmt.Printf("deleting key %s\n", key)
        c.addrs.Delete(key)
    }()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM