解决nginx如何存储HTTP客户端信息的问题
2017-05-17 23:37
405 查看
解决nginx如何存储HTTP客户端信息的问题
背景
在web应用软件开发的过程中,服务器端需要存储Session信息来保存HTTP客户端的连接信息。客户端信息包括用户名、用户二维码、二维码时间戳等等。
SessionMap.cpp
用于测试的testMap.cpp
g++ -o test SessionMap.o testMap.o
运行test可执行文件就可以了。
背景
在web应用软件开发的过程中,服务器端需要存储Session信息来保存HTTP客户端的连接信息。客户端信息包括用户名、用户二维码、二维码时间戳等等。解决方法
定义一个User结构体存储用户信息。定义一个类,在该类中定义一个static map<string ,User>变量。其中string代表客户端的cookie key ,User代表cookie key对应的用户信息。示例代码
SessionMap.h#ifndef _MAP_ #define _MAP_ #include <map> #include <iostream> #include <string> #include <string.h> using namespace std; //用户信息 typedef struct userInfo{ userInfo(){ userName=""; userTimeStamp=0; qrCode =""; qrTimeStamp =0; phoneNum=""; passCode=""; codeTimeStamp=0; certRandomStr=""; certTimeStamp=0; } string userName; int userTimeStamp; string certRandomStr; int certTimeStamp; string qrCode; int qrTimeStamp; string phoneNum; string passCode; int codeTimeStamp; }User; //全局对象类 class Session { public: typedef std::map<std::string, User> OpMap; static void setpair(const string& key,const User& value){ opMap.insert(make_pair(key.c_str(),value)); } static int getCount(){ return opMap.size(); } static User getpairValue(const string& key){ map<string,User>::iterator map_it = opMap.find(key.c_str()); if(map_it != opMap.end()){ return map_it->second; }else{ return User(); } } static void updateValue(const string& key,const User& value ){ map<string,User>::iterator map_it = opMap.find(key.c_str()); if(map_it != opMap.end()){ map_it->second = value; } } static void deleteUser(const string& key){ map<string,User>::iterator iter=opMap.find(key.c_str()); if(iter != opMap.end()){ opMap.erase(iter); } } static void printMap(){ for (std::map<string,User>::iterator it=Session::opMap.begin(); it!=Session::opMap.end(); ++it) { std::cout << it->first <<":"<< it->second.userName << '\n'; } } static void getJsonFormatUser( string &userinfo){ Session::deleteUser(""); userinfo="{\"userInfo\": \[";; for (std::map<string,User>::iterator it=Session::opMap.begin(); it!=Session::opMap.end(); ++it) { userinfo += "{ \"username\":\""; userinfo += it->second.userName; userinfo += "\" },"; } userinfo.replace(userinfo.length()-1,1,""); userinfo +="] }"; } static void deleteUserByName(const string& name){ for (std::map<string,User>::iterator it=Session::opMap.begin(); it!=Session::opMap.end(); ++it) { if( strcmp(it->second.userName.c_str(),name.c_str())==0 ){ opMap.erase(it); } } } static string geUserByQrCode(const string& value){ for(map<string,User>::iterator it=Session::opMap.begin(); it!=Session::opMap.end(); ++it) { string s1 = (it->second.qrCode); if( strcmp(s1.c_str(),value.c_str()) ==0 ){ return it->first; } } } public: private: static OpMap opMap; //初始化函数 static OpMap init_map() { OpMap mapinit; mapinit[""] = User(); return mapinit; } }; #endif
SessionMap.cpp
#include "SessionMap.h" Session::OpMap Session::opMap = Session::init_map();
用于测试的testMap.cpp
#include "SessionMap.h" int main(){ User u1; u1.userName="22222"; Session::setpair("111",u1); User u2; u2.userName="daafa22222"; Session::updateValue("111",u2); User user = Session::getpairValue("111"); cout<<user.userName<<endl; User u1; u1.phoneNum = "111111"; return 0; }
编译
g++ -c SessionMap.cpp testMap.cppg++ -o test SessionMap.o testMap.o
运行test可执行文件就可以了。
总结
可以使用类的static成员可以做为全局变量的优点来实现存储系统信息。相关文章推荐
- 解决Nginx+Tomcat下客户端https请求跳转成http的问题
- iOS开发工具-如何使用网络封包分析工具Charles,通过配置proxy对http、https、tcp、udp 等协议的请求响应过程交互信息进行分析、判断、解决我们移动开发中的遇到的各种实际问题。
- 如何解决Remoting无法传输存储过程参数的问题
- 绝对酷,如何解决asp.net中javascript脚本的问题(使用服务器控件执行客户端脚本)
- 如何解决resin与nginx共用同一端口的问题
- [技术分享- ISA篇] 如何解决IE7/8客户端与ISA验证时弹框的问题
- 如何解决XmlHttp.responseText的乱码问题
- 关于PS批处理存储JPG时总是要选JPG质量的问题,如何解决
- WCF热门问题编程示例(1):WCF服务如何获得客户端IP地址信息
- JQuery ajax 如何设置同步调用(同时只能触发一个函数) 解决与层显示信息时候的冲突问题
- 如何解决WCF REST 中 WebFaultException抛出的异常客户端WebChannelFactory无法处理的问题
- 解决“从用户数据存储中检索信息时出错。未找到平台。”问题。
- WCF热门问题编程示例(1):WCF服务如何获得客户端IP地址信息
- [问题]VB通信中怎么解决1(服务器)对多(客户端)的发信息?
- 如何解决Remoting无法传输存储过程参数的问题
- 解决“从用户数据存储中检索信息时出错。未找到平台。”问题
- 程序员最痛苦的事,就是程序出错;程序员最最痛苦的事,就是程序出错了还没有错误信息!--IIS Service Unavailable 问题如何解决
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- WCF热门问题编程示例(1):WCF服务如何获得客户端IP地址信息
- 如何解决服务器虚拟化中遇到的存储问题