|
@@ -3,6 +3,7 @@ package front
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "github.com/gogf/gf/v2/container/gvar"
|
|
|
"jy/src/jfw/config"
|
|
|
"jy/src/jfw/jyutil"
|
|
|
"jy/src/jfw/paging"
|
|
@@ -45,7 +46,11 @@ type reqLimit struct {
|
|
|
waitPool chan struct{}
|
|
|
}
|
|
|
|
|
|
-var reqLimitInit *reqLimit
|
|
|
+var (
|
|
|
+ reqLimitInit *reqLimit
|
|
|
+ BlogQueryLock = new(sync.RWMutex)
|
|
|
+ HotWordQueryLock = new(sync.RWMutex)
|
|
|
+)
|
|
|
|
|
|
func init() {
|
|
|
xweb.AddAction(&Tags{})
|
|
@@ -578,13 +583,41 @@ func (this *Tags) GetStype(href string) (list []map[string]interface{}) {
|
|
|
return list
|
|
|
}
|
|
|
|
|
|
+// GetL2CacheData 二级缓存
|
|
|
+// 注意 lock定义为全局锁
|
|
|
+func GetL2CacheData(dbName string, lock *sync.RWMutex, key string, f func() interface{}, cacheSec int) *gvar.Var {
|
|
|
+ var (
|
|
|
+ data = gvar.New(redis.Get(dbName, key))
|
|
|
+ newData *gvar.Var
|
|
|
+ )
|
|
|
+ if data.IsEmpty() {
|
|
|
+ data = gvar.New(redis.Get(dbName, fmt.Sprintf("%s_L2Cache", key)))
|
|
|
+ go func() {
|
|
|
+ if lock.TryLock() {
|
|
|
+ defer lock.Unlock()
|
|
|
+ if n := f(); n != nil {
|
|
|
+ newData = gvar.New(n)
|
|
|
+ go func() {
|
|
|
+ redis.Put(dbName, key, newData, cacheSec)
|
|
|
+ redis.Put(dbName, fmt.Sprintf("%s_L2Cache", key), newData, -1)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ if data.IsEmpty() {
|
|
|
+ time.Sleep(100 * time.Millisecond)
|
|
|
+ if !newData.IsEmpty() {
|
|
|
+ data = newData
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
// 剑鱼博客
|
|
|
-func (this *Tags) GetConsult() (list []map[string]interface{}) {
|
|
|
+func (this *Tags) GetConsult() []map[string]interface{} {
|
|
|
rediskey := fmt.Sprintf("pcseo_jybk")
|
|
|
- if l, ok := redis.Get("seoCache", rediskey).([]interface{}); ok && l != nil && len(l) > 0 {
|
|
|
- list = qu.ObjArrToMapArr(l)
|
|
|
- } else {
|
|
|
-
|
|
|
+ res := GetL2CacheData("seoCache", BlogQueryLock, rediskey, func() interface{} {
|
|
|
rs, _, _ := jyutil.JyCmsSearch(map[string]string{
|
|
|
"perPage": "10",
|
|
|
}, "/jyblog/index_%s.html")
|
|
@@ -595,11 +628,14 @@ func (this *Tags) GetConsult() (list []map[string]interface{}) {
|
|
|
delete(v, "s_source")
|
|
|
v["url"] = fmt.Sprintf("/jyblog/%s.html", qu.ObjToString(v["_id"]))
|
|
|
}
|
|
|
- list = *rs
|
|
|
- redis.Put("seoCache", rediskey, list, cacheTime)
|
|
|
}
|
|
|
+ return rs
|
|
|
+ }, cacheTime)
|
|
|
+ log.Println(res.Maps())
|
|
|
+ if !res.IsEmpty() {
|
|
|
+ return res.Maps()
|
|
|
}
|
|
|
- return list
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
func (this *Tags) GetLetterMap(pageSize, pageNum int64, letter string) ([]map[string]interface{}, int64) {
|
|
@@ -627,9 +663,7 @@ func (this *Tags) GetLetterMap(pageSize, pageNum int64, letter string) ([]map[st
|
|
|
|
|
|
func (this *Tags) GetHotLabel(length int64) []map[string]interface{} {
|
|
|
rediskey := fmt.Sprintf("pcseo_getHotLabel_%v", length)
|
|
|
- if l, ok := redis.Get("seoCache", rediskey).([]interface{}); ok && l != nil && len(l) > 0 {
|
|
|
- return qu.ObjArrToMapArr(l)
|
|
|
- } else {
|
|
|
+ res := GetL2CacheData("seoCache", HotWordQueryLock, rediskey, func() interface{} {
|
|
|
m := []map[string]interface{}{}
|
|
|
sql := `select id,name,letter from seo_words.seo_resource where state1=2`
|
|
|
cql := `select count(1) from seo_words.seo_resource where state1=2`
|
|
@@ -650,8 +684,10 @@ func (this *Tags) GetHotLabel(length int64) []map[string]interface{} {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
- redis.Put("seoCache", rediskey, m, cacheTime)
|
|
|
return m
|
|
|
+ }, cacheTime)
|
|
|
+ if !res.IsEmpty() {
|
|
|
+ return res.Maps()
|
|
|
}
|
|
|
return nil
|
|
|
}
|