Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import (
)

type CacheEntry[T any] struct {
Data T `json:"data"`
Date time.Time `json:"date"`
Status int `json:"status"`
ClientID string `json:"clientId,omitempty"`
Data T `json:"data"`
Date time.Time `json:"date"`
InstanceID string `json:"instanceId,omitempty"`
Status int `json:"status"`
TimeElapsed int `json:"timeElapsed,omitempty"`
}

type Cache[T any] interface {
AddRecord(keys []string, data T, status int, taskId string) (date string, expires string)
AddRecord(keys []string, data T, status int, clientID string, startTime time.Time) (date string, expires string)
GetRecord(keys []string) (data T, status int, date string, expires string, found bool)
GetItemCount() int
GetKeys() []string
Expand All @@ -47,11 +50,14 @@ func newRedisCache[T any](client *redis.Client, namespace string) *redisCache[T]
}
}

func (c *redisCache[T]) AddRecord(keys []string, data T, status int, taskId string) (date, expires string) {
func (c *redisCache[T]) AddRecord(keys []string, data T, status int, clientID string, startTime time.Time) (date, expires string) {
entry := CacheEntry[T]{
Data: data,
Date: time.Now(),
Status: status,
ClientID: clientID,
Data: data,
Date: time.Now(),
InstanceID: viper.GetString("instanceid"),
Status: status,
TimeElapsed: int(time.Since(startTime) / time.Millisecond),
}

b, _ := json.Marshal(entry)
Expand Down
14 changes: 7 additions & 7 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestRedisCacheAddAndGetRecord(t *testing.T) {
keys := []string{"key1", "key2"}
status := 200

_, _ = c.AddRecord(keys, data, status, "task123")
_, _ = c.AddRecord(keys, data, status, "client456", time.Now().Add(-time.Second*10))

gotData, gotStatus, _, _, found := c.GetRecord(keys)
if !found {
Expand Down Expand Up @@ -79,8 +79,8 @@ func TestRedisCacheItemCount(t *testing.T) {
t.Fatal("Expected empty cache")
}

c.AddRecord([]string{"a"}, testStruct{"x"}, 200, "task1")
c.AddRecord([]string{"b"}, testStruct{"y"}, 200, "task2")
c.AddRecord([]string{"a"}, testStruct{"x"}, 200, "client1", time.Now().Add(-time.Second*10))
c.AddRecord([]string{"b"}, testStruct{"y"}, 200, "client2", time.Now().Add(-time.Second*10))

if c.GetItemCount() != 2 {
t.Fatalf("Expected 2 items, got %d", c.GetItemCount())
Expand All @@ -90,8 +90,8 @@ func TestRedisCacheItemCount(t *testing.T) {
func TestRedisCacheGetKeys(t *testing.T) {
c := getTestRedisCache[testStruct](t, "test_keys")

c.AddRecord([]string{"k1"}, testStruct{"v1"}, 200, "task1")
c.AddRecord([]string{"k2"}, testStruct{"v2"}, 200, "task2")
c.AddRecord([]string{"k1"}, testStruct{"v1"}, 200, "client1", time.Now().Add(-time.Second*10))
c.AddRecord([]string{"k2"}, testStruct{"v2"}, 200, "client2", time.Now().Add(-time.Second*10))

keys := c.GetKeys()

Expand All @@ -118,8 +118,8 @@ func TestRedisCacheGetKeys(t *testing.T) {
func TestRedisCacheGetValues(t *testing.T) {
c := getTestRedisCache[testStruct](t, "test_values")

c.AddRecord([]string{"a"}, testStruct{"aaa"}, 200, "task1")
c.AddRecord([]string{"b"}, testStruct{"bbb"}, 200, "task2")
c.AddRecord([]string{"a"}, testStruct{"aaa"}, 200, "client1", time.Now().Add(-time.Second*10))
c.AddRecord([]string{"b"}, testStruct{"bbb"}, 200, "client2", time.Now().Add(-time.Second*10))

values := c.GetValues()

Expand Down
2 changes: 1 addition & 1 deletion handlers/getStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var initTime = time.Now()
var version = "1.18.4"
var version = "1.19.0"

func getStatus(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]interface{}{
Expand Down
4 changes: 3 additions & 1 deletion scraper/scrapeAdventurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/gocolly/colly/v2"

Expand Down Expand Up @@ -234,5 +235,6 @@ func scrapeAdventurer(body *colly.HTMLElement, region, profileTarget string) {
profile.CombatFame = utils.CalculateCombatFame(profile.Characters)
}

cache.Profiles.AddRecord([]string{region, profileTarget}, profile, status, body.Request.Ctx.Get("taskId"))
startTime, _ := time.Parse(time.RFC3339, body.Request.Ctx.Get("taskAddedAt"))
cache.Profiles.AddRecord([]string{region, profileTarget}, profile, status, body.Request.Ctx.Get("taskClient"), startTime)
}
4 changes: 3 additions & 1 deletion scraper/scrapeAdventurerSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scraper
import (
"net/http"
"strconv"
"time"

"github.com/gocolly/colly/v2"

Expand Down Expand Up @@ -51,5 +52,6 @@ func scrapeAdventurerSearch(body *colly.HTMLElement, region, query, searchType s
profiles = append(profiles, profile)
})

cache.ProfileSearch.AddRecord([]string{region, query, searchType}, profiles, status, body.Request.Ctx.Get("taskId"))
startTime, _ := time.Parse(time.RFC3339, body.Request.Ctx.Get("taskAddedAt"))
cache.ProfileSearch.AddRecord([]string{region, query, searchType}, profiles, status, body.Request.Ctx.Get("taskClient"), startTime)
}
4 changes: 3 additions & 1 deletion scraper/scrapeGuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"slices"
"strconv"
"strings"
"time"

"github.com/gocolly/colly/v2"

Expand Down Expand Up @@ -74,5 +75,6 @@ func scrapeGuild(body *colly.HTMLElement, region, guildName string) {
}
}

cache.GuildProfiles.AddRecord([]string{region, guildName}, guildProfile, status, body.Request.Ctx.Get("taskId"))
startTime, _ := time.Parse(time.RFC3339, body.Request.Ctx.Get("taskAddedAt"))
cache.GuildProfiles.AddRecord([]string{region, guildName}, guildProfile, status, body.Request.Ctx.Get("taskClient"), startTime)
}
4 changes: 3 additions & 1 deletion scraper/scrapeGuildSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scraper
import (
"net/http"
"strconv"
"time"

"github.com/gocolly/colly/v2"

Expand Down Expand Up @@ -37,5 +38,6 @@ func scrapeGuildSearch(body *colly.HTMLElement, region, query string) {
guildProfiles = append(guildProfiles, guildProfile)
})

cache.GuildSearch.AddRecord([]string{region, query}, guildProfiles, status, body.Request.Ctx.Get("taskId"))
startTime, _ := time.Parse(time.RFC3339, body.Request.Ctx.Get("taskAddedAt"))
cache.GuildSearch.AddRecord([]string{region, query}, guildProfiles, status, body.Request.Ctx.Get("taskClient"), startTime)
}