66 "strings"
77 "time"
88
9- "github.com/eko/gocache/lib/v4/cache"
10- "github.com/eko/gocache/lib/v4/store"
11- "github.com/eko/gocache/store/redis/v4"
129 "github.com/gatewayd-io/gatewayd-plugin-sdk/databases/postgres"
1310 sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
1411 v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
@@ -28,7 +25,6 @@ type Plugin struct {
2825
2926 // Cache configuration.
3027 RedisClient * goRedis.Client
31- RedisStore * redis.RedisStore
3228 RedisURL string
3329 Expiry time.Duration
3430 DefaultDBName string
@@ -76,8 +72,6 @@ func (p *Plugin) GetPluginConfig(
7672func (p * Plugin ) OnTrafficFromClient (
7773 ctx context.Context , req * structpb.Struct ,
7874) (* structpb.Struct , error ) {
79- cacheManager := cache.New [string ](p .RedisStore )
80-
8175 req , err := postgres .HandleClientMessage (req , p .Logger )
8276 if err != nil {
8377 p .Logger .Info ("Failed to handle client message" , "error" , err )
@@ -87,12 +81,12 @@ func (p *Plugin) OnTrafficFromClient(
8781 database := p .DefaultDBName
8882 if database == "" {
8983 client := cast .ToStringMapString (sdkPlugin .GetAttr (req , "client" , nil ))
90- database = p .getDBFromStartupMessage (ctx , req , cacheManager , database , client )
84+ database = p .getDBFromStartupMessage (ctx , req , database , client )
9185
9286 // Get the database from the cache if it's not found in the startup message or
9387 // if the current request is not a startup message.
9488 if database == "" {
95- database , err = cacheManager . Get (ctx , client ["remote" ])
89+ database , err = p . RedisClient . Get (ctx , client ["remote" ]). Result ( )
9690 if err != nil {
9791 CacheMissesCounter .Inc ()
9892 p .Logger .Debug ("Failed to get cache" , "error" , err )
@@ -128,7 +122,7 @@ func (p *Plugin) OnTrafficFromClient(
128122 p .invalidateDML (ctx , query )
129123
130124 // Check if the query is cached.
131- response , err := cacheManager . Get (ctx , cacheKey )
125+ response , err := p . RedisClient . Get (ctx , cacheKey ). Result ( )
132126 if err != nil {
133127 CacheMissesCounter .Inc ()
134128 p .Logger .Debug ("Failed to get cached response" , "error" , err )
@@ -153,8 +147,6 @@ func (p *Plugin) OnTrafficFromClient(
153147func (p * Plugin ) OnTrafficFromServer (
154148 ctx context.Context , resp * structpb.Struct ,
155149) (* structpb.Struct , error ) {
156- cacheManager := cache.New [string ](p .RedisStore )
157-
158150 resp , err := postgres .HandleServerMessage (resp , p .Logger )
159151 if err != nil {
160152 p .Logger .Info ("Failed to handle server message" , "error" , err )
@@ -172,7 +164,7 @@ func (p *Plugin) OnTrafficFromServer(
172164 if database == "" {
173165 client := cast .ToStringMapString (sdkPlugin .GetAttr (resp , "client" , "" ))
174166 if client != nil && client ["remote" ] != "" {
175- database , err = cacheManager . Get (ctx , client ["remote" ])
167+ database , err = p . RedisClient . Get (ctx , client ["remote" ]). Result ( )
176168 if err != nil {
177169 CacheMissesCounter .Inc ()
178170 p .Logger .Debug ("Failed to get cached response" , "error" , err )
@@ -191,16 +183,9 @@ func (p *Plugin) OnTrafficFromServer(
191183 }
192184
193185 cacheKey := strings .Join ([]string {server ["remote" ], database , request }, ":" )
194-
195- options := []store.Option {}
196- if p .Expiry .Seconds () > 0 {
197- p .Logger .Debug ("Key expiry is set" , "expiry" , p .Expiry )
198- options = append (options , store .WithExpiration (p .Expiry ))
199- }
200-
201186 if errorResponse == "" && rowDescription != "" && dataRow != nil && len (dataRow ) > 0 {
202187 // The request was successful and the response contains data. Cache the response.
203- if err := cacheManager . Set (ctx , cacheKey , response , options ... ); err != nil {
188+ if err := p . RedisClient . Set (ctx , cacheKey , response , p . Expiry ). Err ( ); err != nil {
204189 CacheMissesCounter .Inc ()
205190 p .Logger .Debug ("Failed to set cache" , "error" , err )
206191 }
@@ -223,8 +208,8 @@ func (p *Plugin) OnTrafficFromServer(
223208 // the cache when a rows is inserted, updated or deleted into that table.
224209 for _ , table := range tables {
225210 requestQueryCacheKey := strings .Join ([]string {table , cacheKey }, ":" )
226- if err := cacheManager .Set (
227- ctx , requestQueryCacheKey , "" , options ... ); err != nil {
211+ if err := p . RedisClient .Set (
212+ ctx , requestQueryCacheKey , "" , p . Expiry ). Err ( ); err != nil {
228213 CacheMissesCounter .Inc ()
229214 p .Logger .Debug ("Failed to set cache" , "error" , err )
230215 }
@@ -236,10 +221,9 @@ func (p *Plugin) OnTrafficFromServer(
236221}
237222
238223func (p * Plugin ) OnClosed (ctx context.Context , req * structpb.Struct ) (* structpb.Struct , error ) {
239- cacheManager := cache.New [string ](p .RedisStore )
240224 client := cast .ToStringMapString (sdkPlugin .GetAttr (req , "client" , nil ))
241225 if client != nil {
242- if err := cacheManager . Delete (ctx , client ["remote" ]); err != nil {
226+ if err := p . RedisClient . Del (ctx , client ["remote" ]). Err ( ); err != nil {
243227 p .Logger .Debug ("Failed to delete cache" , "error" , err )
244228 CacheMissesCounter .Inc ()
245229 }
@@ -326,7 +310,6 @@ func (p *Plugin) invalidateDML(ctx context.Context, query string) {
326310func (p * Plugin ) getDBFromStartupMessage (
327311 ctx context.Context ,
328312 req * structpb.Struct ,
329- cacheManager * cache.Cache [string ],
330313 database string ,
331314 client map [string ]string ,
332315) string {
@@ -350,8 +333,11 @@ func (p *Plugin) getDBFromStartupMessage(
350333 if startupMsgParams != nil &&
351334 startupMsgParams ["database" ] != "" &&
352335 client ["remote" ] != "" {
353- if err := cacheManager .Set (
354- ctx , client ["remote" ], startupMsgParams ["database" ]); err != nil {
336+ if err := p .RedisClient .Set (
337+ ctx , client ["remote" ],
338+ startupMsgParams ["database" ],
339+ time .Duration (0 ),
340+ ).Err (); err != nil {
355341 CacheMissesCounter .Inc ()
356342 p .Logger .Debug ("Failed to set cache" , "error" , err )
357343 }
0 commit comments