-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(db/redis): cache CPE#Titles #186
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a3f4c6c
to
ebb59c7
Compare
ebb59c7
to
d667c8c
Compare
If you want to enable zstd :100644 100644 c8dd734 0000000 M db/redis.go
:100644 100644 74191c5 0000000 M go.mod
:100644 100644 f7c7b29 0000000 M go.sum
diff --git a/db/redis.go b/db/redis.go
index c8dd734..b549fc9 100644
--- a/db/redis.go
+++ b/db/redis.go
@@ -1,6 +1,7 @@
package db
import (
+ "bytes"
"context"
"encoding/json"
"errors"
@@ -12,6 +13,7 @@ import (
"github.com/cheggaaa/pb/v3"
"github.com/go-redis/redis/v8"
"github.com/hbollon/go-edlib"
+ "github.com/klauspost/compress/zstd"
"github.com/spf13/viper"
"golang.org/x/xerrors"
@@ -256,7 +258,13 @@ func (r *RedisDriver) GetSimilarCpesByTitle(query string, n int, algorithm edlib
var ts []string
bs, err := r.conn.Get(ctx, titleListCacheKey).Bytes()
if err == nil {
- if err := json.Unmarshal(bs, &ts); err != nil {
+ d, err := zstd.NewReader(bytes.NewReader(bs))
+ if err != nil {
+ return nil, xerrors.Errorf("Failed to new zstd reader. err: %w", err)
+ }
+ defer d.Close()
+
+ if err := json.NewDecoder(d).Decode(&ts); err != nil {
return nil, xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
}
} else {
@@ -402,7 +410,13 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
if err != nil {
return xerrors.Errorf("Failed to Marshal JSON. err: %w", err)
}
- if err := r.conn.Set(ctx, titleListCacheKey, string(bs), 0).Err(); err != nil {
+ e, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
+ if err != nil {
+ return xerrors.Errorf("Failed to new zstd writer. err: %w", err)
+ }
+ defer e.Close()
+
+ if err := r.conn.Set(ctx, titleListCacheKey, e.EncodeAll(bs, make([]byte, 0, len(bs))), 0).Err(); err != nil {
return xerrors.Errorf("Failed to SET Titles Cache. err: %w", err)
}
bar.Increment()
diff --git a/go.mod b/go.mod
index 74191c5..70378ee 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/hbollon/go-edlib v1.6.0
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible
+ github.com/klauspost/compress v1.17.9
github.com/knqyf263/go-cpe v0.0.0-20201213041631-54f6ab28673f
github.com/labstack/echo/v4 v4.12.0
github.com/mitchellh/go-homedir v1.1.0
diff --git a/go.sum b/go.sum
index f7c7b29..f973494 100644
--- a/go.sum
+++ b/go.sum
@@ -66,6 +66,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
+github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
+github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/knqyf263/go-cpe v0.0.0-20201213041631-54f6ab28673f h1:vZP1dTKPOR7zSAbgqNbnTnYX77+gj3eu0QK+UmANZqE=
github.com/knqyf263/go-cpe v0.0.0-20201213041631-54f6ab28673f/go.mod h1:4cVhzV/TndScEg4xMtSo3TTz3cMFhEAvhAA4igAyXZY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= |
Some measurement on the latency of GET CPE#Cache#Tiles: ~20msec by slowlog
|
shino
approved these changes
Jun 25, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely Fantastic!!!
9 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If this Pull Request is work in progress, Add a prefix of “[WIP]” in the title.
What did you implement:
The current implementation retrieves 1508985 elements each time, every time.
It is known that the performance of SMEMBERS deteriorates when the set size becomes large.
In the case of this key, since the elements are determined at the time of fetching, performance can be improved by caching or other methods.
go-cpe-dictionary/db/redis.go
Line 247 in 19a510c
Type of change
How Has This Been Tested?
before
after
Checklist:
You don't have to satisfy all of the following.
make fmt
make test
Is this ready for review?: YES
Reference