mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
if repo not found in badger cache, check boltdb. if found in boltdb, set in badger and return
This commit is contained in:
@@ -2,10 +2,12 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/dgraph-io/badger"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gojp/goreportcard/check"
|
||||
@@ -33,20 +35,66 @@ func getFromCache(db *badger.DB, repo string) (checksResp, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if item == nil {
|
||||
return notFoundError{repo}
|
||||
if item != nil {
|
||||
log.Printf("repo %q found in badger cache", repo)
|
||||
err = item.Value(func(val []byte) error {
|
||||
err = json.Unmarshal(val, &resp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
err = item.Value(func(val []byte) error {
|
||||
err = json.Unmarshal(val, &resp)
|
||||
if item == nil {
|
||||
log.Printf("Repo %q not found in badger cache. Checking boltdb...", repo)
|
||||
// try getting from boltdb and saving to badger.
|
||||
boltDB, err := bolt.Open("goreportcard.db", 0600, &bolt.Options{Timeout: 3 * time.Second})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
|
||||
return fmt.Errorf("failed to open bolt database during GET: %v", err)
|
||||
}
|
||||
defer boltDB.Close()
|
||||
|
||||
err = boltDB.View(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte("repos"))
|
||||
if b == nil {
|
||||
return errors.New("No repo bucket")
|
||||
}
|
||||
cached := b.Get([]byte(repo))
|
||||
if cached == nil {
|
||||
log.Println("Repo not found in boltdb")
|
||||
return notFoundError{repo}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(cached, &resp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
|
||||
}
|
||||
|
||||
return db.Update(func(txn *badger.Txn) error {
|
||||
log.Printf("Repo %q found in boltdb. Saving to badger cache...", repo)
|
||||
|
||||
// save repo to cache
|
||||
err = txn.Set([]byte(RepoPrefix+repo), cached)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return updateMetadata(txn, resp, repo, false)
|
||||
})
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if resp.Repo == "" {
|
||||
return notFoundError{repo}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user