Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a469692358 | ||
|
|
bce7131d9f |
@@ -38,13 +38,14 @@ func init() {
|
|||||||
// @receiver s *sIp2region: sIp2region的实例。
|
// @receiver s *sIp2region: sIp2region的实例。
|
||||||
func (s *sIp2region) Load() {
|
func (s *sIp2region) Load() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
var url = "https://github.com/ayflying/resource/raw/refs/heads/main/attachment/ip2region.xdb"
|
||||||
var dbPath = "runtime/library/ip2region.xdb"
|
var dbPath = "runtime/library/ip2region.xdb"
|
||||||
|
|
||||||
if gfile.IsEmpty(dbPath) {
|
if gfile.IsEmpty(dbPath) {
|
||||||
g.Log().Debug(ctx, "等待下载ip库文件")
|
g.Log().Debug(ctx, "等待下载ip库文件")
|
||||||
//下载文件
|
//下载文件
|
||||||
putData, err2 := g.Client().Discovery(nil).
|
putData, err2 := g.Client().Discovery(nil).Get(ctx, url)
|
||||||
Get(ctx, "https://resource.luoe.cn/attachment/ip2region.xdb")
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package aycache
|
package aycache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
v1 "github.com/ayflying/utility_go/api/system/v1"
|
||||||
|
"github.com/ayflying/utility_go/internal/boot"
|
||||||
"github.com/ayflying/utility_go/pkg/aycache/drive"
|
"github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
|
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
|
"github.com/ayflying/utility_go/service"
|
||||||
"github.com/gogf/gf/v2/os/gcache"
|
"github.com/gogf/gf/v2/os/gcache"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mod struct {
|
type Mod struct {
|
||||||
@@ -12,23 +18,23 @@ type Mod struct {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
QPSCount int
|
QPSCount int
|
||||||
//QPS = promauto.NewGauge(
|
QPS = promauto.NewGauge(
|
||||||
// prometheus.GaugeOpts{
|
prometheus.GaugeOpts{
|
||||||
// Name: "Cache_QPS",
|
Name: "Cache_QPS",
|
||||||
// Help: "当前缓存QPS数量",
|
Help: "当前缓存QPS数量",
|
||||||
// },
|
},
|
||||||
//)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
//boot.AddFunc(func() {
|
boot.AddFunc(func() {
|
||||||
// //初始化指标
|
//初始化指标
|
||||||
// service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
|
service.SystemCron().AddCron(v1.CronType_MINUTE, func() error {
|
||||||
// QPS.Set(math.Round(float64(QPSCount) / 60))
|
QPS.Set(math.Round(float64(QPSCount) / 60))
|
||||||
// QPSCount = 0
|
QPSCount = 0
|
||||||
// return nil
|
return nil
|
||||||
// })
|
})
|
||||||
//})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,9 +56,11 @@ func (m *randMod) RandomAll(data map[int]int, n int) []int {
|
|||||||
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
|
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
|
||||||
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
|
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
|
||||||
// 返回值: 随机选中的元素的索引
|
// 返回值: 随机选中的元素的索引
|
||||||
func RandByArrInt(s []interface{}) int {
|
func (m *randMod) RandByArrInt(_s interface{}) int {
|
||||||
// 初始化总权重为 0
|
// 初始化总权重为 0
|
||||||
sv := 0
|
sv := 0
|
||||||
|
s := gconv.Ints(_s)
|
||||||
|
|
||||||
// 遍历切片,累加每个元素的权重
|
// 遍历切片,累加每个元素的权重
|
||||||
for i := range s {
|
for i := range s {
|
||||||
sv += gconv.Int(s[i])
|
sv += gconv.Int(s[i])
|
||||||
@@ -73,7 +75,7 @@ func RandByArrInt(s []interface{}) int {
|
|||||||
var all int
|
var all int
|
||||||
// 再次遍历切片,累加权重
|
// 再次遍历切片,累加权重
|
||||||
for i := range s {
|
for i := range s {
|
||||||
all += gconv.Int(s[i])
|
all += s[i]
|
||||||
// 如果当前累加的权重大于随机数,则返回当前索引
|
// 如果当前累加的权重大于随机数,则返回当前索引
|
||||||
if all > r {
|
if all > r {
|
||||||
return i
|
return i
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
|
|||||||
// 从后向前遍历切片
|
// 从后向前遍历切片
|
||||||
for i := len(slice) - 1; i >= 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
// 检查当前元素是否等于需要移除的值
|
// 检查当前元素是否等于需要移除的值
|
||||||
if InArray(slice[i], value) {
|
if InArray[t](value, slice[i]) {
|
||||||
// 如果相等,移除该元素
|
// 如果相等,移除该元素
|
||||||
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
||||||
slice = append(slice[:i], slice[i+1:]...)
|
slice = append(slice[:i], slice[i+1:]...)
|
||||||
@@ -149,7 +149,7 @@ func RemoveSlice[t Number](slice []t, value ...t) []t {
|
|||||||
// @param value 需要查找的值
|
// @param value 需要查找的值
|
||||||
// @param array 进行查找的切片
|
// @param array 进行查找的切片
|
||||||
// @return bool 返回是否存在
|
// @return bool 返回是否存在
|
||||||
func InArray[t Number](value t, array []t) bool {
|
func InArray[t Number](array []t, value t) bool {
|
||||||
for _, v := range array {
|
for _, v := range array {
|
||||||
if v == value {
|
if v == value {
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in New Issue
Block a user