2
0
mirror of https://github.com/boostorg/redis.git synced 2026-01-19 04:42:09 +00:00

Improvements in the examples.

This commit is contained in:
Marcelo Zimbres
2022-01-07 11:56:54 +01:00
parent cba2acdf70
commit e4cf6ac3e6
14 changed files with 102 additions and 77 deletions

View File

@@ -1512,7 +1512,7 @@ ECLIPSE_DOC_ID = org.doxygen.Project
# The default value is: NO.
# This tag requires that the tag GENERATE_HTML is set to YES.
DISABLE_INDEX = NO
DISABLE_INDEX = YES
# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index
# structure should be generated to display hierarchical information. If the tag

View File

@@ -1,5 +1,7 @@
#include <aedis/aedis.hpp>
#include <iostream>
#include <aedis/aedis.hpp>
#include <aedis/resp3/client_base.hpp>
#include "user_session.hpp"
namespace net = aedis::net;
@@ -10,8 +12,12 @@ class my_redis_client : public client_base_type {
private:
void on_event(response_id id) override
{
// If the user connections is still alive when the response
// arrives we send the echo message to the user, otherwise we
// just log it has expired.
if (auto session = id.session.lock()) {
session->on_event(id);
session->deliver(*id.resp);
id.resp->clear();
} else {
std::cout << "Session expired." << std::endl;
}
@@ -28,16 +34,19 @@ net::awaitable<void> listener()
auto ex = co_await net::this_coro::executor;
tcp_acceptor acceptor(ex, {net::ip::tcp::v4(), 55555});
auto rclient = std::make_shared<my_redis_client>(ex);
rclient->start();
// The redis client instance.
auto client = std::make_shared<my_redis_client>(ex);
client->start();
std::string resp; // TODO: Use a shared_ptr.
// The response is shared by all connections.
auto resp = std::make_shared<std::string>();
// Loops accepting connections.
for (;;) {
auto socket = co_await acceptor.async_accept();
auto session = std::make_shared<user_session>(std::move(socket), rclient);
auto session = std::make_shared<user_session>(std::move(socket), client);
response_id id{command::ping, &resp, session};
response_id id{command::ping, resp, session};
auto filler = [id](auto& req, auto const& msg)
{ req.push(id, msg); };
@@ -49,17 +58,11 @@ int main()
{
try {
net::io_context io_context(1);
net::signal_set signals(io_context, SIGINT, SIGTERM);
signals.async_wait([&](auto, auto){ io_context.stop(); });
co_spawn(io_context, listener(), net::detached);
io_context.run();
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
/// \example echo_server.cpp

View File

@@ -85,5 +85,3 @@ int main()
co_spawn(ioc, containers(), net::detached);
ioc.run();
}
/// \example hashes.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres@gmail.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -73,5 +73,3 @@ int main()
co_spawn(ioc, ping(), net::detached);
ioc.run();
}
/// \example intro.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres@gmail.com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -83,5 +83,3 @@ int main()
co_spawn(ioc, key_expiration(), net::detached);
ioc.run();
}
/// \example key_expiration.cpp

View File

@@ -92,5 +92,3 @@ int main()
co_spawn(ioc, ping(), net::detached);
ioc.run();
}
/// \example lists.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -60,5 +60,3 @@ int main()
co_spawn(ioc, nested_response(), net::detached);
ioc.run();
}
/// \example nested_response.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -81,5 +81,3 @@ int main()
co_spawn(ioc, adapter_example(), net::detached);
ioc.run();
}
/// \example response_adapter.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/// \example basic1.cpp
*
* This Source Code Form is subject to the terms of the Mozilla Public
@@ -77,5 +77,3 @@ int main()
co_spawn(ioc, ping(), net::detached);
ioc.run();
}
/// \example response_queue.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -112,5 +112,3 @@ int main()
co_spawn(ioc, serialization(), net::detached);
ioc.run();
}
/// \example serialization.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -82,5 +82,3 @@ int main()
co_spawn(ioc, containers(), net::detached);
ioc.run();
}
/// \example sets.cpp

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com)
/* Copyright (c) 2019 Marcelo Zimbres Silva (mzimbres at gmail dot com)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -77,5 +77,3 @@ int main()
co_spawn(ioc, subscriber(), net::detached);
ioc.run();
}
/// \example subscriber.cpp

View File

@@ -12,15 +12,15 @@ using aedis::resp3::client_base;
struct user_session_base;
// struct to hold information that we need when the response to a
// command is received. See client_base.hpp for more details on the
// Holds the information that is needed when a response to a
// request arrives. See client_base.hpp for more details on the
// required fields in this struct.
struct response_id {
// The redis command that was send in the request.
// The redis command that corresponds to this command.
command cmd = command::unknown;
// Pointer to the response.
std::string* resp;
std::shared_ptr<std::string> resp;
// The pointer to the session the request belongs to.
std::weak_ptr<user_session_base> session =
@@ -30,7 +30,7 @@ struct response_id {
// Base class for user sessions.
struct user_session_base {
virtual ~user_session_base() {}
virtual void on_event(response_id id) = 0;
virtual void deliver(std::string const& msg) = 0;
};
using client_base_type = client_base<response_id>;
@@ -61,10 +61,9 @@ public:
net::detached);
}
void on_event(response_id id) override
void deliver(std::string const& msg) override
{
write_msgs_.push_back(*id.resp);
id.resp->clear();
write_msgs_.push_back(msg);
timer_.cancel_one();
}

View File

@@ -10,23 +10,14 @@
#include <aedis/resp3/read.hpp>
#include <aedis/resp3/write.hpp>
#include <aedis/resp3/serializer.hpp>
#include <aedis/resp3/client_base.hpp>
/** \mainpage
\tableofcontents
\section reference Reference
- \subpage enums
- \subpage classes
- \subpage operators
- \subpage read_write_ops
- \subpage functions
\section documentation Documentation
- \subpage usage
- \subpage tutorial
- \subpage reference
- \subpage examples
\section overview Overview
@@ -40,34 +31,86 @@
- First class async support with ASIO.
*/
/** \page usage Usage
//---------------------------------------------------------
// Pages
/** \page examples Examples
Aedis is a header only library. You only need to include the header
\b Basics
@code
#include <aedis/src.hpp>
@endcode
- intro.cpp
in one of your source files.
Illustrates the basic usage.
- key_expiration.cpp
Shows how to use \c std::optional to deal with keys that may
have expired or do not exist.
- nested_response.cpp
When the data-structure returned by Redis cannot be translated
in a C++ built-in type like STL containers, std::string, etc.
- subscriber.cpp
Shows how channel subscription works.
- response_queue.cpp
Shows how to process responses asynchronously.
\b STL \b Containers: Many of the Redis data structures can be directly translated in to STL containers. The example bellow shows how to do that. The list of Redis data types can be found at https://redis.io/topics/data-types.
- hashes.cpp
Shows how to read Redis hashes in a \c std::map, \c std::unordered_map and \c std::vector.
- lists.cpp
Shows how to read Redis lists in \c std::list, \c std::deque, \c std::vector. It also illustrates basic serialization.
- sets.cpp
Shows how to read Redis sets in a \c std::set, \c std::unordered_set and \c std::vector.
\b Customization \b points
- serialization.cpp
Shows how to de/serialize your own (simple) data-structures.
- response_adapter.cpp
Customization point for users that want to de/serialize their
own containers.
\b Adavanced: The main difference the examples bellow and the
others above is that they user long lasting connections to Redis.
This is the desired way to communicate with redis.
- echo_server.cpp
Shows the basic principles behind async communication with a database in a tcp server. In this case, the server is a proxy between the user and the database.
*/
/** \page tutorial Tutorial
/** \page reference Reference
*
* Aedis source code reference.
Some info
\section requests Requests
Some text
\section responses Responses
Some text
- \subpage enums
- \subpage classes
- \subpage operators
- \subpage read_write_ops
- \subpage functions
*/
/** \file aedis.hpp
* \brief Includes all headers that are necessary in order to use aedis.
*/
//---------------------------------------------------------
// Groups
/** \defgroup enums Enums
* \brief Enums defined by this library.