2023-01-12 12:42:16 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-19 13:02:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
2023-01-12 12:42:16 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RecentComments(ctx context.Context, n int) (r []models.Comments) {
|
2023-02-09 07:43:20 +00:00
|
|
|
nn := n
|
|
|
|
if nn <= 5 {
|
|
|
|
nn = 10
|
|
|
|
}
|
|
|
|
r, err := recentCommentsCaches.GetCache(ctx, time.Second, ctx, nn)
|
2023-01-12 12:42:16 +00:00
|
|
|
if len(r) > n {
|
|
|
|
r = r[0:n]
|
|
|
|
}
|
|
|
|
logs.ErrPrintln(err, "get recent comment")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostComments(ctx context.Context, Id uint64) ([]models.Comments, error) {
|
|
|
|
ids, err := postCommentCaches.GetCache(ctx, Id, time.Second, ctx, Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return GetCommentByIds(ctx, ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommentById(ctx context.Context, id uint64) (models.Comments, error) {
|
|
|
|
return commentsCache.GetCache(ctx, id, time.Second, ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCommentByIds(ctx context.Context, ids []uint64) ([]models.Comments, error) {
|
|
|
|
return commentsCache.GetCacheBatch(ctx, ids, time.Second, ctx, ids)
|
|
|
|
}
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
func NewCommentCache() *cache.MapCache[string, string] {
|
|
|
|
return newCommentCache
|
|
|
|
}
|