Caching (closes #18)

This commit is contained in:
video-prize-ranch 2022-03-15 22:56:34 -04:00
parent db2195d17f
commit 90ab3b37d3
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
6 changed files with 76 additions and 8 deletions

View File

@ -4,15 +4,22 @@ import (
"io"
"net/http"
"strings"
"time"
"codeberg.org/video-prize-ranch/rimgo/types"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
var albumCache = cache.New(1*time.Hour, 15*time.Minute)
func FetchAlbum(albumID string) (types.Album, error) {
// https://api.imgur.com/post/v1/albums/zk7mdKH?client_id=${CLIENT_ID}&include=media%2Caccount
cacheData, found := albumCache.Get(albumID + "-album")
if found {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&include=media%2Caccount")
if err != nil {
@ -33,10 +40,16 @@ func FetchAlbum(albumID string) (types.Album, error) {
album, err = ParseAlbum(data)
}
albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
return album, err
}
func FetchPosts(albumID string) (types.Album, error) {
cacheData, found := albumCache.Get(albumID + "-posts")
if found {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&include=media%2Caccount%2Ctags")
if err != nil {
return types.Album{}, err
@ -48,11 +61,21 @@ func FetchPosts(albumID string) (types.Album, error) {
}
data := gjson.Parse(string(body))
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
}
return ParseAlbum(data)
albumCache.Set(albumID + "-posts", album, cache.DefaultExpiration)
return album, nil
}
func FetchMedia(mediaID string) (types.Album, error) {
cacheData, found := albumCache.Get(mediaID + "-media")
if found {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&include=media%2Caccount")
if err != nil {
return types.Album{}, err
@ -64,8 +87,13 @@ func FetchMedia(mediaID string) (types.Album, error) {
}
data := gjson.Parse(string(body))
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
}
return ParseAlbum(data)
albumCache.Set(mediaID + "-media", album, cache.DefaultExpiration)
return album, nil
}
func ParseAlbum(data gjson.Result) (types.Album, error) {

View File

@ -10,12 +10,18 @@ import (
"codeberg.org/video-prize-ranch/rimgo/types"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/dustin/go-humanize"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
var commentCache = cache.New(15*time.Minute, 15*time.Minute)
func FetchComments(galleryID string) ([]types.Comment, error) {
// https://api.imgur.com/comment/v1/comments?client_id=546c25a59c58ad7&filter[post]=eq:g1bk7CB&include=account&per_page=30&sort=best
cacheData, found := commentCache.Get(galleryID)
if found {
return cacheData.([]types.Comment), nil
}
res, err := http.Get("https://api.imgur.com/comment/v1/comments?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best")
if err != nil {
@ -45,6 +51,7 @@ func FetchComments(galleryID string) ([]types.Comment, error) {
)
wg.Wait()
commentCache.Set(galleryID, comments, cache.DefaultExpiration)
return comments, nil
}

View File

@ -5,13 +5,22 @@ import (
"net/http"
"strings"
"sync"
"time"
"codeberg.org/video-prize-ranch/rimgo/types"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
var tagCache = cache.New(15*time.Minute, 15*time.Minute)
func FetchTag(tag string, sort string, page string) (types.Tag, error) {
cacheData, found := tagCache.Get(tag + sort + page)
if found {
return cacheData.(types.Tag), nil
}
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts/t/"+tag, nil)
if err != nil {
return types.Tag{}, err
@ -82,12 +91,15 @@ func FetchTag(tag string, sort string, page string) (types.Tag, error) {
wg.Wait()
return types.Tag{
tagData := types.Tag{
Tag: tag,
Display: data.Get("display").String(),
Sort: sort,
PostCount: data.Get("post_count").Int(),
Posts: posts,
Background: "/" + data.Get("background_id").String() + ".webp",
}, nil
}
tagCache.Set(tag, tagData, cache.DefaultExpiration)
return tagData, nil
}

View File

@ -8,11 +8,19 @@ import (
"time"
"codeberg.org/video-prize-ranch/rimgo/types"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
var userCache = cache.New(30*time.Minute, 15*time.Minute)
func FetchUser(username string) (types.User, error) {
cacheData, found := userCache.Get(username)
if found {
return cacheData.(types.User), nil
}
res, err := http.Get("https://api.imgur.com/account/v1/accounts/" + username + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID"))
if err != nil {
return types.User{}, err
@ -27,7 +35,7 @@ func FetchUser(username string) (types.User, error) {
createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String())
return types.User{
user := types.User{
Id: data.Get("id").Int(),
Bio: data.Get("bio").String(),
Username: data.Get("username").String(),
@ -35,10 +43,18 @@ func FetchUser(username string) (types.User, error) {
Cover: strings.ReplaceAll(data.Get("cover_url").String(), "https://imgur.com", ""),
Avatar: strings.ReplaceAll(data.Get("avatar_url").String(), "https://i.imgur.com", ""),
CreatedAt: createdTime.Format("January 2, 2006"),
}, nil
}
userCache.Set(username, user, 1*time.Hour)
return user, nil
}
func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) {
cacheData, found := userCache.Get(username + "-submissions")
if found {
return cacheData.([]types.Submission), nil
}
res, err := http.Get("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID"))
if err != nil {
return []types.Submission{}, err
@ -99,5 +115,7 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis
},
)
wg.Wait()
userCache.Set(username + "-submissions", submissions, 15*time.Minute)
return submissions, nil
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/gofiber/fiber/v2 v2.24.0
github.com/gofiber/template v1.6.21
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/spf13/viper v1.10.1
github.com/tidwall/gjson v1.12.1
)

2
go.sum
View File

@ -313,6 +313,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=