This commit is contained in:
12600k-rog-d4
2025-12-09 20:33:57 +08:00
parent 144ed3cbdf
commit 8f4d399cfd
1498 changed files with 3778 additions and 3654 deletions

View File

@@ -0,0 +1,55 @@
package cache
import (
"bbs-go/internal/models/constants"
"errors"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
)
var (
topicRecommendCacheKey = "recommend_topics_cache"
)
var TopicCache = newTopicCache()
type topicCache struct {
recommendCache cache.LoadingCache
}
func newTopicCache() *topicCache {
return &topicCache{
recommendCache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.TopicRepository.Find(sqls.DB(),
sqls.NewCnd().Eq("status", constants.StatusOk).Desc("id").Limit(50))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(10),
cache.WithRefreshAfterWrite(30*time.Minute),
),
}
}
func (c *topicCache) GetRecommendTopics() []models.Topic {
val, err := c.recommendCache.Get(topicRecommendCacheKey)
if err != nil {
return nil
}
if val != nil {
return val.([]models.Topic)
}
return nil
}
func (c *topicCache) InvalidateRecommend() {
c.recommendCache.Invalidate(topicRecommendCacheKey)
}