Skip to content

Commit

Permalink
func check cache by endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Avelino <avelinorun@gmail.com>
  • Loading branch information
avelino committed Dec 24, 2021
1 parent 695eee3 commit cc975f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cache

import (
"log"

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

// CacheEndpointRules ...
func CacheEndpointRules(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
time = endpoint.CacheTime
return
}
}
return
}
30 changes: 30 additions & 0 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cache

import (
"os"
"testing"

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

func TestCacheEndpointRulesEnable(t *testing.T) {
os.Setenv("PREST_CONF", "./testdata/prest.toml")
os.Setenv("PREST_CACHE", "true")
config.Load()
cacheEnable, cacheTime := CacheEndpointRules("/prest/public/test")
if !cacheEnable {
t.Errorf("expected cache endpoint rule true, but got %t", cacheEnable)
}
if cacheTime != 5 {
t.Errorf("expected cache endpoint time 5, but got %d", cacheTime)
}
}

func TestCacheEndpointRulesDisable(t *testing.T) {
os.Setenv("PREST_CACHE", "true")
config.Load()
cacheEnable, _ := CacheEndpointRules("/prest/public/test-disable")
if cacheEnable {
t.Errorf("expected cache endpoint rule false, but got %t", cacheEnable)
}
}

0 comments on commit cc975f7

Please sign in to comment.