openYuanrong 官网:官网
gitcode仓库:仓库
接口免序列化与反序列化
openYuanrong 支持在使用Put及invoke接口时直接传入YR::Buffer类型的参数,此时可免序列化。
在使用Get接口时支持直接返回YR::Buffer类型,此时可免反序列化。这种方式可以有效提高接口性能。
在
Put接口中传入的YR::Buffer参数不能为空指针或 size 为0,否则会抛出异常 1001。
使用场景
Put接口传入YR::Buffer类型的参数,使用Get接口获取YR::Buffer类型的返回值。- Cpp 程序跨语言调用 Python 函数时,在
invoke接口中传入YR::Buffer类型的参数,调用Get接口获取YR::Buffer类型的返回值。
使用示例
使用 Put/Get 接口
示例使用Put接口传入YR::Buffer类型的参数,然后调用Get接口获取YR::Buffer类型的返回值。
示例代码:
#include <string> #include "yr/yr.h" int main(int argc, char *argv[]) { YR::Init(YR::Config{}, argc, argv); YR::CreateParam param; param.writeMode = YR::WriteMode::NONE_L2_CACHE_EVICT; param.consistencyType = YR::ConsistencyType::PRAM; std::string str = "success"; YR::Buffer yrBuf(str.data(), str.length()); auto resRef = YR::Put(yrBuf, param); auto value = YR::Get(resRef); std::string result = std::string(static_cast<const char*>(value->ImmutableData()), value->GetSize()); std::cout << result << std::endl; YR::Finalize(); return 0; }Cpp 程序中调用 Python 函数
这是一个 Cpp 程序跨语言调用 Python 函数的示例,在invoke接口中传入YR::Buffer类型参数,然后使用Get接口获取YR::Buffer类型的返回值。
示例代码:
importyrdefecho(str):returnstr#include <string> #include "yr/yr.h" int main(int argc, char *argv[]) { YR::Init(YR::Config{}, argc, argv); YR::CreateParam param; param.writeMode = YR::WriteMode::NONE_L2_CACHE_EVICT; param.consistencyType = YR::ConsistencyType::PRAM; std::string str = "success"; YR::Buffer yrBuf(str.data(), str.length()); auto ret = YR::PyFunction<YR::Buffer>("common", "echo") .SetUrn("sn:cn:yrk:default:function:0-yr-stpython:$latest") .Invoke(yrBuf); auto value = YR::Get(ret); std::string result = std::string(static_cast<const char*>(value->ImmutableData()), value->GetSize()); std::cout << result << std::endl; YR::Finalize(); return 0; }