Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
ba7f3d5f
Commit
ba7f3d5f
authored
Dec 05, 2019
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis_server_protocol: update redis_server
parent
5c8794c4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
8 deletions
+34
-8
redis_server.cpp
example/redis_c++/redis_server.cpp
+34
-8
No files found.
example/redis_c++/redis_server.cpp
View file @
ba7f3d5f
...
...
@@ -22,12 +22,13 @@
#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
char
*
value
)
{
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
;
...
...
@@ -59,13 +60,24 @@ public:
GetCommandHandler
(
RedisServiceImpl
*
rsimpl
)
:
_rsimpl
(
rsimpl
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[]
,
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
,
brpc
::
RedisReply
*
output
)
{
if
(
args
[
1
]
==
NULL
)
{
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
;
}
}
if
(
key
.
empty
())
{
output
->
SetError
(
"ERR wrong number of arguments for 'get' command"
);
return
brpc
::
RedisCommandHandler
::
OK
;
}
std
::
string
key
=
args
[
1
];
std
::
string
value
;
if
(
_rsimpl
->
Get
(
key
,
&
value
))
{
output
->
SetBulkString
(
value
);
...
...
@@ -85,14 +97,28 @@ public:
SetCommandHandler
(
RedisServiceImpl
*
rsimpl
)
:
_rsimpl
(
rsimpl
)
{}
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
[]
,
brpc
::
RedisCommandHandler
::
Result
Run
(
const
char
*
args
,
brpc
::
RedisReply
*
output
)
{
if
(
args
[
1
]
==
NULL
||
args
[
2
]
==
NULL
)
{
std
::
string
key
;
std
::
string
value
;
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
if
(
value
.
empty
())
{
value
.
assign
(
sp
.
field
(),
sp
.
length
());
}
else
{
LOG
(
WARNING
)
<<
"unknown args: "
<<
sp
;
}
}
if
(
key
.
empty
()
||
value
.
empty
())
{
output
->
SetError
(
"ERR wrong number of arguments for 'set' command"
);
return
brpc
::
RedisCommandHandler
::
OK
;
}
std
::
string
key
=
args
[
1
];
_rsimpl
->
Set
(
key
,
args
[
2
]);
_rsimpl
->
Set
(
key
,
value
);
output
->
SetStatus
(
"OK"
);
return
brpc
::
RedisCommandHandler
::
OK
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment