Skip to content

Commit 04c52fc

Browse files
authored
feat: cache masterfile on update (#16)
* feat: cache masterfile on update * chore: update deps, workflow * fix: clear cache on manual masterfile load
1 parent ac1a811 commit 04c52fc

File tree

6 files changed

+1601
-2135
lines changed

6 files changed

+1601
-2135
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Check out code into the Go module directory
15-
uses: actions/checkout@v3
15+
uses: actions/checkout@v4
1616

1717
- name: Setup Go
18-
uses: actions/setup-go@v3
18+
uses: actions/setup-go@v5
1919
with:
20-
go-version: ^1.19
20+
go-version: '1.23.x'
2121

2222
- name: Get golangci-lint
23-
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.52.2
23+
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.63.3
2424

2525
- name: Run golangci-lint
2626
run: golangci-lint run

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/UnownHash/gohbem
22

3-
go 1.19
3+
go 1.23
4+
5+
toolchain go1.23.2

ohbem.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gohbem
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"math"
67
"os"
78
"reflect"
@@ -14,7 +15,7 @@ import (
1415
const MaxLevel = 100
1516

1617
// VERSION of gohbem, follows Semantic Versioning. (http://semver.org/)
17-
const VERSION = "0.10.0"
18+
const VERSION = "0.11.0"
1819

1920
// FetchPokemonData Fetch remote MasterFile and keep it in memory.
2021
func (o *Ohbem) FetchPokemonData() error {
@@ -38,6 +39,7 @@ func (o *Ohbem) LoadPokemonData(filePath string) error {
3839
return ErrMasterFileUnmarshall
3940
}
4041
o.PokemonData.Initialized = true
42+
o.ClearCache()
4143
return nil
4244
}
4345

@@ -93,6 +95,14 @@ func (o *Ohbem) WatchPokemonData() error {
9395
o.PokemonData = pokemonData // overwrite PokemonData using new MasterFile
9496
o.PokemonData.Initialized = true
9597
o.ClearCache() // clean compactRankCache cache
98+
// when provided store latest version of MasterFile under provided path
99+
if o.MasterFileCachePath != "" {
100+
err = o.SavePokemonData(o.MasterFileCachePath)
101+
if err != nil {
102+
o.log(fmt.Sprintf("Storing MasterFile cache under %s has failed!", o.MasterFileCachePath))
103+
continue
104+
}
105+
}
96106
}
97107
}
98108
}

ohbem_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,20 @@ func TestIsMegaUnreleased(t *testing.T) {
359359
output bool
360360
}{
361361
{0, 0, false},
362-
{127, 1, true},
362+
{127, 1, false},
363363
{127, 0, false},
364364
{150, 0, false},
365365
{150, 1, false},
366366
{150, 2, true},
367+
{666, 2, false},
367368
}
368369

369370
for ix, test := range tests {
370371
testName := fmt.Sprintf("%d", ix)
371372
t.Run(testName, func(t *testing.T) {
372373
output, _ := ohbem.IsMegaUnreleased(test.pokemonId, test.form)
373374
if output != test.output {
374-
t.Errorf("got %t, want %t", output, test.output)
375+
t.Errorf("%d:%d: got %t, want %t", test.pokemonId, test.form, output, test.output)
375376
}
376377
})
377378
}

structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Ohbem struct {
1111
LevelCaps []int
1212
Leagues map[string]League
1313
DisableCache bool
14+
MasterFileCachePath string // when provided: store there latest changed version of masterfile
1415
RankingComparator RankingComparator
1516
IncludeHundosUnderCap bool
1617
WatcherInterval time.Duration

0 commit comments

Comments
 (0)