This commit is contained in:
awei
2025-03-03 17:43:50 +08:00
commit 113756eaae
1497 changed files with 127997 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package cache
import (
"errors"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
"bbs-go/internal/repositories"
)
type articleTagCache struct {
cache cache.LoadingCache
}
var ArticleTagCache = newArticleTagCache()
func newArticleTagCache() *articleTagCache {
return &articleTagCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
articleTags := repositories.ArticleTagRepository.FindByArticleId(sqls.DB(), key2Int64(key))
if len(articleTags) > 0 {
var tagIds []int64
for _, articleTag := range articleTags {
tagIds = append(tagIds, articleTag.TagId)
}
value = tagIds
} else {
e = errors.New("文章没标签")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
}
}
func (c *articleTagCache) Get(articleId int64) []int64 {
val, err := c.cache.Get(articleId)
if err != nil {
return nil
}
if val != nil {
return val.([]int64)
}
return nil
}
func (c *articleTagCache) Invalidate(articleId int64) {
c.cache.Invalidate(articleId)
}

View File

@@ -0,0 +1,43 @@
package cache
import (
"bbs-go/internal/models"
"bbs-go/internal/repositories"
"log/slog"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
)
type forbiddenWordCache struct {
cache cache.LoadingCache
}
var ForbiddenWordCache = newForbiddenWordCache()
func newForbiddenWordCache() *forbiddenWordCache {
return &forbiddenWordCache{
cache: cache.NewLoadingCache(
func(_ cache.Key) (value cache.Value, e error) {
value = repositories.ForbiddenWordRepository.Find(sqls.DB(), sqls.NewCnd())
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
}
}
func (c *forbiddenWordCache) Get() []models.ForbiddenWord {
val, err := c.cache.Get("_")
if err != nil {
slog.Error(err.Error(), slog.Any("err", err))
return nil
}
return val.([]models.ForbiddenWord)
}
func (c *forbiddenWordCache) Invalidate() {
c.cache.Invalidate("_")
}

View File

@@ -0,0 +1,57 @@
package cache
import (
"errors"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
)
type sysConfigCache struct {
cache cache.LoadingCache
}
var SysConfigCache = newSysConfigCache()
func newSysConfigCache() *sysConfigCache {
return &sysConfigCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.SysConfigRepository.GetByKey(sqls.DB(), key.(string))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
}
}
func (c *sysConfigCache) Get(key string) *models.SysConfig {
val, err := c.cache.Get(key)
if err != nil {
return nil
}
if val != nil {
return val.(*models.SysConfig)
}
return nil
}
func (c *sysConfigCache) GetValue(key string) string {
sysConfig := c.Get(key)
if sysConfig == nil {
return ""
}
return sysConfig.Value
}
func (c *sysConfigCache) Invalidate(key string) {
c.cache.Invalidate(key)
}

View File

@@ -0,0 +1,64 @@
package cache
import (
"errors"
"log/slog"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
)
type tagCache struct {
cache cache.LoadingCache // 标签缓存
}
var TagCache = newTagCache()
func newTagCache() *tagCache {
return &tagCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.TagRepository.Get(sqls.DB(), key2Int64(key))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
}
}
func (c *tagCache) Get(tagId int64) *models.Tag {
val, err := c.cache.Get(tagId)
if err != nil {
slog.Error(err.Error(), slog.Any("err", err))
return nil
}
if val != nil {
return val.(*models.Tag)
}
return nil
}
func (c *tagCache) GetList(tagIds []int64) (tags []models.Tag) {
if len(tagIds) == 0 {
return nil
}
for _, tagId := range tagIds {
tag := c.Get(tagId)
if tag != nil {
tags = append(tags, *tag)
}
}
return
}
func (c *tagCache) Invalidate(tagId int64) {
c.cache.Invalidate(tagId)
}

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)
}

View File

@@ -0,0 +1,94 @@
package cache
import (
"errors"
"time"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/common/dates"
"github.com/mlogclub/simple/sqls"
)
type userCache struct {
cache cache.LoadingCache
scoreRankCache cache.LoadingCache
checkInRankCache cache.LoadingCache
}
var UserCache = newUserCache()
func newUserCache() *userCache {
return &userCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.UserRepository.Get(sqls.DB(), key2Int64(key))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
scoreRankCache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.UserRepository.Find(sqls.DB(), sqls.NewCnd().Desc("score").Limit(10))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(10),
cache.WithRefreshAfterWrite(10*time.Minute),
),
checkInRankCache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
today := dates.GetDay(time.Now())
value = repositories.CheckInRepository.Find(sqls.DB(),
sqls.NewCnd().Eq("latest_day_name", today).Asc("update_time").Limit(10))
return
},
cache.WithMaximumSize(10),
cache.WithExpireAfterAccess(1*time.Hour),
),
}
}
func (c *userCache) Get(userId int64) *models.User {
if userId <= 0 {
return nil
}
val, err := c.cache.Get(userId)
if err != nil {
return nil
}
return val.(*models.User)
}
func (c *userCache) Invalidate(userId int64) {
c.cache.Invalidate(userId)
}
func (c *userCache) GetScoreRank() []models.User {
val, err := c.scoreRankCache.Get("data")
if err != nil {
return nil
}
return val.([]models.User)
}
func (c *userCache) GetCheckInRank() []models.CheckIn {
today := dates.GetDay(time.Now())
val, err := c.checkInRankCache.Get(today)
if err != nil {
return nil
}
return val.([]models.CheckIn)
}
func (c *userCache) RefreshCheckInRank() {
c.checkInRankCache.Refresh(dates.GetDay(time.Now()))
}

View File

@@ -0,0 +1,52 @@
package cache
import (
"errors"
"time"
"github.com/goburrow/cache"
"github.com/mlogclub/simple/sqls"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
)
var UserTokenCache = newUserTokenCache()
type userTokenCache struct {
cache cache.LoadingCache
}
func newUserTokenCache() *userTokenCache {
return &userTokenCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.UserTokenRepository.GetByToken(sqls.DB(), key.(string))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(60*time.Minute),
),
}
}
func (c *userTokenCache) Get(token string) *models.UserToken {
if len(token) == 0 {
return nil
}
val, err := c.cache.Get(token)
if err != nil {
return nil
}
if val != nil {
return val.(*models.UserToken)
}
return nil
}
func (c *userTokenCache) Invalidate(token string) {
c.cache.Invalidate(token)
}

9
bbs-go/server/internal/cache/utils.go vendored Normal file
View File

@@ -0,0 +1,9 @@
package cache
import (
"github.com/goburrow/cache"
)
func key2Int64(key cache.Key) int64 {
return key.(int64)
}