-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
91 lines (82 loc) · 3.11 KB
/
Copy pathmain.cc
File metadata and controls
91 lines (82 loc) · 3.11 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
#include <getopt.h>
#include <boost/lexical_cast.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <lite.hpp>
#include <string>
#include "packet.hpp"
#include "service.hpp"
void PrintHelp() {
LOG(INFO) << "Usage: LiteMemcached [-t thread_num] [-s size] \n"
" [-p port] [-h help]\n";
LOG(INFO) << "Options:\n";
LOG(INFO) << " -t, --thread_num Number of worker threads. Default: \n"
" hardware_concurrency() - 1\n";
LOG(INFO)
<< " -s, --size Max number of items in cache. Default: \n"
" 1024\n";
LOG(INFO) << " -p, --port Default: 11211\n";
LOG(INFO) << " -h, --help Show this help message.\n";
LOG(INFO) << "Example:\n";
LOG(INFO) << " LiteMemcached -t 128 -m 1GiB -p 11211\n";
LOG(INFO) << " LiteMemcached --thread_num=4 --size=256\n";
}
int main(int argc, char* argv[]) {
try {
FLAGS_logtostderr = 1;
FLAGS_logbufsecs = 0;
FLAGS_log_dir = "/workspace/Memcached_codes";
google::InitGoogleLogging(argv[0]);
size_t thread_pool_size = boost::thread::hardware_concurrency() - 1;
size_t cache_size(1024);
const char* port = "11211";
const char* const short_opts = "t:s:p:a:h";
const option long_opts[] = {{"thread_num", required_argument, nullptr, 't'},
{"size", required_argument, nullptr, 's'},
{"port", required_argument, nullptr, 'p'},
{"address", required_argument, nullptr, 'a'},
{"help", no_argument, nullptr, 'h'},
{0, 0, 0, 0}};
int opt;
while ((opt = getopt_long(argc, argv, short_opts, long_opts, nullptr)) !=
-1) {
switch (opt) {
case 't':
thread_pool_size = boost::lexical_cast<size_t>(optarg);
break;
case 's':
cache_size = boost::lexical_cast<size_t>(optarg);
break;
case 'p':
port = optarg;
break;
case 'a':
break;
case 'h':
default:
PrintHelp();
return 0;
}
}
LOG(INFO) << "LiteMemcached starts" << std::endl;
LOG(INFO) << "\tlistening on port: " << port << std::endl;
LOG(INFO) << "\tthread_pool_size: " << thread_pool_size << std::endl;
LOG(INFO) << "\tsize: " << cache_size << std::endl;
// Initialise the server.
// TODO: make address and port configurable.
std::string backend_addr = "";
std::string backend_port = "/tmp/memcached.sock";
// std::string backend_addr = "127.0.0.1";
// std::string backend_port = "60000";
Memcached memcached;
lite::LiteServer<Memcached, Packet, Packet, ConnectionInfo,
std::vector<uint8_t>, CacheEntry>
s(thread_pool_size, cache_size, memcached, backend_addr, backend_port,
1000ms, 10000, 0.9, 1, "/tmp/lite_memcached", false);
// Run the server until stopped.
s.Run(port);
} catch (std::exception& e) {
std::cerr << "exception: " << e.what() << "\n";
}
return 0;
}