Repository navigation
Expand file tree
/
Copy pathRedis.cpp
More file actions
155 lines (126 loc) · 4.36 KB
/
Copy pathRedis.cpp
File metadata and controls
155 lines (126 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by daimiaopeng on 2019/11/17.
//
#include "Redis.h"
Redis::Redis(string ip, u_int port, string passwd) : ip(ip), port(port) {
conn = redisConnect(ip.c_str(), port);
if (conn->err) printf("connection error:%s\n", conn->errstr);
redisReply *reply = static_cast<redisReply *>(redisCommand(conn, "AUTH %s", passwd.c_str()));
if (reply->type == REDIS_REPLY_ERROR) {
LOG(INFO) << "Redis认证失败!";
} else {
LOG(INFO) << "Redis认证成功!";
}
clearToken();
freeReplyObject(reply);
};
Redis::Redis(string ip, u_int port) : ip(ip), port(port) {
conn = redisConnect(ip.c_str(), port);
if (conn->err) LOG(ERROR) << "connection error: " << conn->errstr;
clearToken();
};
auto Redis::redisReply_ptr(void *reply) {
auto delRedisReply = [](redisReply *reply) {
freeReplyObject(reply);
};
unique_ptr<redisReply, decltype(delRedisReply)> reply_unique_ptr(reinterpret_cast<redisReply *>(reply),
delRedisReply);
return reply_unique_ptr;
}
void Redis::pushMessageQueue(const string &message) {
auto reply = redisReply_ptr(redisCommand(conn, "RPUSH messageQueue %s", message.c_str()));
LOG(WARNING) << "添加到消息队列 :" << message;
}
int Redis::lenMessage() {
string str = "LLEN messageQueue";
auto reply = redisReply_ptr(redisCommand(conn, str.c_str()));
int len = reply->integer;
return len;
}
string Redis::popMessageQueue() {
auto reply = redisReply_ptr(redisCommand(conn, "LPOP messageQueue"));
string str = reply->str;
LOG(INFO) << "从消息队列取出:" << str;
return str;
}
string Redis::getName(const string &token) {
auto reply = redisReply_ptr(redisCommand(conn, "HMGET token %s", token.c_str()));
string str;
auto element = reply->element;
if ((*element)->str == nullptr) {
str = "";
return str;
}
str = (*element)->str;
LOG(INFO) << token + "(token->name)" << str;
return str;
}
int Redis::getFd(const string &name) {
auto reply = redisReply_ptr(redisCommand(conn, "HMGET fdName %s", name.c_str()));
string str;
auto element = reply->element;
if ((*element)->str == nullptr) {
str = "-1";
} else {
str = (*element)->str;
}
return stoi(str);
}
void Redis::setName(int fd, const string &name) {
auto reply = redisReply_ptr(redisCommand(conn, "HMSET fdName %s %d", name.c_str(), fd));
}
string Redis::login(const string &name, const string &passwd) {
auto reply = redisReply_ptr(redisCommand(conn, "HMGET login %s", name.c_str()));
auto element = reply->element;
if ((*element)->str == nullptr) {
LOG(INFO) << name + " 该用户没有注册";
return "";
}
string str = (*element)->str;
if (passwd == str) {
LOG(INFO) << name + " 登录成功";
string token = name;
setToken(token, name);
return token;
} else {
LOG(INFO) << name + " 登录密码错误";
return "";
}
}
int Redis::registered(const string &name, const string &passwd) {
auto reply = redisReply_ptr(redisCommand(conn, "HMGET login %s", name.c_str()));
auto element = reply->element;
if ((*element)->str != nullptr) {
LOG(INFO) << name + " 该用户已注册";
return 0;
}
redisCommand(conn, "HMSET login %s %s", name.c_str(), passwd.c_str());
LOG(INFO) << name + " 注册成功";
return 1;
}
void Redis::setToken(const string &token, const string &name) {
auto reply = redisReply_ptr(redisCommand(conn, "HMSET token %s %s", token.c_str(), name.c_str()));
LOG(INFO) << name + " (name->token)" << token;
}
void Redis::init() {
}
int Redis::getRegisterNums() {
auto reply = redisReply_ptr(redisCommand(conn, "HLEN login"));
LOG(INFO) << "当前注册人数:"<<reply->integer;
return reply->integer;
}
set<string> Redis::geOnile() {
set<string> all;
auto reply = redisReply_ptr(redisCommand(conn, "HGETALL token"));
auto element = reply->element;
for (int i =0;i<reply->elements;i= i+2){
//取name而不是token
all.insert(move(string(element[i+1]->str)));
}
// LOG(INFO) << "当前在线人: "<<(*element)->;
return all;
}
void Redis::clearToken() {
redisReply_ptr(redisCommand(conn, "DEL token"));
LOG(INFO) <<"token 清空初始化成功";
}