@@ -296,6 +296,12 @@ func StoreBindingPolicy(policy *BindingPolicyCache) error {
296
296
return fmt .Errorf ("cannot store nil binding policy" )
297
297
}
298
298
299
+ // Check if Redis is available
300
+ if err := rdb .Ping (ctx ).Err (); err != nil {
301
+ log .LogWarn ("redis not available, skipping cache store" , zap .Error (err ))
302
+ return nil // Don't fail the operation if Redis is down
303
+ }
304
+
299
305
// Marshal the policy to JSON
300
306
jsonData , err := json .Marshal (policy )
301
307
if err != nil {
@@ -319,6 +325,11 @@ func StoreBindingPolicy(policy *BindingPolicyCache) error {
319
325
320
326
// GetBindingPolicy retrieves a binding policy from Redis by name
321
327
func GetBindingPolicy (name string ) (* BindingPolicyCache , error ) {
328
+ // Check if Redis is available
329
+ if err := rdb .Ping (ctx ).Err (); err != nil {
330
+ return nil , fmt .Errorf ("redis not available: %v" , err )
331
+ }
332
+
322
333
val , err := rdb .HGet (ctx , BindingPolicyHashKey , name ).Result ()
323
334
if err == redis .Nil {
324
335
return nil , nil // Policy not found
@@ -336,11 +347,20 @@ func GetBindingPolicy(name string) (*BindingPolicyCache, error) {
336
347
337
348
// GetAllBindingPolicies retrieves all binding policies from Redis
338
349
func GetAllBindingPolicies () ([]* BindingPolicyCache , error ) {
350
+ // Check if Redis is available
351
+ if err := rdb .Ping (ctx ).Err (); err != nil {
352
+ return nil , fmt .Errorf ("redis not available: %v" , err )
353
+ }
354
+
339
355
values , err := rdb .HGetAll (ctx , BindingPolicyHashKey ).Result ()
340
356
if err != nil {
341
357
return nil , fmt .Errorf ("failed to get all binding policies from Redis: %v" , err )
342
358
}
343
359
360
+ if len (values ) == 0 {
361
+ return nil , nil
362
+ }
363
+
344
364
policies := make ([]* BindingPolicyCache , 0 , len (values ))
345
365
for _ , val := range values {
346
366
var policy BindingPolicyCache
@@ -356,6 +376,12 @@ func GetAllBindingPolicies() ([]*BindingPolicyCache, error) {
356
376
357
377
// DeleteBindingPolicy removes a binding policy from Redis
358
378
func DeleteBindingPolicy (name string ) error {
379
+ // Check if Redis is available
380
+ if err := rdb .Ping (ctx ).Err (); err != nil {
381
+ log .LogWarn ("redis not available, skipping cache delete" , zap .Error (err ))
382
+ return nil // Don't fail the operation if Redis is down
383
+ }
384
+
359
385
err := rdb .HDel (ctx , BindingPolicyHashKey , name ).Err ()
360
386
if err != nil {
361
387
return fmt .Errorf ("failed to delete binding policy from Redis: %v" , err )
@@ -365,6 +391,12 @@ func DeleteBindingPolicy(name string) error {
365
391
366
392
// DeleteAllBindingPolicies removes all binding policies from Redis
367
393
func DeleteAllBindingPolicies () error {
394
+ // Check if Redis is available
395
+ if err := rdb .Ping (ctx ).Err (); err != nil {
396
+ log .LogWarn ("redis not available, skipping cache delete" , zap .Error (err ))
397
+ return nil // Don't fail the operation if Redis is down
398
+ }
399
+
368
400
err := rdb .Del (ctx , BindingPolicyHashKey ).Err ()
369
401
if err != nil {
370
402
return fmt .Errorf ("failed to delete all binding policies from Redis: %v" , err )
0 commit comments