Last active
September 14, 2020 15:12
-
-
Save saiumesh535/c03b0b47e9b45c28be756f202deb7aa5 to your computer and use it in GitHub Desktop.
Go-redis
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 ( | |
"bytes" | |
"fmt" | |
"log" | |
"net" | |
"strconv" | |
"sync" | |
) | |
type RedisPool struct { | |
lock sync.Mutex | |
connPool []net.Conn | |
} | |
func (pool *RedisPool) SetPool(size int) { | |
var wg sync.WaitGroup | |
connChan := make(chan net.Conn) | |
for i := 0; i < size; i++ { | |
wg.Add(1) | |
go func(c chan net.Conn) { | |
conn, err := net.Dial("tcp", "localhost:6379") | |
if err != nil { | |
log.Fatal(err) | |
} | |
c <- conn | |
}(connChan) | |
} | |
for { | |
wg.Done() | |
pool.connPool = append(pool.connPool, <- connChan) | |
if len(pool.connPool) == 10 { | |
break | |
} | |
} | |
wg.Wait() | |
} | |
func (pool *RedisPool) GetConn() net.Conn { | |
for { | |
if len(pool.connPool) > 0 { | |
pool.lock.Lock() | |
conn := pool.connPool[0] | |
pool.connPool = pool.connPool[1:] | |
pool.lock.Unlock() | |
return conn | |
} | |
} | |
} | |
func (pool *RedisPool) SetConn(con net.Conn) { | |
pool.lock.Lock() | |
defer pool.lock.Unlock() | |
pool.connPool = append(pool.connPool, con) | |
} | |
func (pool *RedisPool) Set(key, value string) ([]byte, error) { | |
return pool.Command("SET", key, value) | |
} | |
func ConstructCommand(command ...string) bytes.Buffer { | |
var stringCommand bytes.Buffer | |
stringCommand.WriteString("*"+ strconv.Itoa(len(command))+"\r\n") | |
for _, v := range command { | |
stringCommand.WriteString("$"+strconv.Itoa(len(v))+"\r\n"+v+"\r\n") | |
} | |
return stringCommand | |
} | |
func (pool *RedisPool) Command(command ...string) ([]byte, error) { | |
stringCommand := ConstructCommand(command...) | |
conn := pool.GetConn() | |
_, err := conn.Write(stringCommand.Bytes()) | |
if err != nil { | |
return nil, err | |
} | |
resp := make([]byte, 1024) | |
_, err = conn.Read(resp) | |
if err != nil { | |
return nil, err | |
} | |
return resp, nil | |
} | |
func (pool *RedisPool) Publish(channel, data string) { | |
_, err := pool.Command("PUBLISH",channel, data) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
} | |
func (pool *RedisPool) SubScribe(channel string) { | |
con := pool.GetConn() | |
command := ConstructCommand("SUBSCRIBE", channel) | |
_, err := con.Write(command.Bytes()) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
for { | |
resp := make([]byte, 1024) | |
_, err := con.Read(resp) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
fmt.Println("HAHAHA", string(resp)) | |
} | |
} | |
// SET KEY VALUE | |
func main() { | |
pool := &RedisPool{} | |
pool.SetPool(10) | |
//_, err := pool.Set(os.Args[1], os.Args[2]) | |
//if err != nil { | |
// log.Fatal(err.Error()) | |
//} | |
//_, err = pool.Command("SET", os.Args[1], os.Args[2]) | |
//if err != nil { | |
// log.Fatal(err.Error()) | |
//} | |
// | |
//resp, err := pool.Command("GET", os.Args[1]) | |
//if err != nil { | |
// log.Fatal(err.Error()) | |
//} | |
pool.SubScribe("CORS") | |
//fmt.Println(string(resp)) | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work!
https://gist.github.com/saiumesh535/c03b0b47e9b45c28be756f202deb7aa5#file-main-go-L54 - 🦾