Forked from cn0047/SuperSimpleRedisCircularBufferWrittenOnGO.md
Created
March 8, 2024 19:54
-
-
Save cghiban/6e58e673eb596177e94c13d1b0e32c20 to your computer and use it in GitHub Desktop.
Revisions
-
cn0047 revised this gist
Apr 30, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -51,8 +51,8 @@ func getPool() *radix.Pool { func circularBuffer() { s := ` redis.call('LPUSH', KEYS[1], ARGV[1]) redis.call('LTRIM', KEYS[1], 0, 5) ` res := "" v := strconv.FormatInt(time.Now().Unix(), 10) -
cn0047 renamed this gist
Apr 17, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ Super simple Redis circular-buffer on GO - Here you can find super simple `redis` based circular-buffer implementation written on `golang`: ````go package main -
cn0047 created this gist
Apr 17, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ Super simple Redis circular-buffer in GO - Here you can find super simple `redis` based circular-buffer implementation written in `golang`: ````go package main import ( "fmt" "log" "strconv" "time" radix "github.com/mediocregopher/radix/v3" ) const ( RedisAddr = "localhost:6379" RedisPoolSize = 3 ) func main() { circularBuffer() } func getPool() *radix.Pool { f := radix.PoolConnFunc(func(network, addr string) (radix.Conn, error) { client, err := radix.Dial(network, addr) if err != nil { return nil, err } if err = client.Do(radix.Cmd(nil, "SELECT", "1")); err != nil { if e := client.Close(); e != nil { return nil, e } return nil, err } return client, nil }) i := radix.PoolPingInterval(1 * time.Second) p, err := radix.NewPool("tcp", RedisAddr, RedisPoolSize, f, i) if err != nil { panic(fmt.Errorf("failed to create redis pool , error: %w", err)) } return p } func circularBuffer() { s := ` redis.call('RPUSH', KEYS[1], ARGV[1]) if (redis.call("LLEN", KEYS[1]) > 5) then redis.call("LPOP", KEYS[1]) end ` res := "" v := strconv.FormatInt(time.Now().Unix(), 10) script := radix.NewEvalScript(1, s).Cmd(&res, "cb", v) p := getPool() err := p.Do(script) if err != nil { panic(fmt.Errorf("failed to run redis script , error: %w", err)) } log.Printf("got result: %#v", res) } ```` #### PS You can find more stuff like this in [my demo repo](https://github.com/cn007b/my). <br>Source code, examples, explanation info, etc. just go and check it out!