Skip to content

Commit

Permalink
consider calling this EndpointRules
Browse files Browse the repository at this point in the history
ref: #652 (review)
thanks @sourcelevel-bot @sourcelevel

Signed-off-by: Avelino <avelinorun@gmail.com>
  • Loading branch information
avelino committed Dec 24, 2021
1 parent 35d1344 commit f5cfb29
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cache/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func BuntGet(key string, w http.ResponseWriter) (cacheExist bool) {
// using response.URL.String() as key
func BuntSet(key, value string) {
uri := strings.Split(key, "?")
cacheRule, cacheTime := CacheEndpointRules(uri[0])
cacheRule, cacheTime := EndpointRules(uri[0])
if !config.PrestConf.Cache || !cacheRule {
return
}
Expand Down
7 changes: 2 additions & 5 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package cache

import (
"log"

"github.com/prest/prest/config"
)

// CacheEndpointRules ...
func CacheEndpointRules(uri string) (cacheEnable bool, time int) {
// EndpointRules checks if there is a custom caching rule for the endpoint
func EndpointRules(uri string) (cacheEnable bool, time int) {
cacheEnable = false
if config.PrestConf.Cache && len(config.PrestConf.CacheEndpoints) == 0 {
cacheEnable = true
}
time = config.PrestConf.CacheTime
log.Println("cache len:", cacheEnable, config.PrestConf.CacheEndpoints, len(config.PrestConf.CacheEndpoints))
for _, endpoint := range config.PrestConf.CacheEndpoints {
if endpoint.Endpoint == uri {
cacheEnable = true
Expand Down
8 changes: 4 additions & 4 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/prest/prest/config"
)

func TestCacheEndpointRulesEnable(t *testing.T) {
func TestEndpointRulesEnable(t *testing.T) {
os.Setenv("PREST_CONF", "./testdata/prest.toml")
os.Setenv("PREST_CACHE", "true")
config.Load()
cacheEnable, cacheTime := CacheEndpointRules("/prest/public/test")
cacheEnable, cacheTime := EndpointRules("/prest/public/test")
if !cacheEnable {
t.Errorf("expected cache endpoint rule true, but got %t", cacheEnable)
}
Expand All @@ -20,10 +20,10 @@ func TestCacheEndpointRulesEnable(t *testing.T) {
}
}

func TestCacheEndpointRulesDisable(t *testing.T) {
func TestEndpointRulesDisable(t *testing.T) {
os.Setenv("PREST_CACHE", "true")
config.Load()
cacheEnable, _ := CacheEndpointRules("/prest/public/test-disable")
cacheEnable, _ := EndpointRules("/prest/public/test-disable")
if cacheEnable {
t.Errorf("expected cache endpoint rule false, but got %t", cacheEnable)
}
Expand Down
2 changes: 1 addition & 1 deletion middlewares/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CacheMiddleware() negroni.Handler {
return
}
// team will not be used when downloading information, second result ignored
cacheRule, _ := cache.CacheEndpointRules(r.URL.Path)
cacheRule, _ := cache.EndpointRules(r.URL.Path)
if config.PrestConf.Cache && r.Method == "GET" && !match && cacheRule {
if cache.BuntGet(r.URL.String(), w) {
return
Expand Down

0 comments on commit f5cfb29

Please sign in to comment.