Skip to content

Commit 02b5dc9

Browse files
committed
pref: language type list support cache
1 parent 84bc41c commit 02b5dc9

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/mattn/go-isatty v0.0.19 // indirect
2727
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2828
github.com/modern-go/reflect2 v1.0.2 // indirect
29+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
2930
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
3031
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3132
github.com/ugorji/go/codec v1.2.11 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
5050
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
5151
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
5252
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
53+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
54+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
5355
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
5456
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
5557
github.com/playwright-community/playwright-go v0.4201.1 h1:fFX/02r3wrL+8NB132RcduR0lWEofxRDJEKuln+9uMQ=
+15-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package handles
22

33
import (
4-
"sync"
54
"time"
65

76
"github.com/lessapidev/lessapi-duckduckgo/internal/searchs"
87
"github.com/lessapidev/lessapi-duckduckgo/internal/types"
98
"github.com/lessapidev/lessapi-duckduckgo/internal/utils"
109

1110
"github.com/gin-gonic/gin"
11+
"github.com/patrickmn/go-cache"
1212
)
1313

14-
type languagesCache struct {
15-
languages []types.LanguageType
16-
ttl time.Duration
17-
lock sync.RWMutex
18-
}
14+
var languagesCache = cache.New(5*time.Minute, 10*time.Minute)
1915

2016
// ListLanguageHandle list all language types
2117
func ListLanguageHandle(c *gin.Context) {
@@ -26,13 +22,26 @@ func ListLanguageHandle(c *gin.Context) {
2622
return
2723
}
2824

25+
// get from cache
26+
languages, found := languagesCache.Get("languages")
27+
if found {
28+
c.JSON(200, utils.BuildApiSuccessData(types.ListLanguageResponse{
29+
Languages: languages.([]types.LanguageType),
30+
}))
31+
return
32+
}
33+
2934
// search text
3035
resp, err := searchs.ListLanguage(payload)
3136
if err != nil {
3237
c.JSON(200, utils.BuildApiError("search_error", err.Error()))
3338
return
3439
}
3540

41+
if len(resp.Languages) > 0 {
42+
languagesCache.Set("languages", resp.Languages, cache.DefaultExpiration)
43+
}
44+
3645
// return response
3746
c.JSON(200, utils.BuildApiSuccessData(resp))
3847
}

0 commit comments

Comments
 (0)