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 @copybref fs::FsCacheClient
5
6#include <userver/engine/io/sys/posix/inotify.hpp>
7#include <userver/fs/read.hpp>
8#include <userver/rcu/rcu_map.hpp>
9#include <userver/utils/periodic_task.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace fs {
14
15/// @ingroup userver_clients
16///
17/// @brief Class client for storing files in memory
18/// Usually retrieved from `components::FsCache`
19class FsCacheClient final {
20 public:
21 /// @brief Fills the cache and starts periodic update
22 /// @param dir directory to cache files from
23 /// @param update_period time (0 - fill the cache only at startup), not used
24 /// in Linux
25 /// @param tp task processor to do filesystem operations
26 FsCacheClient(std::string_view dir, std::chrono::milliseconds update_period,
27 engine::TaskProcessor& tp);
28
29 /// @brief get file from memory
30 /// @param path to file
31 /// @return file info and content ; `nullptr` if no file with specified name
32 /// on FS
33 FileInfoWithDataConstPtr TryGetFile(std::string_view path) const;
34
35 /// @brief Concurrency-safe cache update
37
38 private:
39#ifdef __linux__
40 void InotifyWork();
41
42 void HandleDelete(const std::string& path);
43
44 static void HandleDeleteDirectory(engine::io::sys::posix::Inotify& inotify,
45 const std::string& path);
46
47 void HandleCreate(const std::string& path);
48
49 void HandleCreateDirectory(engine::io::sys::posix::Inotify& inotify,
50 const std::string& path);
51#endif
52
53 const std::string dir_;
54 const std::chrono::milliseconds update_period_;
55 engine::TaskProcessor& tp_;
56#ifndef __linux__
57 utils::PeriodicTask cache_updater_;
58#endif
59 rcu::RcuMap<std::string, const fs::FileInfoWithData> data_;
60
61#ifdef __linux__
62 engine::Task inotify_task_;
63#endif
64};
65
66} // namespace fs
67
68USERVER_NAMESPACE_END