Skip to content

Commit c1277d5

Browse files
committed
change(cache): use badgerdb
1 parent 0d1d8aa commit c1277d5

File tree

17 files changed

+161
-158
lines changed

17 files changed

+161
-158
lines changed

cache/cache.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
package cache
22

33
import (
4-
jsoniter "github.com/json-iterator/go"
54
"github.com/l3uddz/crop/logger"
65
"github.com/l3uddz/crop/stringutils"
7-
"sync"
8-
"time"
6+
"github.com/pkg/errors"
7+
"github.com/zippoxer/bow"
8+
"github.com/zippoxer/bow/codec/json"
99
)
1010

1111
var (
1212
log = logger.GetLogger("cache")
1313
cacheFilePath string
14-
mtx sync.Mutex
15-
vault map[string]time.Time
1614

1715
// Internal
18-
json = jsoniter.ConfigCompatibleWithStandardLibrary
16+
db *bow.DB
1917
)
2018

2119
/* Public */
2220

23-
func Init(cachePath string) error {
21+
func Init(cachePath string, logLevel int) error {
2422
// set globals
2523
cacheFilePath = cachePath
26-
vault = make(map[string]time.Time)
2724

28-
// load cache from disk
29-
if err := loadFromFile(cachePath); err != nil {
30-
return err
25+
// set badger options
26+
opts := []bow.Option{
27+
// codec
28+
bow.SetCodec(json.Codec{}),
3129
}
3230

31+
if logLevel < 2 {
32+
// disable badger logging for non trace log level
33+
opts = append(opts, bow.SetLogger(nil))
34+
}
35+
36+
// init database
37+
v, err := bow.Open(cachePath, opts...)
38+
if err != nil {
39+
return errors.WithMessage(err, "failed opening cache")
40+
}
41+
42+
db = v
3343
return nil
3444
}
3545

46+
func Close() {
47+
if err := db.Close(); err != nil {
48+
log.WithError(err).Error("Failed closing cache gracefully...")
49+
}
50+
}
51+
3652
func ShowUsing() {
3753
log.Infof("Using %s = %q", stringutils.LeftJust("CACHE", " ", 10), cacheFilePath)
3854
}

cache/dump.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

cache/get.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

cache/load.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

cache/sa.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cache
2+
3+
import (
4+
"github.com/zippoxer/bow"
5+
"time"
6+
)
7+
8+
type Banned struct {
9+
Path string `bow:"key"`
10+
Expires time.Time
11+
}
12+
13+
func IsBanned(key string) (bool, time.Time) {
14+
// check if key was found in banned bucket
15+
var item Banned
16+
err := db.Bucket("banned").Get(key, &item)
17+
18+
// was key not found
19+
if err == bow.ErrNotFound {
20+
// this key is not banned
21+
return false, time.Time{}
22+
} else if err != nil {
23+
log.WithError(err).Errorf("Failed checking banned bucket for: %q", key)
24+
return false, time.Time{}
25+
}
26+
27+
// check if the ban has expired
28+
if item.Expires.Before(time.Now().UTC()) {
29+
// the ban has expired, remove
30+
log.Warnf("Expired %q: %v", key, item.Expires)
31+
32+
err := db.Bucket("banned").Delete(key)
33+
if err != nil {
34+
log.WithError(err).Errorf("Failed removing from banned bucket: %q", key)
35+
return false, time.Time{}
36+
}
37+
38+
return false, time.Time{}
39+
}
40+
41+
// this key is still banned
42+
return true, item.Expires
43+
44+
}
45+
46+
func SetBanned(key string, hours int) error {
47+
expiry := time.Now().UTC().Add(time.Duration(hours) * time.Hour)
48+
49+
return db.Bucket("banned").Put(Banned{
50+
Path: key,
51+
Expires: expiry,
52+
})
53+
}

cache/set.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
flagLogLevel = 0
2121
flagConfigFolder = pathutils.GetCurrentBinaryPath()
2222
flagConfigFile = "config.yaml"
23-
flagCacheFile = "cache.json"
23+
flagCachePath = "cache"
2424
flagLogFile = "activity.log"
2525

2626
flagDryRun bool
@@ -50,7 +50,7 @@ func init() {
5050
// Parse persistent flags
5151
rootCmd.PersistentFlags().StringVar(&flagConfigFolder, "config-dir", flagConfigFolder, "Config folder")
5252
rootCmd.PersistentFlags().StringVarP(&flagConfigFile, "config", "c", flagConfigFile, "Config file")
53-
rootCmd.PersistentFlags().StringVarP(&flagCacheFile, "cache", "d", flagCacheFile, "Cache file")
53+
rootCmd.PersistentFlags().StringVarP(&flagCachePath, "cache", "d", flagCachePath, "Cache path")
5454
rootCmd.PersistentFlags().StringVarP(&flagLogFile, "log", "l", flagLogFile, "Log file")
5555
rootCmd.PersistentFlags().CountVarP(&flagLogLevel, "verbose", "v", "Verbose level")
5656

@@ -63,7 +63,7 @@ func initCore(showAppInfo bool) {
6363
flagConfigFile = filepath.Join(flagConfigFolder, flagConfigFile)
6464
}
6565
if !rootCmd.PersistentFlags().Changed("cache") {
66-
flagCacheFile = filepath.Join(flagConfigFolder, flagCacheFile)
66+
flagCachePath = filepath.Join(flagConfigFolder, flagCachePath)
6767
}
6868
if !rootCmd.PersistentFlags().Changed("log") {
6969
flagLogFile = filepath.Join(flagConfigFolder, flagLogFile)
@@ -84,7 +84,7 @@ func initCore(showAppInfo bool) {
8484
setConfigOverrides()
8585

8686
// Init Cache
87-
if err := cache.Init(flagCacheFile); err != nil {
87+
if err := cache.Init(flagCachePath, flagLogLevel); err != nil {
8888
log.WithError(err).Fatal("Failed to initialize cache")
8989
}
9090

cmd/sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"github.com/dustin/go-humanize"
5+
"github.com/l3uddz/crop/cache"
56
"github.com/l3uddz/crop/config"
67
"github.com/l3uddz/crop/rclone"
78
"github.com/l3uddz/crop/syncer"
@@ -23,6 +24,7 @@ var syncCmd = &cobra.Command{
2324
Run: func(cmd *cobra.Command, args []string) {
2425
// init core
2526
initCore(true)
27+
defer cache.Close()
2628

2729
// iterate syncer's
2830
for syncerName, syncerConfig := range config.Config.Syncer {

cmd/update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"bufio"
55
"github.com/blang/semver"
6+
"github.com/l3uddz/crop/cache"
67
"github.com/l3uddz/crop/runtime"
78
"github.com/rhysd/go-github-selfupdate/selfupdate"
89
"github.com/spf13/cobra"
@@ -17,6 +18,7 @@ var updateCmd = &cobra.Command{
1718
Run: func(cmd *cobra.Command, args []string) {
1819
// init core
1920
initCore(false)
21+
defer cache.Close()
2022

2123
// parse current version
2224
v, err := semver.Parse(runtime.Version)

cmd/upload.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"github.com/dustin/go-humanize"
5+
"github.com/l3uddz/crop/cache"
56
"github.com/l3uddz/crop/config"
67
"github.com/l3uddz/crop/rclone"
78
"github.com/l3uddz/crop/uploader"
@@ -23,6 +24,7 @@ var uploadCmd = &cobra.Command{
2324
Run: func(cmd *cobra.Command, args []string) {
2425
// init core
2526
initCore(true)
27+
defer cache.Close()
2628

2729
// iterate uploader's
2830
for uploaderName, uploaderConfig := range config.Config.Uploader {

0 commit comments

Comments
 (0)