From 4ca09febb40130338f4bd134c0b152a2e6c6609e Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Tue, 2 Nov 2021 20:35:28 +0800 Subject: [PATCH] feat(shell): add recover aof --- shell/make_service.py | 11 +++++++++++ storage/temp/dao.template | 6 +++++- storage/temp/dao_aof.template | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 storage/temp/dao_aof.template diff --git a/shell/make_service.py b/shell/make_service.py index 83923d8..218ea0e 100644 --- a/shell/make_service.py +++ b/shell/make_service.py @@ -143,11 +143,22 @@ def gen_aof(proto): temp_path = f"{aofPath}/codec.gen.go" with open(temp_path, 'w', encoding='utf-8') as f: f.write(text) + + # 生成 AOF 恢复机制 + tem_text = load_template("dao_aof.template") + template = Template(tem_text) + + text = template.render(keys=proto) + + temp_path = f"{daoPath}/dao.gen.go" + with open(temp_path, 'w', encoding='utf-8') as f: + f.write(text) def format_code_go(): go_fmt(f"{daoPath}/interface.gen.go") go_fmt(f"{servicePath}/single_service.gen.go") go_fmt(f"{aofPath}/codec.gen.go") + go_fmt(f"{daoPath}/dao.gen.go") def main(): # 加载 protobuf diff --git a/storage/temp/dao.template b/storage/temp/dao.template index 55a2b90..0ea1134 100644 --- a/storage/temp/dao.template +++ b/storage/temp/dao.template @@ -3,10 +3,14 @@ package dao -import "gitee.com/wheat-os/wheatCache/pkg/proto" +import ( + "gitee.com/wheat-os/wheatCache/pkg/proto" + protobuf "google.golang.org/protobuf/proto" +) type Interface interface { {%for key in keys %} {{key.method}}({% for req in key.option %} {{req[1]}}, {% endfor %}) (*proto.{{key.method}}Response, error) {%- endfor %} + ExecMessage(message protobuf.Message) error } diff --git a/storage/temp/dao_aof.template b/storage/temp/dao_aof.template new file mode 100644 index 0000000..37dcac1 --- /dev/null +++ b/storage/temp/dao_aof.template @@ -0,0 +1,33 @@ +// Code generated by gen-struct. DO NOT EDIT. +// make gen-service generated +package dao + +import ( + "gitee.com/wheat-os/wheatCache/pkg/errorx" + "gitee.com/wheat-os/wheatCache/pkg/lru" + "gitee.com/wheat-os/wheatCache/pkg/proto" + protobuf "google.golang.org/protobuf/proto" +) + +type Dao struct { + lru lru.CacheInterface +} + +func NewDao(lru lru.CacheInterface) Interface { + return &Dao{ + lru: lru, + } +} + +// 执行 msg +func (d *Dao) ExecMessage(message protobuf.Message) error { + switch req := message.(type) { + {%for key in keys %} + case *proto.{{key.method}}Request: + _, err := d.{{key.method}}({% for req in key.option %} req.{{req[0]}}, {% endfor %}) + return err + {%- endfor %} + default: + return errorx.New("The type that is not registered exec err") + } +}