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
3/// @file userver/fs/fs_cache_client.hpp
4/// @brief @copybrief fs::FsCacheClient
5
6#include <chrono>
7#include <cstddef>
8#include <string>
9#include <string_view>
10
11#include <userver/engine/io/sys_linux/inotify.hpp>
12#include <userver/engine/task/task_processor_fwd.hpp>
13#include <userver/fs/read.hpp>
14#include <userver/rcu/rcu_map.hpp>
15#include <userver/utils/periodic_task.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace fs {
20
21/// Default maximum file size stored in memory by FsCacheClient (1 GiB).
22inline constexpr std::size_t kDefaultMaxSizeToCache = 1024ULL * 1024 * 1024;
23
24/// @ingroup userver_clients
25///
26/// @brief Class client for storing files in memory
27/// Usually retrieved from `components::FsCache`
28class FsCacheClient final {
29public:
30 /// Configuration parameters for FsCacheClient.
31 struct Settings final {
32 /// Directory to cache files from
33 std::string dir;
34
35 /// Update period (0 - fill the cache only at startup), not used in Linux
36 std::chrono::milliseconds update_period{0};
37
38 /// Task processor to do filesystem operations
39 engine::TaskProcessor& task_processor;
40
41 /// For files larger than this limit, the full path is stored instead of the contents
43 };
44
45 /// @brief Fills the cache and starts periodic update
46 explicit FsCacheClient(const Settings& settings);
47
48 /// @brief get file from memory
49 /// @param path to file
50 /// @return file info and content ; `nullptr` if no file with specified name
51 /// on FS
52 FileInfoWithDataConstPtr TryGetFile(std::string_view path) const;
53
54 /// @brief Concurrency-safe cache update
56
57private:
58#ifdef __linux__
59 void InotifyWork();
60
61 void HandleDelete(const std::string& path);
62
63 static void HandleDeleteDirectory(engine::io::sys_linux::Inotify& inotify, const std::string& path);
64
65 void HandleCreate(const std::string& path);
66
67 void HandleCreateDirectory(engine::io::sys_linux::Inotify& inotify, const std::string& path);
68
69 void HandleCreateDirectoryBlocking(engine::io::sys_linux::Inotify& inotify, const std::string& path);
70#endif
71
72 const std::string dir_;
73 const std::chrono::milliseconds update_period_;
74 const std::size_t max_size_to_cache_;
75 engine::TaskProcessor& tp_;
76#ifndef __linux__
77 utils::PeriodicTask cache_updater_;
78#endif
79 rcu::RcuMap<std::string, const fs::FileInfoWithData> data_;
80
81#ifdef __linux__
82 engine::Task inotify_task_;
83#endif
84};
85
86} // namespace fs
87
88USERVER_NAMESPACE_END