|
5 | 5 | namespace real_medium::cache::articles_cache { |
6 | 6 |
|
7 | 7 | userver::storages::postgres::Query ArticlesCachePolicy::kQuery = |
8 | | - userver::storages::postgres::Query( |
9 | | - real_medium::sql::kSelectFullArticleInfo.data()); |
| 8 | + userver::storages::postgres::Query(real_medium::sql::kSelectFullArticleInfo.data()); |
10 | 9 |
|
11 | 10 | void ArticlesCacheContainer::insert_or_assign(Key&& key, Article&& article) { |
12 | | - auto articlePtr = std::make_shared<const Article>(std::move(article)); |
13 | | - auto oldValue = articleByKey_.find(key); |
14 | | - if (oldValue != articleByKey_.end()) { |
15 | | - articleBySlug_.erase(oldValue->second->slug); |
16 | | - for (const auto& oldFollower : oldValue->second->authorFollowedByUsersIds) |
17 | | - if (articlePtr->authorFollowedByUsersIds.find(oldFollower) == |
18 | | - articlePtr->authorFollowedByUsersIds.end()) |
19 | | - articlesByFollower_[oldFollower].erase(articlePtr->articleId); |
20 | | - } |
| 11 | + auto articlePtr = std::make_shared<const Article>(std::move(article)); |
| 12 | + auto oldValue = articleByKey_.find(key); |
| 13 | + if (oldValue != articleByKey_.end()) { |
| 14 | + articleBySlug_.erase(oldValue->second->slug); |
| 15 | + for (const auto& oldFollower : oldValue->second->authorFollowedByUsersIds) |
| 16 | + if (articlePtr->authorFollowedByUsersIds.find(oldFollower) == articlePtr->authorFollowedByUsersIds.end()) |
| 17 | + articlesByFollower_[oldFollower].erase(articlePtr->articleId); |
| 18 | + } |
21 | 19 |
|
22 | | - articleByKey_.insert_or_assign(key, articlePtr); |
23 | | - articleBySlug_.insert_or_assign(articlePtr->slug, articlePtr); |
24 | | - for (const auto& follower : articlePtr->authorFollowedByUsersIds) |
25 | | - articlesByFollower_[follower].insert_or_assign(articlePtr->articleId, |
26 | | - articlePtr); |
27 | | - recentArticles_.insert_or_assign( |
28 | | - {articlePtr->createdAt, articlePtr->articleId}, articlePtr); |
| 20 | + articleByKey_.insert_or_assign(key, articlePtr); |
| 21 | + articleBySlug_.insert_or_assign(articlePtr->slug, articlePtr); |
| 22 | + for (const auto& follower : articlePtr->authorFollowedByUsersIds) |
| 23 | + articlesByFollower_[follower].insert_or_assign(articlePtr->articleId, articlePtr); |
| 24 | + recentArticles_.insert_or_assign({articlePtr->createdAt, articlePtr->articleId}, articlePtr); |
29 | 25 | } |
30 | 26 |
|
31 | 27 | size_t ArticlesCacheContainer::size() const { return articleByKey_.size(); } |
32 | 28 |
|
33 | | -ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug( |
34 | | - const Slug& slug) const { |
35 | | - auto it = articleBySlug_.find(slug); |
36 | | - if (it == articleBySlug_.end()) return nullptr; |
37 | | - return it->second; |
| 29 | +ArticlesCacheContainer::ArticlePtr ArticlesCacheContainer::findArticleBySlug(const Slug& slug) const { |
| 30 | + auto it = articleBySlug_.find(slug); |
| 31 | + if (it == articleBySlug_.end()) return nullptr; |
| 32 | + return it->second; |
38 | 33 | } |
39 | 34 |
|
40 | | -std::vector<ArticlesCacheContainer::ArticlePtr> |
41 | | -ArticlesCacheContainer::getRecent( |
42 | | - real_medium::handlers::ArticleFilterDTO& filter) const { |
43 | | - std::vector<ArticlePtr> articles; |
44 | | - int offset = 0; |
45 | | - for (const auto& it : recentArticles_) { |
46 | | - if (filter.limit && |
47 | | - articles.size() >= |
48 | | - userver::utils::numeric_cast<std::size_t>(filter.limit)) |
49 | | - break; |
| 35 | +std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getRecent( |
| 36 | + real_medium::handlers::ArticleFilterDTO& filter |
| 37 | +) const { |
| 38 | + std::vector<ArticlePtr> articles; |
| 39 | + int offset = 0; |
| 40 | + for (const auto& it : recentArticles_) { |
| 41 | + if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break; |
50 | 42 |
|
51 | | - const auto& tags = it.second->tags; |
52 | | - if (filter.tag && it.second->tags.find(filter.tag.value()) == tags.end()) |
53 | | - continue; |
| 43 | + const auto& tags = it.second->tags; |
| 44 | + if (filter.tag && it.second->tags.find(filter.tag.value()) == tags.end()) continue; |
54 | 45 |
|
55 | | - if (filter.author && it.second->authorInfo.username != filter.author) |
56 | | - continue; |
| 46 | + if (filter.author && it.second->authorInfo.username != filter.author) continue; |
57 | 47 |
|
58 | | - const auto& favorited = it.second->articleFavoritedByUsernames; |
59 | | - if (filter.favorited && |
60 | | - favorited.find(filter.favorited.value()) == favorited.end()) |
61 | | - continue; |
| 48 | + const auto& favorited = it.second->articleFavoritedByUsernames; |
| 49 | + if (filter.favorited && favorited.find(filter.favorited.value()) == favorited.end()) continue; |
62 | 50 |
|
63 | | - if (filter.offset && offset < filter.offset) { |
64 | | - ++offset; |
65 | | - continue; |
| 51 | + if (filter.offset && offset < filter.offset) { |
| 52 | + ++offset; |
| 53 | + continue; |
| 54 | + } |
| 55 | + articles.push_back(it.second); |
66 | 56 | } |
67 | | - articles.push_back(it.second); |
68 | | - } |
69 | | - return articles; |
| 57 | + return articles; |
70 | 58 | } |
71 | | -std::vector<ArticlesCacheContainer::ArticlePtr> ArticlesCacheContainer::getFeed( |
72 | | - real_medium::handlers::FeedArticleFilterDTO& filter, UserId authId) const { |
73 | | - auto followedArticlesUMap = articlesByFollower_.find(authId); |
74 | | - if (followedArticlesUMap == articlesByFollower_.end()) return {}; |
| 59 | +std::vector<ArticlesCacheContainer::ArticlePtr> |
| 60 | +ArticlesCacheContainer::getFeed(real_medium::handlers::FeedArticleFilterDTO& filter, UserId authId) const { |
| 61 | + auto followedArticlesUMap = articlesByFollower_.find(authId); |
| 62 | + if (followedArticlesUMap == articlesByFollower_.end()) return {}; |
75 | 63 |
|
76 | | - RecentArticlesMap followedArticlesOrdered; |
77 | | - for (const auto& it : followedArticlesUMap->second) |
78 | | - followedArticlesOrdered.insert_or_assign( |
79 | | - {it.second->createdAt, it.second->articleId}, it.second); |
| 64 | + RecentArticlesMap followedArticlesOrdered; |
| 65 | + for (const auto& it : followedArticlesUMap->second) |
| 66 | + followedArticlesOrdered.insert_or_assign({it.second->createdAt, it.second->articleId}, it.second); |
80 | 67 |
|
81 | | - std::vector<ArticlePtr> articles; |
82 | | - ; |
83 | | - int offset = 0; |
84 | | - for (const auto& it : followedArticlesOrdered) { |
85 | | - if (filter.limit && |
86 | | - articles.size() >= |
87 | | - userver::utils::numeric_cast<std::size_t>(filter.limit)) |
88 | | - break; |
89 | | - if (filter.offset && offset < filter.offset) { |
90 | | - ++offset; |
91 | | - continue; |
| 68 | + std::vector<ArticlePtr> articles; |
| 69 | + ; |
| 70 | + int offset = 0; |
| 71 | + for (const auto& it : followedArticlesOrdered) { |
| 72 | + if (filter.limit && articles.size() >= userver::utils::numeric_cast<std::size_t>(filter.limit)) break; |
| 73 | + if (filter.offset && offset < filter.offset) { |
| 74 | + ++offset; |
| 75 | + continue; |
| 76 | + } |
| 77 | + articles.push_back(it.second); |
92 | 78 | } |
93 | | - articles.push_back(it.second); |
94 | | - } |
95 | | - return articles; |
| 79 | + return articles; |
96 | 80 | } |
97 | 81 |
|
98 | 82 | } // namespace real_medium::cache::articles_cache |
0 commit comments