clean seafile api
This commit is contained in:
1 parent
133424f5f8
commit
3e4694ceb5
14 files changed
+476
-290
No files matched your search
@@ -7,13 +7,11 @@
|
||||
#include "seafile_api_interface.hpp"
|
||||
#include "seafile/cache/cache.hpp"
|
||||
|
||||
namespace seafile::api
|
||||
{
|
||||
class CachedSeafileApi final : public ISeaFileApi
|
||||
{
|
||||
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, std::vector<RepoEntry> > repo_entries_cache;
|
||||
mutable cache::Cache<std::string, AccountInfo> account_info_cache;
|
||||
|
||||
std::shared_ptr<ISeaFileApi> underlayer_api;
|
||||
@@ -22,13 +20,11 @@ namespace seafile::api
|
||||
static std::string makeKey(
|
||||
std::string_view prefix,
|
||||
std::initializer_list<std::string_view> args
|
||||
)
|
||||
{
|
||||
) {
|
||||
std::string key(prefix);
|
||||
|
||||
for (auto& arg : args)
|
||||
{
|
||||
key += ":";
|
||||
for (auto &arg: args) {
|
||||
key += ':';
|
||||
key += arg;
|
||||
}
|
||||
|
||||
@@ -36,66 +32,46 @@ namespace seafile::api
|
||||
}
|
||||
|
||||
public:
|
||||
explicit CachedSeafileApi(std::shared_ptr<ISeaFileApi> api) : underlayer_api(std::move(api))
|
||||
{
|
||||
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);
|
||||
auth(
|
||||
const std::string &username,
|
||||
const std::string &password
|
||||
) override {
|
||||
return underlayer_api->auth(username, password);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const override
|
||||
{
|
||||
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);
|
||||
defaultRepo() const override {
|
||||
return underlayer_api->defaultRepo();
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
repoInfo(const std::string &repo_id) const override {
|
||||
return underlayer_api->repoInfo(repo_id);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
repoDir(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("repoDir", {repo_id, path});
|
||||
|
||||
if (const auto value = repo_entries_cache.get(key))
|
||||
{
|
||||
if (const auto value = repo_entries_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->repoDir(repo_id, path, token, type);
|
||||
if (result)
|
||||
{
|
||||
auto result = underlayer_api->repoDir(repo_id, path);
|
||||
if (result) {
|
||||
repo_entries_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
return result;
|
||||
@@ -103,43 +79,22 @@ namespace seafile::api
|
||||
|
||||
|
||||
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;
|
||||
accountInfo() const override {
|
||||
return underlayer_api->accountInfo();
|
||||
}
|
||||
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
fileDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("file", {repo_id, path});
|
||||
|
||||
if (const auto value = file_info_cache.get(key))
|
||||
{
|
||||
if (const auto value = file_info_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->fileDetail(repo_id, path, token, type);
|
||||
auto result = underlayer_api->fileDetail(repo_id, path);
|
||||
|
||||
if (result)
|
||||
{
|
||||
if (result) {
|
||||
file_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
@@ -149,55 +104,39 @@ namespace seafile::api
|
||||
|
||||
|
||||
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);
|
||||
downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
|
||||
return underlayer_api->downloadFileUrl(repo_id, path);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
downloadFile(const std::string &repo_id, const std::string &path, std::optional<size_t> start,
|
||||
std::optional<size_t> end) const override {
|
||||
return underlayer_api->downloadFile(repo_id, path, start, end);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<DirInfo, SeafileException>
|
||||
dirDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType type
|
||||
) const override
|
||||
{
|
||||
dirDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("dir", {repo_id, path});
|
||||
|
||||
if (auto value = dir_info_cache.get(key))
|
||||
{
|
||||
if (auto value = dir_info_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->dirDetail(repo_id, path, token, type);
|
||||
auto result = underlayer_api->dirDetail(repo_id, path);
|
||||
|
||||
if (result)
|
||||
{
|
||||
if (result) {
|
||||
dir_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
tl::expected<std::vector<RepoInfo>, SeafileException> repos(const std::string &type,
|
||||
const std::string &name_contains) const override {
|
||||
return underlayer_api->repos(type, name_contains);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif //SEADISK_CACHED_SEAFILE_API_HPP
|
||||
Reference in new issue
Block a user