Opitimized writing redis hspatial queries a bit.

This commit is contained in:
Sascha L. Teichmann 2017-02-24 19:49:08 +01:00
parent b0a42867d8
commit 3c2ab551f1

View File

@ -30,16 +30,18 @@ func (client *RedisClient) Close() error {
return client.conn.Close()
}
func (client *RedisClient) writeArray(size int) (err error) {
_, err = client.conn.Write([]byte(fmt.Sprintf("*%d\r\n", size)))
return
}
func redisLength(prefix byte, s int) []byte {
buf := append(make([]byte, 0, 16), prefix)
return append(strconv.AppendInt(buf, int64(s), 10), '\r', '\n')
}
var (
writeArray4 = []byte("*4\r\n")
hspatial = []byte("HSPATIAL")
nl = []byte("\r\n")
ignore = []byte("IGNORE")
)
func (client *RedisClient) writeBulkString(data []byte) (err error) {
if _, err = client.conn.Write(redisLength('$', len(data))); err != nil {
return
@ -47,18 +49,18 @@ func (client *RedisClient) writeBulkString(data []byte) (err error) {
if _, err = client.conn.Write(data); err != nil {
return
}
_, err = client.conn.Write([]byte("\r\n"))
_, err = client.conn.Write(nl)
return
}
func (client *RedisClient) writeHSpatial(p1, p2 int64) (err error) {
if err = client.writeArray(4); err != nil {
if _, err = client.conn.Write(writeArray4); err != nil {
return
}
if err = client.writeBulkString([]byte("HSPATIAL")); err != nil {
if err = client.writeBulkString(hspatial); err != nil {
return
}
if err = client.writeBulkString([]byte("IGNORE")); err != nil {
if err = client.writeBulkString(ignore); err != nil {
return
}
if err = client.writeBulkString(StringToBytes(p1)); err != nil {