| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package link
- import (
- "time"
- "github.com/gin-gonic/gin"
- "github.com/runningwater/gohub/pkg/app"
- "github.com/runningwater/gohub/pkg/cache"
- "github.com/runningwater/gohub/pkg/database"
- "github.com/runningwater/gohub/pkg/helpers"
- "github.com/runningwater/gohub/pkg/paginator"
- )
- func Get(idStr string) (link Link) {
- database.DB.Where("id", idStr).First(&link)
- return
- }
- func GetBy(field, value string) (link Link) {
- database.DB.Where("? = ?", field, value).First(&link)
- return
- }
- func All() (links []Link) {
- database.DB.Find(&links)
- return
- }
- func AllCached() (links []Link) {
- cacheKey := "links:aa"
- expireTime := 120 * time.Minute
- // 获取数据
- cache.GetObject(cacheKey, &links)
- if helpers.Empty(links) {
- links = All()
- if helpers.Empty(links) {
- return links
- }
- cache.Set(cacheKey, links, expireTime)
- }
- return
- }
- func IsExist(field, value string) bool {
- var count int64
- database.DB.Model(Link{}).Where("? = ?", field, value).Count(&count)
- return count > 0
- }
- // Paginate 分页内容
- func Paginate(c *gin.Context, pageSize int) (links []Link, paging paginator.Paging) {
- paging = paginator.Paginate(
- c,
- database.DB.Model(Link{}),
- &links,
- app.V1URL(database.TableName(&Link{})),
- pageSize,
- )
- return
- }
|