-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.cpp
243 lines (221 loc) · 5.76 KB
/
redis.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <chrono>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <sw/redis++/redis++.h>
#include "redis.h"
#include "session_manager.h"
const std::string url = "tcp://127.0.0.1:6379";
sw::redis::Redis get_redis()
{
auto ret = sw::redis::Redis(url);
std::ifstream pwd("pwd");
std::string pass;
if (pwd >> pass) {
ret.auth(pass);
//std::cerr << "AUTH_OK" << std::endl;
}
return ret;
}
static std::string escape(const std::string &str)
{
std::ostringstream ret;
for (const char c : str) {
if (c == '<')
ret << "<";
else if (c == '>')
ret << ">";
else
ret << c;
}
return ret.str();
}
static std::string limit(std::string str, size_t size)
{
if (str.size() > size)
str.resize(size);
std::string::iterator i = str.begin();
while (true) {
std::string::iterator j = i;
for (int k = 0; k < 107; k++) {
if (*j == '\n')
k = 1;
if (j != str.end())
++j;
}
if (j == str.end())
break;
while (*j != ' ' && j != str.end())
j++;
if (j == str.end())
break;
*j = '\n';
i = ++j;
}
return str;
}
void Redis::hit()
{
auto redis = get_redis();
//std::cerr << "HIT..." << std::endl;
try {
redis.incr("movepoint.ru:hits");
/*const std::chrono::time_point now{std::chrono::system_clock::now()};
const std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(now)};
std::ostringstream YMD;
YMD << ymd.year() << "/" << ymd.month() << "/" << ymd.day();
std::string key = "movepoint.ru:hits:" + YMD.str();
redis.incr(key);*/
} catch (...) {
std::cerr << "Can't record hit" << std::endl;
//throw;
}
}
long long Redis::hits()
{
auto redis = get_redis();
return redis.command<long long>("get", "movepoint.ru:hits");
}
long long Redis::hits_today()
{
auto redis = get_redis();
/*
const std::chrono::time_point now{std::chrono::system_clock::now()};
const std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(now)};
std::ostringstream YMD;
YMD << ymd.year() << "/" << ymd.month() << "/" << ymd.day();
std::string key = "movepoint.ru:hits:" + YMD.str();
return redis.command<long long>("get", key);
*/
return 0;
}
std::pair<long long, std::string> Redis::new_session()
{
auto redis = get_redis();
auto session = redis.command<long long>("incr", "movepoint.ru:session");
static session_manager sm;
auto shash = sm.get_sessionhash(session);
session_hash(session, shash);
return std::make_pair(session, shash);
}
std::string Redis::new_invite(const std::string &login)
{
auto redis = get_redis();
static session_manager sm;
auto ihash = sm.get_invitehash(login);
invite_hash(login, ihash);
return ihash;
}
void Redis::login_session(long long session, const std::string &login)
{
std::ostringstream K;
K << "movepoint.ru:session:" << session << ":login";
std::string key{K.str()};
auto redis = get_redis();
redis.set(key, login);
}
std::string Redis::session_login(long long session)
{
std::ostringstream K;
K << "movepoint.ru:session:" << session << ":login";
std::string key{K.str()};
auto redis = get_redis();
auto v = redis.get(key);
if (v.has_value())
return v.value();
else
return std::string();
}
std::string Redis::display_name(const std::string &login)
{
std::ostringstream K;
K << "movepoint.ru:acc:" << login << ":dn";
std::string key{K.str()};
auto redis = get_redis();
auto v = redis.get(key);
if (v.has_value())
return v.value();
else
return std::string();
}
void Redis::invite_hash(const std::string &login, const std::string &invitehash)
{
std::ostringstream K;
K << "movepoint.ru:invite:" << login << ":hash";
std::string key{K.str()};
auto redis = get_redis();
redis.set(key, invitehash);
}
void Redis::session_hash(long long session, const std::string &sessionhash)
{
std::ostringstream K;
K << "movepoint.ru:session:" << session << ":hash";
std::string key{K.str()};
auto redis = get_redis();
redis.set(key, sessionhash);
}
std::string Redis::session_hash(long long session)
{
//std::string key{std::format("movepoint.ru:session:{}:hash", session);
std::ostringstream K;
K << "movepoint.ru:session:" << session << ":hash";
//std::string key{std::format("movepoint.ru:session:{}:hash", session);
std::string key{K.str()};
auto redis = get_redis();
auto v = redis.get(key);
if (v.has_value())
return v.value();
else
return std::string();
}
std::string Redis::password_hash(const std::string &login)
{
std::ostringstream K;
K << "movepoint.ru:acc:" << login << ":ph";
std::string key{K.str()};
auto redis = get_redis();
auto v = redis.get(key);
if (v.has_value())
return v.value();
else
return std::string();
}
std::string Redis::invite_hash(const std::string &login)
{
std::ostringstream K;
K << "movepoint.ru:invite:" << login << ":ih";
std::string key{K.str()};
auto redis = get_redis();
auto v = redis.get(key);
if (v.has_value())
return v.value();
else
return std::string();
}
std::string Redis::status()
{
auto redis = get_redis();
std::vector<std::string> statuses;
redis.lrange("movepoint.ru:status", -1, -1, std::back_inserter(statuses));
std::ostringstream ret;
for (const auto &s : statuses)
ret << escape(s);
return ret.str();
}
void Redis::status(const std::string &s)
{
auto redis = get_redis();
std::vector<std::string> statuses;
redis.rpush("movepoint.ru:status", s);
}
std::vector<std::string> Redis::spamlist()
{
auto redis = get_redis();
const char *key = "spamlist";
std::vector<std::string> ret;
redis.lrange(key, 0, -1, std::back_inserter(ret));
return ret;
}