Skip to content

Instantly share code, notes, and snippets.

@paderinandrey
Created December 27, 2021 00:55
Show Gist options
  • Save paderinandrey/b21249cd3e35c6361398a43c1aff9cdf to your computer and use it in GitHub Desktop.
Save paderinandrey/b21249cd3e35c6361398a43c1aff9cdf to your computer and use it in GitHub Desktop.
RDS-06
package main
import (
"context"
"fmt"
"os"
"strconv"
"github.com/go-redis/redis/v8"
)
func main() {
var ctx = context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
key := os.Args[1]
limit, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println(err)
}
for i := 0; i < limit; i++ {
val := rdb.LPop(ctx, key).Val()
if val != "" {
fmt.Println(val)
}
}
}
package main
import (
"bufio"
"context"
"fmt"
"os"
"github.com/go-redis/redis/v8"
)
func main() {
var ctx = context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
key := os.Args[1]
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if rdb.LLen(ctx, key).Val() == 20 {
rdb.LPop(ctx, key)
}
err := rdb.RPush(ctx, key, scanner.Text()).Err()
if err != nil {
fmt.Println(err)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment