Skip to content

Commit 1580029

Browse files
committed
Caching full historical data read from DB
1 parent 8c49240 commit 1580029

File tree

15 files changed

+1697
-385
lines changed

15 files changed

+1697
-385
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ require (
88
github.com/go-pkgz/rest v1.17.0
99
github.com/mattn/go-sqlite3 v1.14.16
1010
github.com/parMaster/htu21 v0.0.0-20230220190438-31e4538dc67a
11-
github.com/stretchr/testify v1.8.2
11+
github.com/parMaster/mcache v0.0.0-20230710130850-062e48b74ec9
12+
github.com/stretchr/testify v1.8.4
1213
github.com/umputun/go-flags v1.5.1
1314
gopkg.in/yaml.v3 v3.0.1
1415
periph.io/x/conn/v3 v3.7.0

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwp
1212
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
1313
github.com/parMaster/htu21 v0.0.0-20230220190438-31e4538dc67a h1:TOAqYxV6fBjuppOP/YNZsit/PkBXvK26EsnHWYKeTCA=
1414
github.com/parMaster/htu21 v0.0.0-20230220190438-31e4538dc67a/go.mod h1:j6Sc5brwkWz9kSTjsoak9gM8cyVWHkA5mz5tbWVlOC0=
15+
github.com/parMaster/mcache v0.0.0-20230710130850-062e48b74ec9 h1:eQUJfIaewKFD8GgZP+Wn5UdyIWbamqpJC7B8W+K0Tbs=
16+
github.com/parMaster/mcache v0.0.0-20230710130850-062e48b74ec9/go.mod h1:IHmVlGB2VV4WCcWgImPynd7sqmSLc6wI+YgCYCun5TA=
1517
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1618
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1719
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
1820
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8=
1921
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
21-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2222
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
25-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
26-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
23+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
24+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
2725
github.com/umputun/go-flags v1.5.1 h1:vRauoXV3Ultt1HrxivSxowbintgZLJE+EcBy5ta3/mY=
2826
github.com/umputun/go-flags v1.5.1/go.mod h1:nTbvsO/hKqe7Utri/NoyN18GR3+EWf+9RrmsdwdhrEc=
2927
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/go-chi/chi/v5"
1818
"github.com/go-pkgz/lgr"
1919
"github.com/go-pkgz/rest"
20+
"github.com/parMaster/mcache"
2021
"github.com/parMaster/rpid/config"
2122
"github.com/parMaster/rpid/storage"
2223
"github.com/parMaster/rpid/storage/model"
@@ -47,6 +48,7 @@ type Worker struct {
4748
modules Modules
4849
mx sync.Mutex
4950
store storage.Storer
51+
cache mcache.Cacher
5052
ctx context.Context
5153
}
5254

@@ -60,6 +62,7 @@ func NewWorker(config *config.Parameters) *Worker {
6062
w := &Worker{
6163
config: *config,
6264
data: data,
65+
cache: mcache.NewCache(),
6366
}
6467

6568
return w
@@ -301,11 +304,16 @@ func (w *Worker) router() http.Handler {
301304
}
302305
rw.Header().Set("Content-Type", "application/json")
303306
rw.Header().Set("Access-Control-Allow-Origin", "*")
304-
out, err := w.store.View(w.ctx, module)
307+
308+
out, err := w.cache.Get(module)
305309
if err != nil {
306-
log.Printf("[ERROR] Failed to get view: %v", err)
307-
rw.WriteHeader(http.StatusInternalServerError)
308-
return
310+
out, err = w.store.View(w.ctx, module)
311+
if err != nil {
312+
log.Printf("[ERROR] Failed to get view: %v", err)
313+
rw.WriteHeader(http.StatusInternalServerError)
314+
return
315+
}
316+
w.cache.Set(module, out, 60)
309317
}
310318
json.NewEncoder(rw).Encode(out)
311319
})

vendor/github.com/parMaster/mcache/.gitignore

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)