jamesge commented on a change in pull request #972: Redis server protocol URL: https://github.com/apache/incubator-brpc/pull/972#discussion_r354706421
########## File path: example/redis_c++/redis_server.cpp ########## @@ -0,0 +1,146 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// +// A brpc based redis-server. Currently just implement set and +// get, but it's sufficient that you can get the idea how to +// implement brpc::RedisCommandHandler. + +#include <brpc/server.h> +#include <brpc/redis.h> +#include <butil/crc32c.h> +#include <butil/string_splitter.h> +#include <gflags/gflags.h> +#include <unordered_map> + +class RedisServiceImpl : public brpc::RedisService { +public: + bool Set(const std::string& key, const std::string& value) { + int slot = butil::crc32c::Value(key.c_str(), key.size()) % HashSlotNum; + _mutex[slot].lock(); + _map[slot][key] = value; + _mutex[slot].unlock(); + return true; + } + + bool Get(const std::string& key, std::string* value) { + int slot = butil::crc32c::Value(key.c_str(), key.size()) % HashSlotNum; + _mutex[slot].lock(); + auto it = _map[slot].find(key); + if (it == _map[slot].end()) { + _mutex[slot].unlock(); + return false; + } + *value = it->second; + _mutex[slot].unlock(); + return true; + } + +private: + const static int HashSlotNum = 32; + std::unordered_map<std::string, std::string> _map[HashSlotNum]; + butil::Mutex _mutex[HashSlotNum]; +}; + +class GetCommandHandler : public brpc::RedisCommandHandler { +public: + GetCommandHandler(RedisServiceImpl* rsimpl) + : _rsimpl(rsimpl) {} + + brpc::RedisCommandHandler::Result Run(const char* args, + brpc::RedisReply* output) { + std::string key; + bool parse_command = false; + butil::StringSplitter sp(args, ' '); + for (; sp; ++sp) { + if (!parse_command) { + parse_command = true; + } else if (key.empty()) { + key.assign(sp.field(), sp.length()); + } else { + LOG(WARNING) << "unknown args: " << sp; + } Review comment: 我觉得example里不应该用StringSplitter,对于大部分用户不够直观 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
