From 90ab3b37d3a3cf9efd001d3e14cc24e6c105bc48 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Tue, 15 Mar 2022 22:56:34 -0400 Subject: [PATCH] Caching (closes #18) --- api/album.go | 34 +++++++++++++++++++++++++++++++--- api/comments.go | 9 ++++++++- api/tag.go | 16 ++++++++++++++-- api/user.go | 22 ++++++++++++++++++++-- go.mod | 1 + go.sum | 2 ++ 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/api/album.go b/api/album.go index 31c93a6..1333acc 100644 --- a/api/album.go +++ b/api/album.go @@ -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) { diff --git a/api/comments.go b/api/comments.go index d85ad5a..d087556 100644 --- a/api/comments.go +++ b/api/comments.go @@ -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 } diff --git a/api/tag.go b/api/tag.go index 7e6a8bf..a4fabf9 100644 --- a/api/tag.go +++ b/api/tag.go @@ -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 } diff --git a/api/user.go b/api/user.go index 0b68d39..29c785a 100644 --- a/api/user.go +++ b/api/user.go @@ -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 } diff --git a/go.mod b/go.mod index 44e7590..c0f08cd 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 1e5d262..cf7003d 100644 --- a/go.sum +++ b/go.sum @@ -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=