Skip to content

Commit

Permalink
feat(searching): added caching to search functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nokusukun committed Mar 16, 2020
1 parent cc1e378 commit 8c674c8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
36 changes: 36 additions & 0 deletions anime/anime_test.go
Expand Up @@ -44,6 +44,42 @@ func TestSearch(t *testing.T) {
})
assert.Nil(t, err)
assert.Len(t, result.Results, 1, result)
mia := result.Results[0]

fmt.Println("Title:", result.Results[0].Title)
fmt.Println("Request Cached?", result.RequestCached)

result, err = Search(
Query{Q: "Full Metal Alchemist",
Limit: 1,
Genre: GenreAdventure,
})
assert.Nil(t, err)
assert.Len(t, result.Results, 1, result)

fmt.Println("Title:", result.Results[0].Title)
fmt.Println("Request Cached?", result.RequestCached)

result, err = Search(
Query{Q: "Naruto",
Limit: 1,
Genre: GenreAdventure,
})
assert.Nil(t, err)
assert.Len(t, result.Results, 1, result)

fmt.Println("Title:", result.Results[0].Title)
fmt.Println("Request Cached?", result.RequestCached)

result, err = Search(
Query{Q: "made in abyss",
Limit: 1,
Genre: GenreAdventure,
})
assert.Nil(t, err)
assert.Len(t, result.Results, 1, result)

assert.Equal(t, result.Results[0].MalID, mia.MalID)

fmt.Println("Title:", result.Results[0].Title)
fmt.Println("Request Cached?", result.RequestCached)
Expand Down
2 changes: 1 addition & 1 deletion anime/search.go
Expand Up @@ -15,7 +15,7 @@ func Search(q Query) (SearchResult, error) {
q.Page = 1
}

request, err := req.Get(utils.Config.AppendAPIf("/search/anime"), q.ToParam())
request, err := utils.CachedReqGet(utils.Config.AppendAPIf("/search/anime"), q.ToParam())
if err != nil {
return SearchResult{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion manga/search.go
Expand Up @@ -14,7 +14,7 @@ func Search(q Query) (SearchResult, error) {
q.Page = 1
}

request, err := req.Get(utils.Config.AppendAPIf("/search/manga"), q.ToParam())
request, err := utils.CachedReqGet(utils.Config.AppendAPIf("/search/manga"), q.ToParam())
if err != nil {
return SearchResult{}, err
}
Expand Down
6 changes: 6 additions & 0 deletions user/user_test.go
Expand Up @@ -20,6 +20,12 @@ func TestGetAnimeList(t *testing.T) {
naux, err := GetUser(User{Username: "naux"})
assert.Nil(t, err)

_, err = GetUser(User{Username: "nekomata1037"})
assert.Nil(t, err)

_, err = GetUser(User{Username: "naux"})
assert.Nil(t, err)

nauxlist, err := GetAnimeList(naux, AllList)
assert.Nil(t, err)

Expand Down
20 changes: 19 additions & 1 deletion utils/cached_requests.go
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/imroc/req"
)

var lastUncachedRequest = time.Now()

type CachedRequest struct {
Data []byte
RequestedOn time.Time
Expand Down Expand Up @@ -60,7 +62,14 @@ func readCache(reqhash string) (*CachedRequest, error) {
}

func CachedReqGet(url string, v ...interface{}) (*CachedRequest, error) {
requestHash := hash(strings.ReplaceAll(url, Config.API, ""))
ttl := time.Now().Sub(lastUncachedRequest)
if ttl < time.Millisecond*500 {
sleepTime := (time.Millisecond * 500) - ttl
log.Println("Sleeping for", sleepTime)
time.Sleep(sleepTime)
}

requestHash := hash(strings.ReplaceAll(url, Config.API, "") + fmt.Sprintf("%v", v))

cr := &CachedRequest{}
if !cacheExists(requestHash) {
Expand Down Expand Up @@ -89,6 +98,11 @@ func CachedReqGet(url string, v ...interface{}) (*CachedRequest, error) {
cr.RequestedOn = time.Now()
cr.ETag = data.Response().Header.Get("ETag")

if strings.Contains(string(cr.Data), "\"request_cached\": false") {
log.Println("uncached request, setting timeout for next call")
lastUncachedRequest = time.Now()
}

err = writeCache(requestHash, cr)
if err != nil {
return nil, fmt.Errorf("[%v] Failed to write cache: %v\n", requestHash, err)
Expand Down Expand Up @@ -125,6 +139,10 @@ func CachedReqGet(url string, v ...interface{}) (*CachedRequest, error) {
cache.ETag = data.Response().Header.Get("ETag")
}
cache.RequestedOn = time.Now()
if strings.Contains(string(cr.Data), "\"request_cached\": false") {
log.Println("uncached request, setting timeout for next call")
lastUncachedRequest = time.Now()
}

// Write the cache
err = writeCache(requestHash, cache)
Expand Down

0 comments on commit 8c674c8

Please sign in to comment.