添加文件缓存
This commit is contained in:
1 parent
fb63205a70
commit
95b68b9542
18 files changed
+481
-107
No files matched your search
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
* @brief Seafile user profile information.
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile {
|
||||
namespace seafile::api {
|
||||
struct AuthToken final {
|
||||
std::string token;
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
* default repo response
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#define SEADISK_DIR_INFO_HPP
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
* @brief Repository directory information.
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#define SEADISK_EXCEPTION_HPP
|
||||
#include <stdexcept>
|
||||
|
||||
namespace seafile {
|
||||
namespace seafile::api {
|
||||
class SeafileException : public std::runtime_error {
|
||||
public:
|
||||
explicit SeafileException(const std::string &msg)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
* @brief Detailed file metadata.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
* @brief File or directory entry metadata.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
/**
|
||||
*@brief Seafile repository metadata.
|
||||
|
||||
@@ -20,11 +20,12 @@
|
||||
#include "file_info.hpp"
|
||||
#include "repo_entry.hpp"
|
||||
#include "repo_info.hpp"
|
||||
#include "seafile_api_interface.hpp"
|
||||
#include "server_info.hpp"
|
||||
#include "token_type.hpp"
|
||||
|
||||
namespace seafile {
|
||||
class SeaFileApi final {
|
||||
namespace seafile::api {
|
||||
class SeaFileApi final:public ISeaFileApi {
|
||||
//base url of the seafile api server
|
||||
std::string base_url;
|
||||
|
||||
@@ -104,7 +105,7 @@ namespace seafile {
|
||||
* @return token when success else SeafileException
|
||||
*/
|
||||
tl::expected<AuthToken, SeafileException>
|
||||
authToken(const std::string &username, const std::string &password) {
|
||||
authToken(const std::string &username, const std::string &password) override {
|
||||
const nlohmann::json body = {
|
||||
{"username", username},
|
||||
{"password", password}
|
||||
@@ -113,7 +114,7 @@ namespace seafile {
|
||||
}
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const {
|
||||
serverInfo() const override{
|
||||
const auto response = cpr::Get(
|
||||
cpr::Url{makeUrl("/api2/server-info/")},
|
||||
cpr::Header{
|
||||
@@ -130,7 +131,7 @@ namespace seafile {
|
||||
* @return default repo info
|
||||
*/
|
||||
tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo(const std::string &token, const TokenType token_type) const {
|
||||
defaultRepo(const std::string &token, const TokenType token_type) const override{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = makeUrl("/api2/default-repo/");
|
||||
const auto response = cpr::Get(cpr::Url{url},
|
||||
@@ -142,7 +143,7 @@ namespace seafile {
|
||||
}
|
||||
|
||||
tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(const std::string &repo_id, const std::string &token, const TokenType token_type) const {
|
||||
repoInfo(const std::string &repo_id, const std::string &token, const TokenType token_type) const override{
|
||||
const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id));
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -164,7 +165,7 @@ namespace seafile {
|
||||
*/
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(const std::string &repo_id, const std::string &path, const std::string &token,
|
||||
const TokenType token_type) const {
|
||||
const TokenType token_type) const override{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -181,7 +182,7 @@ namespace seafile {
|
||||
|
||||
tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo(const std::string &token,
|
||||
const TokenType token_type) const {
|
||||
const TokenType token_type) const override{
|
||||
const auto url = cpr::Url{makeUrl("/api2/account/info/")};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -195,7 +196,7 @@ namespace seafile {
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(const std::string &repo_id, const std::string &path, const std::string &token,
|
||||
const TokenType token_type) const {
|
||||
const TokenType token_type) const override{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -211,7 +212,7 @@ namespace seafile {
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(const std::string &repo_id, const std::string &path, const std::string &token,
|
||||
const TokenType token_type) const {
|
||||
const TokenType token_type) const override{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/", repo_id))};
|
||||
const auto response = cpr::Get(url,
|
||||
@@ -231,7 +232,7 @@ namespace seafile {
|
||||
const std::optional<size_t> start,
|
||||
const std::optional<size_t> end,
|
||||
const std::string &token,
|
||||
const TokenType token_type) const {
|
||||
const TokenType token_type) const override{
|
||||
if (auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value()) {
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{download_url.value()};
|
||||
@@ -255,7 +256,7 @@ namespace seafile {
|
||||
}
|
||||
|
||||
tl::expected<DirInfo, SeafileException> dirDetail(const std::string &repo_id, const std::string &path,
|
||||
const std::string &token, const TokenType token_type) const {
|
||||
const std::string &token, const TokenType token_type) const override{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api/v2.1/repos/{}/dir/detail/", repo_id))};
|
||||
const auto response = cpr::Get(url, cpr::Header{
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// Created by Shell on 2026-07-14.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_SEAFILE_API_INTERFACE_HPP
|
||||
#define SEADISK_SEAFILE_API_INTERFACE_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <tl/expected.hpp>
|
||||
|
||||
#include "exception.hpp"
|
||||
#include "account_info.hpp"
|
||||
#include "auth_token.hpp"
|
||||
#include "default_repo.hpp"
|
||||
#include "dir_info.hpp"
|
||||
#include "file_info.hpp"
|
||||
#include "repo_entry.hpp"
|
||||
#include "repo_info.hpp"
|
||||
#include "server_info.hpp"
|
||||
#include "token_type.hpp"
|
||||
|
||||
|
||||
namespace seafile::api {
|
||||
|
||||
class ISeaFileApi {
|
||||
public:
|
||||
|
||||
virtual ~ISeaFileApi() = default;
|
||||
|
||||
|
||||
virtual tl::expected<AuthToken, SeafileException>
|
||||
authToken(
|
||||
const std::string& username,
|
||||
const std::string& password
|
||||
) = 0;
|
||||
|
||||
|
||||
virtual tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo(
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(
|
||||
const std::string& repo_id,
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo(
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual 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 token_type
|
||||
) const = 0;
|
||||
|
||||
|
||||
virtual tl::expected<DirInfo, SeafileException>
|
||||
dirDetail(
|
||||
const std::string& repo_id,
|
||||
const std::string& path,
|
||||
const std::string& token,
|
||||
TokenType token_type
|
||||
) const = 0;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //SEADISK_SEAFILE_API_INTERFACE_HPP
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace seafile {
|
||||
namespace seafile::api {
|
||||
/**
|
||||
* seafile server info
|
||||
*/
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#ifndef SEADISK_TOKEN_TYPE_HPP
|
||||
#define SEADISK_TOKEN_TYPE_HPP
|
||||
namespace seafile
|
||||
namespace seafile::api
|
||||
{
|
||||
enum class TokenType
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user