wp-go/internal/plugins/digest.go

41 lines
1.0 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package plugins
import (
2023-02-02 11:16:18 +00:00
"context"
2023-01-12 12:42:16 +00:00
"fmt"
"github.com/fthvgb1/wp-go/cache"
"github.com/fthvgb1/wp-go/internal/cmd/cachemanager"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/plugin/digest"
2023-01-12 12:42:16 +00:00
"strings"
"time"
)
var digestCache *cache.MapCache[uint64, string]
func InitDigestCache() {
digestCache = cachemanager.MapCacheBy[uint64](digestRaw, config.GetConfig().CacheTime.DigestCacheTime)
2023-01-12 12:42:16 +00:00
}
func digestRaw(arg ...any) (string, error) {
str := arg[0].(string)
id := arg[1].(uint64)
limit := arg[2].(int)
2023-01-12 12:42:16 +00:00
if limit < 0 {
return str, nil
} else if limit == 0 {
return "", nil
}
return digest.Raw(str, limit, fmt.Sprintf("/p/%d", id)), nil
}
func Digest(ctx context.Context, post *models.Posts, limit int) {
content, _ := digestCache.GetCache(ctx, post.Id, time.Second, post.PostContent, post.Id, limit)
post.PostContent = content
2023-01-12 12:42:16 +00:00
}
func PostExcerpt(post *models.Posts) {
post.PostContent = strings.Replace(post.PostExcerpt, "\n", "<br/>", -1)
2023-01-12 12:42:16 +00:00
}