Best Venom code snippet using redis.New
redis.go
Source:redis.go
...18var Redis *RedisClient19// ConnectRedis è¿æ¥ redis æ°æ®åºï¼è®¾ç½®å
¨å±ç Redis 对象20func ConnectRedis(address string, username string, password string, db int) {21 once.Do(func() {22 Redis = NewClient(address, username, password, db)23 })24}25// NewClient å建ä¸ä¸ªæ°ç redis è¿æ¥26func NewClient(address string, username string, password string, db int) *RedisClient {27 // åå§åèªå®ç RedisClient å®ä¾28 rds := &RedisClient{}29 // 使ç¨é»è®¤ç context30 rds.Context = context.Background()31 // ä½¿ç¨ redis åºéç NewClient åå§åè¿æ¥32 rds.Client = redis.NewClient(&redis.Options{33 Addr: address,34 Username: username,35 Password: password,36 DB: db,37 })38 // æµè¯ä»¥ä¸è¿æ¥39 err := rds.Ping()40 logger.LogIf(err)41 return rds42}43// Ping ç¨ä»¥æµè¯ redis è¿æ¥æ¯å¦æ£å¸¸44func (rds RedisClient) Ping() error {45 _, err := rds.Client.Ping(rds.Context).Result()46 return err...
rate_limiting_test.go
Source:rate_limiting_test.go
...5 "github.com/go-redis/redis/v7"6 "github.com/stretchr/testify/assert"7)8const redisURL = "redis://localhost:6379/0"9var testInstance = prefixer.NewPrefixer("cozy.example.net", "cozy-example-net")10func TestLoginRateNotExceededMem(t *testing.T) {11 globalCounter = NewMemCounter()12 assert.NoError(t, CheckRateLimit(testInstance, AuthType))13}14func TestLoginRateExceededMem(t *testing.T) {15 globalCounter = NewMemCounter()16 for i := 1; i <= 1000; i++ {17 assert.NoError(t, CheckRateLimit(testInstance, AuthType))18 }19 err := CheckRateLimit(testInstance, AuthType)20 assert.Error(t, err)21}22func TestLoginRateNotExceededRedis(t *testing.T) {23 opts, _ := redis.ParseURL(redisURL)24 client := redis.NewClient(opts)25 client.Del("auth:" + testInstance.DomainName())26 globalCounter = NewRedisCounter(client)27 assert.NoError(t, CheckRateLimit(testInstance, AuthType))28}29func TestLoginRateExceededRedis(t *testing.T) {30 opts, _ := redis.ParseURL(redisURL)31 client := redis.NewClient(opts)32 globalCounter = NewRedisCounter(client)33 client.Del("auth:" + testInstance.DomainName())34 for i := 1; i <= 1000; i++ {35 assert.NoError(t, CheckRateLimit(testInstance, AuthType))36 }37 assert.Error(t, CheckRateLimit(testInstance, AuthType))38}39func Test2FAGenerationNotExceededMem(t *testing.T) {40 globalCounter = NewMemCounter()41 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorGenerationType))42}43func Test2FAGenerationExceededMem(t *testing.T) {44 globalCounter = NewMemCounter()45 for i := 1; i <= 20; i++ {46 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorGenerationType))47 }48 err := CheckRateLimit(testInstance, TwoFactorGenerationType)49 assert.Error(t, err)50}51func Test2FAGenerationNotExceededRedis(t *testing.T) {52 opts, _ := redis.ParseURL(redisURL)53 client := redis.NewClient(opts)54 client.Del("two-factor-generation:" + testInstance.DomainName())55 globalCounter = NewRedisCounter(client)56 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorGenerationType))57}58func Test2FAGenerationExceededRedis(t *testing.T) {59 opts, _ := redis.ParseURL(redisURL)60 client := redis.NewClient(opts)61 globalCounter = NewRedisCounter(client)62 client.Del("two-factor-generation:" + testInstance.DomainName())63 for i := 1; i <= 20; i++ {64 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorGenerationType))65 }66 assert.Error(t, CheckRateLimit(testInstance, TwoFactorGenerationType))67}68func Test2FARateExceededNotExceededMem(t *testing.T) {69 globalCounter = NewMemCounter()70 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorType))71}72func Test2FARateExceededMem(t *testing.T) {73 globalCounter = NewMemCounter()74 for i := 1; i <= 10; i++ {75 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorType))76 }77 err := CheckRateLimit(testInstance, TwoFactorType)78 assert.Error(t, err)79}80func Test2FANotExceededRedis(t *testing.T) {81 opts, _ := redis.ParseURL(redisURL)82 client := redis.NewClient(opts)83 client.Del("two-factor:" + testInstance.DomainName())84 globalCounter = NewRedisCounter(client)85 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorType))86}87func Test2FAExceededRedis(t *testing.T) {88 opts, _ := redis.ParseURL(redisURL)89 client := redis.NewClient(opts)90 globalCounter = NewRedisCounter(client)91 client.Del("two-factor:" + testInstance.DomainName())92 for i := 1; i <= 10; i++ {93 assert.NoError(t, CheckRateLimit(testInstance, TwoFactorType))94 }95 assert.Error(t, CheckRateLimit(testInstance, TwoFactorType))96}...
dao.go
Source:dao.go
...11)12// Dao dao struct .13type Dao struct {14 maxNum int15 expNewArc int16 conf *conf.Config17 redis *redis.Pool18 redisRank *redis.Pool19 redisTag *redis.Pool20 memcache *memcache.Pool21 platform *sql.DB22 // grpc23 filClient filgrpc.FilterClient24}25// New init Dao .26func New(c *conf.Config) (d *Dao) {27 d = &Dao{28 conf: c,29 platform: sql.NewMySQL(c.Platform.MySQL),30 maxNum: c.Tag.MaxArcsLimit,31 redis: redis.NewPool(c.Redis.Rank.Redis),32 redisRank: redis.NewPool(c.RedisRank),33 redisTag: redis.NewPool(c.RedisTag),34 memcache: memcache.NewPool(c.Memcache),35 expNewArc: int(time.Duration(c.Redis.Rank.Expire.TagNewArc) / time.Second),36 }37 var err error38 if d.filClient, err = filgrpc.NewClient(c.FilterGRPC); err != nil {39 panic(err)40 }41 return42}43// PingRedis ping redis .44func (d *Dao) pingRedis(c context.Context) (err error) {45 conn := d.redis.Get(c)46 _, err = conn.Do("SET", "PING", "PONG")47 conn.Close()48 return49}50// pingRedisRank ping rank redis .51func (d *Dao) pingRedisRank(c context.Context) (err error) {52 conn := d.redisRank.Get(c)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!