Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RespCommand

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use RespCommand". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use RespCommand.

Order

This paper mainly studies RespCommand.

RespCommand

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/RespCommand.java

@ FunctionalInterfacepublic interface RespCommand {RedisToken execute (Request request);}

RespCommand defines the execute method, which receives the Request parameter and returns RedisToken

Request

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/Request.java

Public interface Request {String getCommand (); ImmutableArray getParams (); SafeString getParam (int I); Option getOptionalParam (int I); int getLength (); boolean isEmpty (); Session getSession (); ServerContext getServerContext (); boolean isExit ();}

Request interface defines getCommand, getParams, getParam, getOptionalParam, getLength, isEmpty, getSession, getServerContext, isExit methods

DefaultRequest

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/DefaultRequest.java

Public class DefaultRequest implements Request {private final SafeString command; private final ImmutableArray params; private final Session session; private final ServerContext server; public DefaultRequest (ServerContext server, Session session, SafeString command, ImmutableArray params) {this.server = server; this.session = session; this.command = requireNonNull (command); this.params = requireNonNull (params);} @ Override public String getCommand () {return command.toString ();} @ Override public ImmutableArray getParams () {return params } @ Override public SafeString getParam (int I) {if (I)

< params.size()) { return params.get(i); } return null; } @Override public Option getOptionalParam(int i) { return Option.of(() ->

GetParam (I));} @ Override public int getLength () {return params.size ();} @ Override public boolean isEmpty () {return params.isEmpty ();} @ Override public boolean isExit () {return command.toString () .equalsIgnoreCase ("quit");} @ Override public Session getSession () {return session;} @ Override public ServerContext getServerContext () {return server } @ Override public String toString () {return command + "[" + params.size () + "]:" + params;}}

DefaultRequest implements the Request interface, which defines command, params, session, and server attributes, all of which are passed in to the constructor

CommandSuite

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/CommandSuite.java

Public class CommandSuite {private static final Logger LOGGER = LoggerFactory.getLogger (CommandSuite.class); private final Map clazz = command.getClass (); Command annotation = clazz.getAnnotation (Command.class); if (annotation! = null) {commands.put (annotation.value (), factory.wrap (command)); metadata.put (annotation.value (), clazz);} else {LOGGER.warn ("annotation not present at {}", clazz.getName ()) } private Class getMetadata (String name) {return metadata.getOrDefault (name.toLowerCase (), Void.class);}}

The constructor of CommandSuite adds four command;addCommand methods, PingCommand, EchoCommand, QuitCommand and TimeCommand, to put the command of factory.wrap (command) into commands.

CommandWrapperFactory

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/CommandWrapperFactory.java

Public interface CommandWrapperFactory {RespCommand wrap (Object command);}

CommandWrapperFactory defines the wrap method, which wraps command as RespCommand

DefaultCommandWrapperFactory

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/DefaultCommandWrapperFactory.java

Public class DefaultCommandWrapperFactory implements CommandWrapperFactory {@ Override public RespCommand wrap (Object command) {if (command instanceof RespCommand) {return new CommandWrapper ((RespCommand) command);} throw new IllegalArgumentException ("must implements command interface");}}

DefaultCommandWrapperFactory implements the CommandWrapperFactory interface, and its wrap method wraps RespCommand with CommandWrapper

CommandWrapper

Resp-server-0.16.0/src/main/java/com/github/tonivade/resp/command/CommandWrapper.java

Public class CommandWrapper implements RespCommand {private int params; private final RespCommand command; public CommandWrapper (RespCommand command) {this.command = command; ParamLength length = command.getClass () .getAnnotation (ParamLength.class); if (length! = null) {this.params = length.value () } @ Override public RedisToken execute (Request request) {if (request.getLength () < params) {return error ("ERR wrong number of arguments for'" + request.getCommand () + "'command");} return command.execute (request);}}

CommandWrapper reads ParamLength annotations, and if so, assigns the value value of the annotations to params, which is used by the execute method to verify the parameter length of request.

Summary

RespCommand defines the execute method, receives Request parameters, and the constructor that returns RedisToken;CommandSuite adds four command;addCommand methods, PingCommand, EchoCommand, QuitCommand, and TimeCommand, to put the command of factory.wrap (command) into commands; DefaultCommandWrapperFactory implements the CommandWrapperFactory interface, and its wrap method uses CommandWrapper to wrap RespCommand;CommandWrapper to read ParamLength annotations, and if it reads, assigns the value value of the annotations to params, which is used to verify the length of request parameters by execute method.

At this point, I believe that you have a deeper understanding of "the use of RespCommand", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report