Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cghiban/6e58e673eb596177e94c13d1b0e32c20 to your computer and use it in GitHub Desktop.
Save cghiban/6e58e673eb596177e94c13d1b0e32c20 to your computer and use it in GitHub Desktop.

Revisions

  1. @cn0047 cn0047 revised this gist Apr 30, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SuperSimpleRedisCircularBufferWrittenOnGO.md
    Original file line number Diff line number Diff line change
    @@ -51,8 +51,8 @@ func getPool() *radix.Pool {

    func circularBuffer() {
    s := `
    redis.call('RPUSH', KEYS[1], ARGV[1])
    if (redis.call("LLEN", KEYS[1]) > 5) then redis.call("LPOP", KEYS[1]) end
    redis.call('LPUSH', KEYS[1], ARGV[1])
    redis.call('LTRIM', KEYS[1], 0, 5)
    `
    res := ""
    v := strconv.FormatInt(time.Now().Unix(), 10)
  2. @cn0047 cn0047 renamed this gist Apr 17, 2020. 1 changed file with 2 additions and 2 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    Super simple Redis circular-buffer in GO
    Super simple Redis circular-buffer on GO
    -

    Here you can find super simple `redis` based circular-buffer implementation written in `golang`:
    Here you can find super simple `redis` based circular-buffer implementation written on `golang`:

    ````go
    package main
  3. @cn0047 cn0047 created this gist Apr 17, 2020.
    74 changes: 74 additions & 0 deletions SuperSimpleRedisCircularBufferInGO.md
    Original 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!