From 13218ccee703e3373ab0e8b28d78b46916811917 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Wed, 9 May 2018 18:11:56 +0800 Subject: [PATCH] add session cache in HttpAppFramework --- CMakeLists.txt | 6 +----- lib/src/HttpAppFramework.cc | 42 ++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e88e1ff..44809b37 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,11 +28,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules/) find_package (jsoncpp REQUIRED) include_directories(${JSONCPP_INCLUDE_DIRS}) link_directories(${JSONCPP_LIBRARY_DIRS}) -find_package (UUID) - -if(UUID_FOUND) - add_definitions(-DUSE_UUID) -endif() +find_package (UUID REQUIRED) find_package(Boost) if(Boost_FOUND) diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index 6098530e..c9968ba9 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -12,9 +12,8 @@ #include #include #include -#ifdef USE_UUID #include -#endif + namespace drogon { @@ -29,11 +28,15 @@ namespace drogon uint16_t _port; void onAsyncRequest(const HttpRequest& req,std::functioncallback); void readSendFile(const std::string& filePath,const HttpRequest& req, HttpResponse* resp); -#ifdef USE_UUID + //if uuid package found,we can use a uuid string as session id; //set _sessionTimeout=0 to disable location session control based on cookies; uint _sessionTimeout=0; -#endif + typedef std::shared_ptr SessionPtr; + std::unique_ptr> _sessionMapPtr; + + + bool _enableLastModify=true; std::set _fileTypeSet={"html","jpg"}; std::string _rootPath; @@ -41,10 +44,10 @@ namespace drogon //tool funcs -#ifdef USE_UUID + std::string getUuid(); std::string stringToHex(unsigned char* ptr, long long length); -#endif + }; } @@ -63,6 +66,7 @@ void HttpAppFrameworkImpl::run() trantor::EventLoop loop; HttpServer httpServer(&loop,InetAddress(_ip,_port),""); httpServer.setHttpAsyncCallback(std::bind(&HttpAppFrameworkImpl::onAsyncRequest,this,_1,_2)); + _sessionMapPtr=std::unique_ptr>(new CacheMap(&loop,1,1200)); httpServer.start(); loop.loop(); } @@ -91,7 +95,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::function0) @@ -100,17 +104,17 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::function(),_sessionTimeout); + _sessionMapPtr->insert(session_id,std::make_shared< Session >(),_sessionTimeout); } else { -// if(_sessionMap.find(session_id)==false) -// { -// _sessionMap.insert(session_id,std::make_shared< Session >(),_sessionTimeout); -// } + if(_sessionMapPtr->find(session_id)==false) + { + _sessionMapPtr->insert(session_id,std::make_shared< Session >(),_sessionTimeout); + } } } -#endif + std::string path = req.path().c_str(); auto pos = path.rfind("."); if(pos != std::string::npos) { @@ -120,10 +124,10 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::functionsetBody(str); delete contents; } -#ifdef USE_UUID + std::string HttpAppFrameworkImpl::getUuid() { uuid_t uu; @@ -296,7 +300,7 @@ std::string HttpAppFrameworkImpl::stringToHex(unsigned char* ptr, long long leng } return idString; } -#endif +