package timercache

import (
	"fmt"
	"os"
	"os/signal"
	"time"
)

//package timer
// https://blog.csdn.net/li_101357/article/details/90111230
// https://studygolang.com/articles/13173

// https://github.com/caucy/timeloop/blob/master/timer/timer.go
// https://studygolang.com/articles/13060
var saveTimerCache *CustomCacheKeys

func exmaple() {
	saveTimerCache = NewCustomKeys(time.Second * 5)

	//正常流程
	fmt.Println(saveTimerCache)

	callback1 := func(e *TimerEntry) {
		fmt.Println(e.GetVal())
	}

	saveTimerCache.Add("1", 1, callback1)
	saveTimerCache.Add("2", 2, callback1)

	//等待过程中进行修改
	saveTimerCache.Add("1", 3, callback1)

	//执行过程中进行修改
	saveTimerCache.Add("4", 4, func(e *TimerEntry) {
		fmt.Println(e.GetVal())

		saveTimerCache.Add("4", 5, callback1)
	})

	//循环缓存
	var callback2 func(e *TimerEntry)
	callback2 = func(e *TimerEntry) {
		fmt.Println(e.GetVal())

		saveTimerCache.Add("6", 6, callback2)
		fmt.Printf("%#v", saveTimerCache)
	}

	saveTimerCache.Add("6", 6, callback2)

	//等待时 删除
	saveTimerCache.Add("7", 7, callback1)
	saveTimerCache.Remove("7")

	//执行时删除
	var callback3 func(e *TimerEntry)
	callback3 = func(e *TimerEntry) {
		fmt.Println(e.GetVal())

		saveTimerCache.Remove("8")
	}
	saveTimerCache.Add("8", 8, callback3)

	c1 := make(chan os.Signal, 1)
	signal.Notify(c1, os.Interrupt, os.Kill)
	sig := <-c1
	fmt.Printf("Grave closing down (signal: %v)", sig)
}
