Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua: redis support expire #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ server_id=1 # 配置 MySQL replaction 需要定义,不要和 go-mysql-transfer

# 更新日志

**v1.0.0 bate**
**v1.0.0 beta**

* 9.17 初始化提交bate版本
* 9.17 初始化提交beta版本

**v1.0.1 release**

Expand Down
7 changes: 7 additions & 0 deletions service/endpoint/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"strings"
"sync"
"time"

"github.com/go-redis/redis"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -265,6 +266,12 @@ func (s *RedisEndpoint) preparePipe(resp *model.RedisRespond, pipe redis.Cmdable
val := redis.Z{Score: resp.Score, Member: resp.Val}
pipe.ZAdd(resp.Key, val)
}
default:
if resp.Action == "expire" {
vv, _ := resp.Val.(float64)
var s int64 = int64(vv) * 1000000000 //trans to ns
pipe.Expire(resp.Key, time.Duration(s))
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion service/luaengine/redis_actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package luaengine

import (
"github.com/siddontang/go-mysql/canal"
"github.com/yuin/gopher-lua"
lua "github.com/yuin/gopher-lua"

"go-mysql-transfer/global"
"go-mysql-transfer/model"
Expand Down Expand Up @@ -54,6 +54,8 @@ var _redisModuleApi = map[string]lua.LGFunction{

"ZADD": redisZAdd,
"ZREM": redisZRem,

"EXPIRE": redisExpire,
}

func rawOldRow(L *lua.LState) int {
Expand All @@ -62,6 +64,14 @@ func rawOldRow(L *lua.LState) int {
return 1
}

func redisExpire(L *lua.LState) int {
key := L.CheckString(1)
val := L.CheckAny(2)
ret := L.GetGlobal(_globalRET)
L.SetTable(ret, lua.LString("expire_0_"+key), val)
return 0
}

func redisSet(L *lua.LState) int {
key := L.CheckString(1)
val := L.CheckAny(2)
Expand Down Expand Up @@ -224,6 +234,12 @@ func DoRedisOps(input map[string]interface{}, previous map[string]interface{}, a
ls = append(ls, resp)
})

var lsLen = len(ls)
if lsLen > 1 && ls[0].Action == "expire" {
// "expire" it will not work when the key not exist, swap it
ls[0], ls[lsLen-1] = ls[lsLen-1], ls[0]
}

return ls, nil
}

Expand Down