Skip to content

Commit

Permalink
redis格式化优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Jun 4, 2024
1 parent db702f6 commit ee8f966
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
6 changes: 6 additions & 0 deletions extend/lcodec/lcodec.lmak
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ TARGET_NAME = "lcodec"
----工程类型: static/dynamic/exe
PROJECT_TYPE = "dynamic"

--需要定义的选项
DEFINES = {
"FMT_HEADER_ONLY"
}

--需要的include目录
INCLUDES = {
"../lua/lua",
"../fmt/include",
"../luakit/include"
}

Expand Down
2 changes: 2 additions & 0 deletions extend/lcodec/lcodec.mak
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ STDCPP = -std=c++17

#需要的include目录
MYCFLAGS += -I../lua/lua
MYCFLAGS += -I../fmt/include
MYCFLAGS += -I../luakit/include

#需要定义的选项
MYCFLAGS += -DFMT_HEADER_ONLY

#LDFLAGS
LDFLAGS =
Expand Down
4 changes: 2 additions & 2 deletions extend/lcodec/lcodec.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\lua\lua;..\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\lua\lua;..\fmt\include;..\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;FMT_HEADER_ONLY;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<PrecompiledHeader></PrecompiledHeader>
Expand Down
17 changes: 6 additions & 11 deletions extend/lcodec/src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#endif

#include "lua_kit.h"
#include "fmt/core.h"

using namespace std;
using namespace luakit;

namespace lcodec {
inline size_t CRLF_LEN = 2;
inline size_t HEAD_SIZE = 24;
inline const char* RDS_CRLF = "\r\n";

class rdscodec : public codec_base {
Expand All @@ -27,8 +27,7 @@ namespace lcodec {
m_buf->clean();
int n = lua_gettop(L);
uint32_t session_id = lua_tointeger(L, index++);
char* head = (char*)m_buf->peek_space(HEAD_SIZE);
m_buf->pop_space(sprintf(head, "*%d\r\n", n - index + 1));
m_buf->write(fmt::format("*{}\r\n", n - index + 1));
for (int i = index; i <= n; ++i) {
encode_bulk_string(L, i);
}
Expand Down Expand Up @@ -160,28 +159,24 @@ namespace lcodec {

void number_encode(double value) {
auto svalue = std::to_string(value);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size());
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str()));
m_buf->write(fmt::format("${}\r\n{}\r\n", svalue.size(), svalue.c_str()));
}

void integer_encode(int64_t integer) {
auto svalue = std::to_string(integer);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size());
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str()));
m_buf->write(fmt::format("${}\r\n{}\r\n", svalue.size(), svalue.c_str()));
}

void string_encode(lua_State* L, int idx) {
size_t len;
const char* data = lua_tolstring(L, idx, &len);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + len);
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", len, data));
m_buf->write(fmt::format("${}\r\n{}\r\n", len, string_view(data, len)));
}

void table_encode(lua_State* L, int idx) {
size_t len;
char* body = (char*)m_jcodec->encode(L, idx, &len);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + len);
m_buf->pop_space(sprintf(head, "$%zd\r\n[js]%s\r\n", len + 4, body));
m_buf->write(fmt::format("${}\r\n[js]{}\r\n", len + 4, string_view(body, len)));
}

void encode_bulk_string(lua_State* L, int idx) {
Expand Down
47 changes: 30 additions & 17 deletions server/business/component/attr_component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,23 @@ function AttrComponent:__delegate()
return this:set_attr(attr.id, value)
end
if attr.increase then
AttrComponent["add_" .. attr.nick] = function(this, value, ncheck)
AttrComponent["add_" .. attr.nick] = function(this, value)
if value < 0 then
return this:cost_attr(attr.id, -value, ncheck)
return this:sub_attr(attr.id, -value)
end
return this:add_attr(attr.id, value)
end
AttrComponent["cost_" .. attr.nick] = function(this, value, ncheck)
AttrComponent["sub_" .. attr.nick] = function(this, value)
if value < 0 then
return this:add_attr(attr.id, -value)
end
return this:cost_attr(attr.id, value, ncheck)
return this:sub_attr(attr.id, value)
end
AttrComponent["cost_" .. attr.nick] = function(this, value)
if value <= 0 then
return false
end
return this:cost_attr(attr.id, value)
end
end
end
Expand All @@ -78,7 +84,7 @@ end

--停止对某个服务属性共享
function AttrComponent:stop_share(service_name)
for _, attr_info in pairs(self.attrset) do
for _, attr_info in pairs(self.attr_set) do
if attr_info.services[service_name] then
attr_info.services[service_name] = nil
end
Expand Down Expand Up @@ -135,9 +141,10 @@ function AttrComponent:on_db_player_attr_load(data)
log_debug("[AttrComponent][on_db_player_attr_load] data({})", data)
if data.player_id then
self:load_attrs(data.attrs or {})
return true
event_mgr:notify_trigger("on_player_attr_init", self)
return
end
event_mgr:notify_trigger("on_player_attr_init", self)
event_mgr:notify_trigger("on_player_attr_init", self, true)
return true
end

Expand All @@ -146,7 +153,7 @@ end
function AttrComponent:set_attr(attr_id, value, service_id)
local attr = self.attr_set[attr_id]
if not attr or not value then
log_warn("[AttrComponent][set_attr] attr({}-{}) is not vaild", attr_id)
log_warn("[AttrComponent][set_attr] attr({}-{}) is not vaild", attr_id, value)
return false
end
if attr.complex then
Expand Down Expand Up @@ -224,7 +231,7 @@ function AttrComponent:update_attr(attr_id)
self:set_attrs_field(attr_id, value)
--通知改变
if self:is_load_success() then
self:notify_attr(attr_id, value, cur_val, self.attrset[attr_id])
self:notify_attr(attr_id, value, cur_val, self.attr_set[attr_id])
end
end

Expand All @@ -251,7 +258,7 @@ function AttrComponent:check_attr(attr_id, value)
return false
end

--增加属性
--加属性
function AttrComponent:add_attr(attr_id, value)
if not value then
return false
Expand All @@ -260,17 +267,23 @@ function AttrComponent:add_attr(attr_id, value)
return self:set_attr(attr_id, ovalue + value)
end

--消耗属性
function AttrComponent:cost_attr(attr_id, value, ncheck)
--减属性
function AttrComponent:sub_attr(attr_id, value)
if not value then
return false
end
local ovalue = self.attrs[attr_id]
if ncheck then
return self:set_attr(attr_id, ovalue > value and (ovalue - value) or 0)
local nvalue = self.attrs[attr_id] - value
return self:set_attr(attr_id, nvalue)
end

--消耗属性
function AttrComponent:cost_attr(attr_id, value)
if not value then
return false
end
if ovalue >= value then
return self:set_attr(attr_id, ovalue - value)
local nvalue = self.attrs[attr_id] - value
if nvalue >= 0 then
return self:set_attr(attr_id, nvalue)
end
return false
end
Expand Down

0 comments on commit ee8f966

Please sign in to comment.