添加文件缓存
This commit is contained in:
1 parent
fb63205a70
commit
95b68b9542
18 files changed
+481
-107
No files matched your search
@@ -0,0 +1,203 @@
|
||||
//
|
||||
// Created by Shell on 2026-07-14.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_CACHED_SEAFILE_API_HPP
|
||||
#define SEADISK_CACHED_SEAFILE_API_HPP
|
||||
#include "seafile_api_interface.hpp"
|
||||
#include "seafile/cache/cache.hpp"
|
||||
|
||||
namespace seafile::api
|
||||
{
|
||||
class CachedSeafileApi final : public ISeaFileApi
|
||||
{
|
||||
mutable cache::Cache<std::string, FileInfo> file_info_cache;
|
||||
mutable cache::Cache<std::string, DirInfo> dir_info_cache;
|
||||
mutable cache::Cache<std::string, std::vector<RepoEntry>> repo_entries_cache;
|
||||
mutable cache::Cache<std::string, AccountInfo> account_info_cache;
|
||||
|
||||
std::shared_ptr<ISeaFileApi> underlayer_api;
|
||||
std::chrono::seconds default_ttl = std::chrono::seconds(300);
|
||||
|
||||
static std::string makeKey(
|
||||
std::string_view prefix,
|
||||
std::initializer_list<std::string_view> args
|
||||
)
|
||||
{
|
||||
std::string key(prefix);
|
||||
|
||||
for (auto& arg : args)
|
||||
{
|
||||
key += ":";
|
||||
key += arg;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit CachedSeafileApi(std::shared_ptr<ISeaFileApi> api) : underlayer_api(std::move(api))
|
||||
{
|
||||
};
|
||||
|
||||
tl::expected<AuthToken, SeafileException>
|
||||
authToken(
|
||||
const std::string& username,
|
||||
const std::string& password
|
||||
) override
|
||||
{
|
||||
return underlayer_api->authToken(username, password);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const override
|
||||
{
|
||||
return underlayer_api->serverInfo();
|
||||
}
|
||||
|
||||
|
||||
tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo(
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
return underlayer_api->defaultRepo(token, type);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(
|
||||
const std::string& repo_id,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
return underlayer_api->repoInfo(repo_id, token, type);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
const auto key = makeKey("repoDir", {repo_id, path});
|
||||
|
||||
if (const auto value = repo_entries_cache.get(key))
|
||||
{
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->repoDir(repo_id, path, token, type);
|
||||
if (result)
|
||||
{
|
||||
repo_entries_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo(
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
if (const auto info = account_info_cache.get(token); info.has_value())
|
||||
{
|
||||
return *info;
|
||||
}
|
||||
const auto result = underlayer_api->accountInfo(token, type);
|
||||
if (result)
|
||||
{
|
||||
account_info_cache.put(token, result.value(), default_ttl);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
const auto key = makeKey("file", {repo_id, path});
|
||||
|
||||
if (const auto value = file_info_cache.get(key))
|
||||
{
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->fileDetail(repo_id, path, token, type);
|
||||
|
||||
if (result)
|
||||
{
|
||||
file_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
return underlayer_api->downloadFileUrl(repo_id, path, token, type);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFile(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
std::optional<size_t> start,
|
||||
std::optional<size_t> end,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
return underlayer_api->downloadFile(repo_id, path, start, end, token, type);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<DirInfo, SeafileException>
|
||||
dirDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
const auto key = makeKey("dir", {repo_id, path});
|
||||
|
||||
if (auto value = dir_info_cache.get(key))
|
||||
{
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->dirDetail(repo_id, path, token, type);
|
||||
|
||||
if (result)
|
||||
{
|
||||
dir_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif //SEADISK_CACHED_SEAFILE_API_HPP
|
||||
Reference in new issue
Block a user