搜索单独缓存
This commit is contained in:
parent
247593cb57
commit
477fd126f4
|
@ -22,6 +22,7 @@ var recentCommentsCaches *cache.SliceCache[models.WpComments]
|
||||||
var postCommentCaches *cache.MapCache[uint64, []models.WpComments]
|
var postCommentCaches *cache.MapCache[uint64, []models.WpComments]
|
||||||
var postsCache *cache.MapCache[uint64, models.WpPosts]
|
var postsCache *cache.MapCache[uint64, models.WpPosts]
|
||||||
var monthPostsCache *cache.MapCache[string, []uint64]
|
var monthPostsCache *cache.MapCache[string, []uint64]
|
||||||
|
var postListIdsCache *cache.MapCache[string, PostIds]
|
||||||
var searchPostIdsCache *cache.MapCache[string, PostIds]
|
var searchPostIdsCache *cache.MapCache[string, PostIds]
|
||||||
|
|
||||||
func InitActionsCommonCache() {
|
func InitActionsCommonCache() {
|
||||||
|
@ -30,13 +31,15 @@ func InitActionsCommonCache() {
|
||||||
setCacheFunc: archives,
|
setCacheFunc: archives,
|
||||||
}
|
}
|
||||||
|
|
||||||
searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, time.Hour)
|
searchPostIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.SearchPostCacheTime)
|
||||||
|
|
||||||
monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, time.Hour)
|
postListIdsCache = cache.NewMapCache[string, PostIds](searchPostIds, vars.Conf.PostListCacheTime)
|
||||||
|
|
||||||
|
monthPostsCache = cache.NewMapCache[string, []uint64](monthPost, vars.Conf.MonthPostCacheTime)
|
||||||
|
|
||||||
postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime)
|
postContextCache = cache.NewMapCache[uint64, PostContext](getPostContext, vars.Conf.ContextPostCacheTime)
|
||||||
|
|
||||||
postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, time.Hour)
|
postsCache = cache.NewMapBatchCache[uint64, models.WpPosts](getPostsByIds, vars.Conf.PostDataCacheTime)
|
||||||
postsCache.SetCacheFunc(getPostById)
|
postsCache.SetCacheFunc(getPostById)
|
||||||
|
|
||||||
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
||||||
|
@ -44,6 +47,7 @@ func InitActionsCommonCache() {
|
||||||
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
||||||
|
|
||||||
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
|
recentCommentsCaches = cache.NewSliceCache[models.WpComments](recentComments, vars.Conf.RecentCommentsCacheTime)
|
||||||
|
|
||||||
postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5)
|
postCommentCaches = cache.NewMapCache[uint64, []models.WpComments](postComments, time.Minute*5)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,12 +22,14 @@ func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.WpPosts, error)
|
||||||
return postsCache.GetCacheBatch(ctx, ids, time.Second, ids)
|
return postsCache.GetCacheBatch(ctx, ids, time.Second, ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetPostCache(ids []models.WpPosts) error {
|
func SearchPost(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) {
|
||||||
var arg []uint64
|
ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...)
|
||||||
for _, posts := range ids {
|
if err != nil {
|
||||||
arg = append(arg, posts.Id)
|
return
|
||||||
}
|
}
|
||||||
return postsCache.SetByBatchFn(arg)
|
total = ids.Length
|
||||||
|
r, err = GetPostsByIds(ctx, ids.Ids)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPostById(id ...any) (post models.WpPosts, err error) {
|
func getPostById(id ...any) (post models.WpPosts, err error) {
|
||||||
|
@ -89,8 +91,8 @@ func getPostsByIds(ids ...any) (m map[uint64]models.WpPosts, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchPostIds(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) {
|
func PostLists(ctx context.Context, key string, args ...any) (r []models.WpPosts, total int, err error) {
|
||||||
ids, err := searchPostIdsCache.GetCache(ctx, key, time.Second, args...)
|
ids, err := postListIdsCache.GetCache(ctx, key, time.Second, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -180,8 +180,10 @@ func Index(c *gin.Context) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else if h.search != "" {
|
||||||
|
postIds, totalRaw, err = common.SearchPost(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status)
|
||||||
} else {
|
} else {
|
||||||
postIds, totalRaw, err = common.SearchPostIds(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status)
|
postIds, totalRaw, err = common.PostLists(c, h.getSearchKey(), h.where, h.page, h.pageSize, models.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
17
cache/map.go
vendored
17
cache/map.go
vendored
|
@ -99,8 +99,9 @@ func (m *MapCache[K, V]) GetCache(c context.Context, key K, timeout time.Duratio
|
||||||
if !ok {
|
if !ok {
|
||||||
data = mapCacheStruct[V]{}
|
data = mapCacheStruct[V]{}
|
||||||
}
|
}
|
||||||
|
now := time.Duration(time.Now().UnixNano())
|
||||||
var err error
|
var err error
|
||||||
expired := time.Duration(data.setTime.Unix())+m.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(data.setTime.UnixNano())+m.expireTime < now
|
||||||
//todo 这里应该判断下取出的值是否为零值,不过怎么操作呢?
|
//todo 这里应该判断下取出的值是否为零值,不过怎么操作呢?
|
||||||
if !ok || (ok && m.expireTime >= 0 && expired) {
|
if !ok || (ok && m.expireTime >= 0 && expired) {
|
||||||
t := data.incr
|
t := data.incr
|
||||||
|
@ -145,13 +146,14 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
|
||||||
var needFlush []K
|
var needFlush []K
|
||||||
var res []V
|
var res []V
|
||||||
t := 0
|
t := 0
|
||||||
|
now := time.Duration(time.Now().UnixNano())
|
||||||
for _, k := range key {
|
for _, k := range key {
|
||||||
d, ok := m.data[k]
|
d, ok := m.data[k]
|
||||||
if !ok {
|
if !ok {
|
||||||
needFlush = append(needFlush, k)
|
needFlush = append(needFlush, k)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
expired := time.Duration(d.setTime.Unix())+m.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(d.setTime.UnixNano())+m.expireTime < now
|
||||||
if expired {
|
if expired {
|
||||||
needFlush = append(needFlush, k)
|
needFlush = append(needFlush, k)
|
||||||
}
|
}
|
||||||
|
@ -204,3 +206,14 @@ func (m *MapCache[K, V]) GetCacheBatch(c context.Context, key []K, timeout time.
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MapCache[K, V]) ClearExpiredCache() {
|
||||||
|
now := time.Duration(time.Now().UnixNano())
|
||||||
|
m.mutex.Lock()
|
||||||
|
defer m.mutex.Unlock()
|
||||||
|
for k, v := range m.data {
|
||||||
|
if now > time.Duration(v.setTime.UnixNano())+m.expireTime {
|
||||||
|
delete(m.data, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
2
cache/slice.go
vendored
2
cache/slice.go
vendored
|
@ -35,7 +35,7 @@ func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, par
|
||||||
l := len(c.data)
|
l := len(c.data)
|
||||||
data := c.data
|
data := c.data
|
||||||
var err error
|
var err error
|
||||||
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(c.setTime.UnixNano())+c.expireTime < time.Duration(time.Now().UnixNano())
|
||||||
if l < 1 || (l > 0 && c.expireTime >= 0 && expired) {
|
if l < 1 || (l > 0 && c.expireTime >= 0 && expired) {
|
||||||
t := c.incr
|
t := c.incr
|
||||||
call := func() {
|
call := func() {
|
||||||
|
|
|
@ -27,3 +27,11 @@ recentCommentsCacheTime: 5m
|
||||||
digestCacheTime: 5m
|
digestCacheTime: 5m
|
||||||
# 摘要字数
|
# 摘要字数
|
||||||
digestWordCount: 300
|
digestWordCount: 300
|
||||||
|
# 文档列表id页缓存 包括默认列表、分类
|
||||||
|
postListCacheTime: 1h
|
||||||
|
# 搜索文档id缓存时间
|
||||||
|
searchPostCacheTime: 5m
|
||||||
|
# 月归档文章id缓存
|
||||||
|
monthPostCacheTime: 1h
|
||||||
|
# 文档数据缓存时间
|
||||||
|
postDataCacheTime: 1h
|
||||||
|
|
|
@ -18,6 +18,10 @@ type Config struct {
|
||||||
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
|
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
|
||||||
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
|
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
|
||||||
DigestWordCount int `yaml:"digestWordCount"`
|
DigestWordCount int `yaml:"digestWordCount"`
|
||||||
|
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
|
||||||
|
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
|
||||||
|
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
|
||||||
|
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mysql struct {
|
type Mysql struct {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user