diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3252171 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fd4dce2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,78 @@ +cmake_minimum_required(VERSION 3.5) + +set(MODULE_NAME "localstorage") +# 设置项目名为当前目录名 +project(${MODULE_NAME}) + +# 搜索源文件和头文件 +file(GLOB_RECURSE SOURCE_FILES "${PROJECT_SOURCE_DIR}/src/*.cpp") +file(GLOB_RECURSE HEADER_FILES + "${PROJECT_SOURCE_DIR}/src/*.h" +) + +# 将源文件分配到 Source Files 文件夹 +foreach(source IN LISTS SOURCE_FILES) + get_filename_component(source_path "${source}" PATH) + file(RELATIVE_PATH source_path_rel "${PROJECT_SOURCE_DIR}" "${source_path}") + string(REPLACE "/" "\\" source_path_rel_win "${source_path_rel}") + source_group("Source Files\\${source_path_rel_win}" FILES "${source}") +endforeach() + +# 将头文件分配到 Header Files 文件夹 +foreach(header IN LISTS HEADER_FILES) + get_filename_component(header_path "${header}" PATH) + file(RELATIVE_PATH header_path_rel "${PROJECT_SOURCE_DIR}" "${header_path}") + string(REPLACE "/" "\\" header_path_rel_win "${header_path_rel}") + source_group("Header Files\\${header_path_rel_win}" FILES "${header}") +endforeach() + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) +# 安装复制 +set(CMAKE_INSTALL_ALWAYS_COPY TRUE) + +set(YLIB ${CMAKE_INSTALL_PREFIX}/../ylib) +set(FASTWEB ${CMAKE_INSTALL_PREFIX}/../fastweb) + +# 包含路径 +include_directories( + ${YLIB}/include + ${FASTWEB}/include + ${FASTWEB}/include/lua +) + +# 添加共享库 +add_library(${MODULE_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES}) + +if(MSVC) + target_link_libraries(${MODULE_NAME} PRIVATE + odbc32.lib + User32.lib + Advapi32.lib + IPHLPAPI.lib + WS2_32.lib + Shell32.lib + + ${YLIB}/lib/libcrypto_static_win64.lib + $<$:${FASTWEB}/bin/debug/3rdparty/lib/lua.lib> + $<$:${YLIB}/lib/ylib_d.lib> + $<$:${YLIB}/lib/leveldb_d.lib> + + $<$:${FASTWEB}/bin/release/3rdparty/lib/lua.lib> + $<$:${YLIB}/lib/ylib.lib> + $<$:${YLIB}/lib/leveldb.lib> + ) +else() + target_link_libraries(${MODULE_NAME} + hpsocket + ylib + crypto + lua5.3 + mysqlcppconn + pthread + ) + +endif() + + +install(TARGETS ${MODULE_NAME} DESTINATION $,${FASTWEB}/bin/debug/module/${MODULE_NAME},${FASTWEB}/bin/release/module/${MODULE_NAME}>) \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0b9d68f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 nianhua + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index fecd236..517eecd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # module-localstorage + FastWeb - 本地缓存模块 + +文档:点击访问 diff --git a/src/localstorage.cpp b/src/localstorage.cpp new file mode 100644 index 0000000..6ac9ca6 --- /dev/null +++ b/src/localstorage.cpp @@ -0,0 +1,54 @@ +#include "localstorage.h" +#include "util/localstorage.h" +#include "dll_interface.h" + +extern "C" { +#ifdef _WIN32 + DLL_EXPORT +#endif + int fastweb_module_regist(void* sol2, void* lua) + { + sol::state* state = static_cast(sol2); + module::local_storage::regist(state); + return 0; + } +} + +module::local_storage::local_storage() +{ +} + +module::local_storage::~local_storage() +{ + ::ylib::local_storage::close(); +} + +sol::optional module::local_storage::readex(const std::string& name) +{ + std::string value; + if (this->read(name, value) == false) + return sol::nullopt; + return value; +} + +void module::local_storage::regist(sol::state* lua) +{ + lua->new_usertype("local_storage", + "new", sol::constructors(), + "clear", &module::local_storage::clear, + "close", &module::local_storage::close, + "del", &module::local_storage::del, + "exist", &module::local_storage::exist, + "open", &module::local_storage::open, + "read", &module::local_storage::readex, + "write", &module::local_storage::write, + "self", &module::local_storage::self, + "last_error", &module::local_storage::last_error + ); +} + +void module::local_storage::regist_global(const std::string& name, sol::state* lua) +{ + lua->registry()[name] = this; + (*lua)[name] = this; +} diff --git a/src/localstorage.h b/src/localstorage.h new file mode 100644 index 0000000..1b810ed --- /dev/null +++ b/src/localstorage.h @@ -0,0 +1,27 @@ +#pragma once +#include "util/localstorage.h" +#include "basemodule.h" +namespace module +{ + /// + /// 本地缓存(leveldb) + /// + class local_storage : public ylib::local_storage,public module::base { + public: + local_storage(); + ~local_storage() override; + /// + /// 取数据 + /// + /// + /// + sol::optional readex(const std::string& name); + + static void regist(sol::state* lua); + private: + // 通过 imodule 继承 + virtual void regist_global(const std::string& name, sol::state* lua); + virtual void delete_global() { delete this; } + }; +} +