Skip to content

Commit

Permalink
Add commands: Multi,Discard,Exec
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed May 5, 2024
1 parent a5f6c30 commit c6500ef
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Simple way to embed in your application.
| QUIT | SAVE | EXPIRE | INCR | SCARD | HDEL | LPOP | ZRANK |
| ECHO | INFO | EXPIREAT | DECR | SPOP | HLEN | RPOP | ZREVRANK |
| DBSIZE | | KEYS | SETBIT | SDIFF | HKEYS | LLEN | ZSCORE |
| | | TTL | GETBIT | SINTER | HEXISTS | LINDEX | ZINCRBY |
| | | RENAME | INCR | SISMEMBER | HGETALL | LINSERT | ZRANGE |
| | | TYPE | DESR | SMEMBERS | HINCRBY | LPUSHX | ZREVRANGE |
| MULTI | | TTL | GETBIT | SINTER | HEXISTS | LINDEX | ZINCRBY |
| DISCARD | | RENAME | INCR | SISMEMBER | HGETALL | LINSERT | ZRANGE |
| EXEC | | TYPE | DESR | SMEMBERS | HINCRBY | LPUSHX | ZREVRANGE |
| | | SCAN | SETEX | SREM | HICRBYFLOAT | RPUSHX | ZRANGEBYSCORE |
| | | RANDOMKEY | INCRBY | SMOVE | HSETNX | LREM | ZREVRANGEBYSCORE |
| | | RENAMEEX | DECRBY | SRANDMEMBER | HMGET | LSET | ZREM |
Expand Down
23 changes: 12 additions & 11 deletions README_zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ English | [简体中文](https://github.com/diiyw/nodis/blob/main/README_zh-cn.m
| PING | FLUSHDB | EXISTS | SET | SSCAN | HGET | RPUSH | ZCARD |
| QUIT | SAVE | EXPIRE | INCR | SCARD | HDEL | LPOP | ZRANK |
| ECHO | INFO | EXPIREAT | DECR | SPOP | HLEN | RPOP | ZREVRANK |
| | | KEYS | SETBIT | SDIFF | HKEYS | LLEN | ZSCORE |
| | | TTL | GETBIT | SINTER | HEXISTS | LINDEX | ZINCRBY |
| | | RENAME | INCR | SISMEMBER | HGETALL | LINSERT | ZRANGE |
| | | TYPE | DESR | SMEMBERS | HINCRBY | LPUSHX | ZREVRANGE |
| DBSIZE | | KEYS | SETBIT | SDIFF | HKEYS | LLEN | ZSCORE |
| MULTI | | TTL | GETBIT | SINTER | HEXISTS | LINDEX | ZINCRBY |
| DISCARD | | RENAME | INCR | SISMEMBER | HGETALL | LINSERT | ZRANGE |
| EXEC | | TYPE | DESR | SMEMBERS | HINCRBY | LPUSHX | ZREVRANGE |
| | | SCAN | SETEX | SREM | HICRBYFLOAT | RPUSHX | ZRANGEBYSCORE |
| | | RANDOMKEY | INCRBY | | HSETNX | LREM | ZREVRANGEBYSCORE |
| | | | DECRBY | | HMGET | LSET | ZREM |
| | | | | | HMSET | LRANGE | ZREMRANGEBYRANK |
| | | | | | HCLEAR | LPOPRPUSH | ZREMRANGEBYSCORE |
| | | | | | HSCAN | RPOPLPUSH | ZCLEAR |
| | | | | | HVALS | | ZEXISTS |
| | | | | | | | |
| | | RANDOMKEY | INCRBY | SMOVE | HSETNX | LREM | ZREVRANGEBYSCORE |
| | | RENAMEEX | DECRBY | SRANDMEMBER | HMGET | LSET | ZREM |
| | | PERSIST | SETNX | SINTERSTORE | HMSET | LRANGE | ZREMRANGEBYRANK |
| | | | INCRBYFLOAT | SUNIONSTORE | HCLEAR | LPOPRPUSH | ZREMRANGEBYSCORE |
| | | | APPEND | | HSCAN | RPOPLPUSH | ZCLEAR |
| | | | GETRANGE | | HVALS | BLPOP | ZEXISTS |
| | | | STRLEN | | | BRPOP | ZUNIONSTORE |
| | | | STRLEN | | | BRPOP | ZINTERSTORE |
## Get Started
```bash
go get github.com/diiyw/[email protected]
Expand Down
42 changes: 42 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func getCommand(name string) func(n *Nodis, conn *redis.Conn, cmd *redis.Command
return quit
case "FLUSHDB":
return flushDB
case "MULTI":
return multi
case "DISCARD":
return discard
case "EXEC":
return exec
case "FLUSHALL":
return flushDB
case "SAVE":
Expand Down Expand Up @@ -304,6 +310,42 @@ func flushDB(n *Nodis, conn *redis.Conn, cmd *redis.Command) {
conn.WriteOK()
}

func multi(n *Nodis, conn *redis.Conn, cmd *redis.Command) {
if conn.Multi {
conn.WriteError("ERR MULTI calls can not be nested")
return
}
conn.Multi = true
conn.WriteOK()
}

func discard(n *Nodis, conn *redis.Conn, cmd *redis.Command) {
conn.Multi = false
conn.Commands = nil
conn.WriteOK()
}

func exec(n *Nodis, conn *redis.Conn, cmd *redis.Command) {
if !conn.Multi {
conn.WriteError("ERR EXEC without MULTI")
return
}
if len(conn.Commands) == 0 {
conn.WriteArray(0)
return
}
conn.WriteArray(len(conn.Commands))
for _, command := range conn.Commands {
c := getCommand(command.Name)
if c == nil {
conn.WriteError("ERR unknown command: " + command.Name)
return
}
c(n, conn, command)
}
conn.Multi = false
}

func del(n *Nodis, conn *redis.Conn, cmd *redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("DEL requires at least one argument")
Expand Down
16 changes: 16 additions & 0 deletions nodis.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ func (n *Nodis) Serve(addr string) error {
conn.WriteError("ERR unknown command: " + cmd.Name)
return
}
if conn.Multi {
if cmd.Name == "MULTI" {
conn.WriteError("ERR MULTI calls can not be nested")
return
}
if cmd.Name != "MULTI" && cmd.Name != "EXEC" {
newCmd := &redis.Command{
Name: cmd.Name,
Options: cmd.Options,
}
newCmd.Args = cmd.Args
conn.Commands = append(conn.Commands, newCmd)
conn.WriteString("QUEUED")
return
}
}
func() {
defer func() {
if r := recover(); r != nil {
Expand Down
1 change: 1 addition & 0 deletions redis/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Conn struct {
*Reader
*Writer
Network net.Conn
Multi bool
Commands []*Command
}

Expand Down

0 comments on commit c6500ef

Please sign in to comment.