Created
September 15, 2021 06:07
-
-
Save datoga/cb31c8ee1ae540dcd64cb956dfecc9a1 to your computer and use it in GitHub Desktop.
redis cache bug - main failed once demo with dropped connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/go-redis/cache/v8" | |
"github.com/go-redis/redis/v8" | |
) | |
const key = "key" | |
const newKey = "new-key" | |
const value = "value" | |
func main() { | |
client := redis.NewClient(&redis.Options{}) | |
redisCache := cache.New(&cache.Options{ | |
Redis: client, | |
}) | |
err := client.Ping(context.Background()).Err() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("PING OK") | |
err = redisCache.Once(&cache.Item{ | |
Key: key, | |
Do: func(item *cache.Item) (interface{}, error) { | |
return value, nil | |
}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("ONCE OK") | |
fmt.Println("---------") | |
fmt.Println("DROPS CONNECTION") | |
fmt.Println("---------") | |
err = client.Close() | |
if err != nil { | |
panic("Failed closing the connection") | |
} | |
err = client.Ping(context.Background()).Err() | |
if err == nil { | |
panic("expecting ping error") | |
} | |
fmt.Printf("PING ERROR (OK): %v\n", err) | |
err = redisCache.Set(&cache.Item{ | |
Key: newKey, | |
Value: value, | |
}) | |
if err == nil { | |
panic(err) | |
} | |
fmt.Printf("SET ERROR (OK): %v\n", err) | |
err = redisCache.Once(&cache.Item{ | |
Key: newKey, | |
Do: func(item *cache.Item) (interface{}, error) { | |
return value, nil | |
}, | |
}) | |
if err == nil { | |
panic("expecting once error, got nil") | |
} | |
fmt.Printf("ONCE ERROR (OK): %v\n", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment