Skip to content

Commit

Permalink
caching support to buntdb
Browse files Browse the repository at this point in the history
Signed-off-by: Avelino <avelinorun@gmail.com>
  • Loading branch information
avelino committed Dec 23, 2021
1 parent 2b17719 commit 7e2afd7
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cache/buntdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cache

import (
"net/http"
"time"

"github.com/avelino/slugify"
"github.com/prest/prest/config"
"github.com/tidwall/buntdb"
)

// BuntConnect connects to database BuntDB - used for caching
func BuntConnect(key string) (db *buntdb.DB, err error) {
if key != "" {
// each url will have its own cache,
// this will avoid slowing down the cache base
// it is saved in a file on the file system
key = slugify.Slugify(key)
}
db, err = buntdb.Open(config.PrestConf.CacheStoragePath + key + config.PrestConf.CacheSufixFile)
if err != nil {
// in case of an error to open buntdb the prestd cache is forced to false
config.PrestConf.Cache = false
}
return
}

// BuntGet downloads the data - if any - that is in the buntdb (embeded cache database)
// using response.URL as key
func BuntGet(key string, w http.ResponseWriter) (cacheExist bool) {
db, _ := BuntConnect(key)
cacheExist = false
db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get(key)
if err == nil {
cacheExist = true
w.WriteHeader(http.StatusOK)
w.Write([]byte(val))
}
return nil
})
defer db.Close()
return
}

// BuntSet sets data as cache in buntdb (embeded cache database)
// using response.URL as key
func BuntSet(key, value string) {
if !config.PrestConf.Cache {
return
}
db, _ := BuntConnect(key)
db.Update(func(tx *buntdb.Tx) error {
tx.Set(key, value,
&buntdb.SetOptions{
Expires: true,
TTL: time.Duration(config.PrestConf.CacheTime) * time.Minute})
return nil
})
defer db.Close()
}

0 comments on commit 7e2afd7

Please sign in to comment.