mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-03 22:06:37 +08:00
实现IP黑白名单、国家|地区封禁、省份封禁
This commit is contained in:
@@ -84,7 +84,7 @@ func (this *Grid) WriteInt64(key []byte, value int64, lifeSeconds int64) {
|
||||
Key: key,
|
||||
Type: ItemInt64,
|
||||
ValueInt64: value,
|
||||
ExpireAt: time.Now().Unix() + lifeSeconds,
|
||||
ExpireAt: UnixTime() + lifeSeconds,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -61,9 +61,12 @@ func TestMemoryGrid_Compress(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkMemoryGrid_Performance(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
grid := NewGrid(1024)
|
||||
for i := 0; i < b.N; i++ {
|
||||
grid.WriteInt64([]byte("key:"+strconv.Itoa(i)), int64(i), 3600)
|
||||
key := "key:" + strconv.Itoa(i)
|
||||
grid.WriteInt64([]byte(key), int64(i), 3600)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
internal/grids/time.go
Normal file
26
internal/grids/time.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package grids
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var unixTime = time.Now().Unix()
|
||||
var unixTimerIsReady = false
|
||||
|
||||
func init() {
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
unixTimerIsReady = true
|
||||
unixTime = time.Now().Unix()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
|
||||
func UnixTime() int64 {
|
||||
if unixTimerIsReady {
|
||||
return unixTime
|
||||
}
|
||||
return time.Now().Unix()
|
||||
}
|
||||
13
internal/grids/time_test.go
Normal file
13
internal/grids/time_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package grids
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUnixTime(t *testing.T) {
|
||||
for i := 0; i < 5; i++ {
|
||||
t.Log(UnixTime(), "real:", time.Now().Unix())
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user