File tree 3 files changed +18
-6
lines changed
3 files changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ require (
26
26
github.com/mattn/go-isatty v0.0.19 // indirect
27
27
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
28
28
github.com/modern-go/reflect2 v1.0.2 // indirect
29
+ github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
29
30
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
30
31
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
31
32
github.com/ugorji/go/codec v1.2.11 // indirect
Original file line number Diff line number Diff line change @@ -50,6 +50,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
50
50
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd /go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q =
51
51
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M =
52
52
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 =
53
55
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ =
54
56
github.com/pelletier/go-toml/v2 v2.0.8 /go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4 =
55
57
github.com/playwright-community/playwright-go v0.4201.1 h1:fFX/02r3wrL+8NB132RcduR0lWEofxRDJEKuln+9uMQ =
Original file line number Diff line number Diff line change 1
1
package handles
2
2
3
3
import (
4
- "sync"
5
4
"time"
6
5
7
6
"github.com/lessapidev/lessapi-duckduckgo/internal/searchs"
8
7
"github.com/lessapidev/lessapi-duckduckgo/internal/types"
9
8
"github.com/lessapidev/lessapi-duckduckgo/internal/utils"
10
9
11
10
"github.com/gin-gonic/gin"
11
+ "github.com/patrickmn/go-cache"
12
12
)
13
13
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 )
19
15
20
16
// ListLanguageHandle list all language types
21
17
func ListLanguageHandle (c * gin.Context ) {
@@ -26,13 +22,26 @@ func ListLanguageHandle(c *gin.Context) {
26
22
return
27
23
}
28
24
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
+
29
34
// search text
30
35
resp , err := searchs .ListLanguage (payload )
31
36
if err != nil {
32
37
c .JSON (200 , utils .BuildApiError ("search_error" , err .Error ()))
33
38
return
34
39
}
35
40
41
+ if len (resp .Languages ) > 0 {
42
+ languagesCache .Set ("languages" , resp .Languages , cache .DefaultExpiration )
43
+ }
44
+
36
45
// return response
37
46
c .JSON (200 , utils .BuildApiSuccessData (resp ))
38
47
}
You can’t perform that action at this time.
0 commit comments