Skip to content

Commit

Permalink
breaking-change(redis): deprecated expire option (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n authored Oct 3, 2021
1 parent 9a33239 commit b2ebf6b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 40 deletions.
3 changes: 0 additions & 3 deletions commands/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ func init() {

fetchCmd.PersistentFlags().Int("batch-size", 100, "The number of batch size to insert.")
_ = viper.BindPFlag("batch-size", fetchCmd.PersistentFlags().Lookup("batch-size"))

fetchCmd.PersistentFlags().Uint("expire", 0, "timeout to set for Redis keys in seconds. If set to 0, the key is persistent.")
_ = viper.BindPFlag("expire", fetchCmd.PersistentFlags().Lookup("expire"))
}
38 changes: 1 addition & 37 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/cheggaaa/pb/v3"
"github.com/go-redis/redis/v8"
Expand Down Expand Up @@ -186,7 +185,6 @@ func (r *RedisDriver) GetCpesByVendorProduct(vendor, product string) ([]string,
// InsertCpes Select Cve information from DB.
func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes []models.CategorizedCpe) (err error) {
ctx := context.Background()
expire := viper.GetUint("expire")
batchSize := viper.GetInt("batch-size")
if batchSize < 1 {
return xerrors.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
Expand Down Expand Up @@ -220,28 +218,12 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes []models.Categ
for _, c := range cpes[idx.From:idx.To] {
bar.Increment()
vendorProductStr := fmt.Sprintf("%s#%s", c.Vendor, c.Product)
vpKey := fmt.Sprintf(vpKeyFormat, c.Vendor, c.Product)
if err := pipe.SAdd(ctx, vpListKey, vendorProductStr).Err(); err != nil {
return xerrors.Errorf("Failed to SAdd vendorProduct. err: %w", err)
}
if err := pipe.SAdd(ctx, vpKey, c.CpeURI).Err(); err != nil {
if err := pipe.SAdd(ctx, fmt.Sprintf(vpKeyFormat, c.Vendor, c.Product), c.CpeURI).Err(); err != nil {
return xerrors.Errorf("Failed to SAdd CpeURI. err: %w", err)
}
if expire > 0 {
if err := pipe.Expire(ctx, vpListKey, time.Duration(expire*uint(time.Second))).Err(); err != nil {
return xerrors.Errorf("Failed to set Expire to Key. err: %w", err)
}
if err := pipe.Expire(ctx, vpKey, time.Duration(expire*uint(time.Second))).Err(); err != nil {
return xerrors.Errorf("Failed to set Expire to Key. err: %w", err)
}
} else {
if err := pipe.Persist(ctx, vpListKey).Err(); err != nil {
return xerrors.Errorf("Failed to remove the existing timeout on Key. err: %w", err)
}
if err := pipe.Persist(ctx, vpKey).Err(); err != nil {
return xerrors.Errorf("Failed to remove the existing timeout on Key. err: %w", err)
}
}
if _, ok := newDeps["VP"][vendorProductStr]; !ok {
newDeps["VP"][vendorProductStr] = map[string]struct{}{}
}
Expand All @@ -259,15 +241,6 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes []models.Categ
if err := pipe.SAdd(ctx, deprecatedCPEsKey, c.CpeURI).Err(); err != nil {
return xerrors.Errorf("Failed to set to deprecated CPE. err: %w", err)
}
if expire > 0 {
if err := pipe.Expire(ctx, deprecatedCPEsKey, time.Duration(expire*uint(time.Second))).Err(); err != nil {
return xerrors.Errorf("Failed to set Expire to Key. err: %w", err)
}
} else {
if err := pipe.Persist(ctx, deprecatedCPEsKey).Err(); err != nil {
return xerrors.Errorf("Failed to remove the existing timeout on Key. err: %w", err)
}
}
newDeps["DeprecatedCPEs"][c.CpeURI] = map[string]struct{}{}
delete(oldDeps["DeprecatedCPEs"], c.CpeURI)
}
Expand Down Expand Up @@ -306,15 +279,6 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes []models.Categ
if err := pipe.HSet(ctx, depKey, string(fetchType), string(newDepsJSON)).Err(); err != nil {
return xerrors.Errorf("Failed to Set depkey. err: %w", err)
}
if expire > 0 {
if err := pipe.Expire(ctx, depKey, time.Duration(expire*uint(time.Second))).Err(); err != nil {
return xerrors.Errorf("Failed to set Expire to Key. err: %w", err)
}
} else {
if err := pipe.Persist(ctx, depKey).Err(); err != nil {
return xerrors.Errorf("Failed to remove the existing timeout on Key. err: %w", err)
}
}
if _, err = pipe.Exec(ctx); err != nil {
return xerrors.Errorf("Failed to exec pipeline. err: %w", err)
}
Expand Down

0 comments on commit b2ebf6b

Please sign in to comment.