如何使用自动调度器创建一个结构体?

huangapple go评论82阅读模式
英文:

How to create a struct with auto scheduler?

问题

我的目标是自动清理map[string]time.Time中的过期键,而不是手动循环检查。期望的结果是,key会自动删除,而不是每隔1分钟进行检查和删除。

当前的解决方案是循环遍历并逐个检查

  1. 创建var addrs = map[string]time.Time{}
  2. 启动一个goroutine,并依赖于for {}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()
}
英文:

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()
}

答案1

得分: 2

你可以设置一个定时器,在你想要过期的项目时触发。不过,如果你计划存储许多项目,最好避免使用定时器,因为它需要消耗更多的内存来存储等待定时器触发的goroutine。

你可以在这里找到我创建的简单示例:
https://go.dev/play/p/cXdTXKUwJcf

package main

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

func main() {
    // 初始化缓存
	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("存储键 %s,过期时间为 %s\n", key, val)
	c.addrs.Store(key, val)

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

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 (
	&quot;fmt&quot;
	&quot;sync&quot;
	&quot;time&quot;
)

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

	time.Sleep(time.Second*10)
}

type customCacheWithExpiry struct {
	addrs sync.Map
}

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

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

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

huangapple
  • 本文由 发表于 2022年9月26日 17:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73852135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定