userver: userver/fs/fs_cache_client.hpp Source File
Loading...
Searching...
No Matches
fs_cache_client.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <chrono>
7#include <cstddef>
8#include <string>
9#include <string_view>
10
13#include <userver/fs/read.hpp>
15#include <userver/utils/impl/transparent_hash.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace fs {
21
22namespace impl {
23
24struct FsCacheRcuMapTraits : public rcu::DefaultRcuMapTraits<std::string> {
25 using Hash = utils::impl::TransparentHash<std::string>;
26 using KeyEqual = std::equal_to<>;
27};
28
29} // namespace impl
30
32inline constexpr std::size_t kDefaultMaxSizeToCache = 1024ULL * 1024 * 1024;
33
38class FsCacheClient final {
39public:
41 struct Settings final {
43 std::string dir;
44
46 std::chrono::milliseconds update_period{0};
47
49 engine::TaskProcessor& task_processor;
50
53 };
54
56 explicit FsCacheClient(const Settings& settings);
57
62 FileInfoWithDataConstPtr TryGetFile(std::string_view path) const;
63
66
67private:
68#ifdef __linux__
69 void InotifyWork();
70
71 void HandleDelete(const std::string& path);
72
73 static void HandleDeleteDirectory(engine::io::sys_linux::Inotify& inotify, const std::string& path);
74
75 void HandleCreate(const std::string& path);
76
77 void HandleCreateDirectory(engine::io::sys_linux::Inotify& inotify, const std::string& path);
78
79 void HandleCreateDirectoryBlocking(engine::io::sys_linux::Inotify& inotify, const std::string& path);
80#endif
81
82 const std::string dir_;
83 const std::chrono::milliseconds update_period_;
84 const std::size_t max_size_to_cache_;
85 engine::TaskProcessor& tp_;
86#ifndef __linux__
87 utils::PeriodicTask cache_updater_;
88#endif
90
91#ifdef __linux__
92 engine::Task inotify_task_;
93#endif
94};
95
96} // namespace fs
97
98USERVER_NAMESPACE_END