diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index dbf9f4f..7d2ba45 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -184,9 +184,6 @@ Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_te , tqueue_id_(tqueue_id) , parameters_(std::move(parameters)) , stat_actor_(std::move(stat_actor)) { - messages_lru_root_.lru_next = &messages_lru_root_; - messages_lru_root_.lru_prev = &messages_lru_root_; - static auto is_inited = init_methods(); CHECK(is_inited); } @@ -3505,7 +3502,6 @@ ServerBotInfo Client::get_bot_info() const { void Client::start_up() { start_time_ = td::Time::now(); next_bot_updates_warning_time_ = start_time_ + 600; - schedule_next_delete_messages_lru(); webhook_set_time_ = start_time_; next_allowed_set_webhook_time_ = start_time_; next_set_webhook_logging_time_ = start_time_; @@ -9462,58 +9458,12 @@ void Client::delete_message(int64 chat_id, int64 message_id, bool only_from_cach } auto message_info = it->second.get(); - CHECK(message_info->lru_next != nullptr); - message_info->lru_next->lru_prev = message_info->lru_prev; - message_info->lru_prev->lru_next = message_info->lru_next; set_message_reply_to_message_id(message_info, 0); messages_.erase(it); } -void Client::schedule_next_delete_messages_lru() { - CHECK(!next_delete_messages_lru_timeout_.has_timeout()); - next_delete_messages_lru_timeout_.set_callback(Client::delete_messages_lru); - next_delete_messages_lru_timeout_.set_callback_data(static_cast(this)); - next_delete_messages_lru_timeout_.set_timeout_in(td::Random::fast(MESSAGES_CACHE_TIME, 2 * MESSAGES_CACHE_TIME)); -} - -void Client::delete_messages_lru(void *client_void) { - CHECK(client_void != nullptr); - auto client = static_cast(client_void); - - auto now = td::Time::now(); - int32 deleted_message_count = 0; - while (client->messages_lru_root_.lru_next->access_time < now - MESSAGES_CACHE_TIME) { - auto message = client->messages_lru_root_.lru_next; - if (client->yet_unsent_reply_message_ids_.count({message->chat_id, message->id})) { - LOG(DEBUG) << "Force usage of message " << message->id << " in " << message->chat_id; - client->update_message_lru(message); - } else { - client->delete_message(message->chat_id, message->id, true); - deleted_message_count++; - } - } - - if (deleted_message_count != 0) { - LOG(DEBUG) << "Delete " << deleted_message_count << " messages from cache"; - } - client->schedule_next_delete_messages_lru(); -} - -void Client::update_message_lru(const MessageInfo *message_info) const { - message_info->access_time = td::Time::now(); - if (message_info->lru_next != nullptr) { - message_info->lru_next->lru_prev = message_info->lru_prev; - message_info->lru_prev->lru_next = message_info->lru_next; - } - auto prev = messages_lru_root_.lru_prev; - message_info->lru_prev = prev; - prev->lru_next = message_info; - message_info->lru_next = &messages_lru_root_; - messages_lru_root_.lru_prev = message_info; -} - Client::FullMessageId Client::add_message(object_ptr &&message, bool force_update_content) { CHECK(message != nullptr); CHECK(message->sending_state_ == nullptr); @@ -9530,8 +9480,6 @@ Client::FullMessageId Client::add_message(object_ptr &&message, message_info = std::move(it->second); } - update_message_lru(message_info.get()); - message_info->id = message_id; message_info->chat_id = chat_id; message_info->date = message->date_; @@ -9673,9 +9621,7 @@ const Client::MessageInfo *Client::get_message(int64 chat_id, int64 message_id) } LOG(DEBUG) << "Found message " << message_id << " from chat " << chat_id; - auto result = it->second.get(); - update_message_lru(result); - return result; + return it->second.get(); } Client::MessageInfo *Client::get_message_editable(int64 chat_id, int64 message_id) { @@ -9686,9 +9632,7 @@ Client::MessageInfo *Client::get_message_editable(int64 chat_id, int64 message_i } LOG(DEBUG) << "Found message " << message_id << " from chat " << chat_id; - auto result = it->second.get(); - update_message_lru(result); - return result; + return it->second.get(); } td::string Client::get_chat_member_status(const object_ptr &status) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 7468ab0..6cd452f 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -15,7 +15,6 @@ #include "td/actor/actor.h" #include "td/actor/PromiseFuture.h" #include "td/actor/SignalSlot.h" -#include "td/actor/Timeout.h" #include "td/utils/common.h" #include "td/utils/Container.h" @@ -65,8 +64,6 @@ class Client : public WebhookActor::Callback { static constexpr int32 MAX_CONCURRENTLY_SENT_CHAT_MESSAGES = 1000; // some unreasonably big value - static constexpr int32 MESSAGES_CACHE_TIME = 3600; - static constexpr std::size_t MIN_PENDING_UPDATES_WARNING = 200; static constexpr int64 GREAT_MINDS_SET_ID = 1842540969984001; @@ -657,10 +654,6 @@ class Client : public WebhookActor::Callback { td::string get_chat_description(int64 chat_id) const; struct MessageInfo { - mutable double access_time = 1e20; - mutable const MessageInfo *lru_next = nullptr; - mutable const MessageInfo *lru_prev = nullptr; - int64 id = 0; int64 sender_user_id = 0; int64 sender_chat_id = 0; @@ -722,9 +715,6 @@ class Client : public WebhookActor::Callback { void remove_replies_to_message(int64 chat_id, int64 reply_to_message_id, bool only_from_cache); void delete_message(int64 chat_id, int64 message_id, bool only_from_cache); - static void delete_messages_lru(void *client_void); - void schedule_next_delete_messages_lru(); - void update_message_lru(const MessageInfo *message_info) const; void add_new_message(object_ptr &&message, bool is_edited); void process_new_message_queue(int64 chat_id); @@ -871,7 +861,6 @@ class Client : public WebhookActor::Callback { static std::unordered_map methods_; - MessageInfo messages_lru_root_; std::unordered_map, FullMessageIdHash> messages_; // message cache std::unordered_map users_; // user info cache std::unordered_map groups_; // group info cache @@ -883,8 +872,6 @@ class Client : public WebhookActor::Callback { std::unordered_map, FullMessageIdHash> yet_unsent_reply_message_ids_; // message -> replies to it - td::Timeout next_delete_messages_lru_timeout_; - std::unordered_map> file_download_listeners_; std::unordered_set download_started_file_ids_;