cesium-native  0.41.0
CachingAssetAccessor.h
1 #pragma once
2 
3 #include "IAssetAccessor.h"
4 #include "IAssetRequest.h"
5 #include "ICacheDatabase.h"
6 #include "ThreadPool.h"
7 
8 #include <spdlog/fwd.h>
9 
10 #include <atomic>
11 #include <cstddef>
12 #include <memory>
13 #include <string>
14 
15 namespace CesiumAsync {
16 class AsyncSystem;
17 
26 public:
40  const std::shared_ptr<spdlog::logger>& pLogger,
41  const std::shared_ptr<IAssetAccessor>& pAssetAccessor,
42  const std::shared_ptr<ICacheDatabase>& pCacheDatabase,
43  int32_t requestsPerCachePrune = 10000);
44 
45  virtual ~CachingAssetAccessor() noexcept override;
46 
48  virtual Future<std::shared_ptr<IAssetRequest>>
49  get(const AsyncSystem& asyncSystem,
50  const std::string& url,
51  const std::vector<THeader>& headers) override;
52 
53  virtual Future<std::shared_ptr<IAssetRequest>> request(
54  const AsyncSystem& asyncSystem,
55  const std::string& verb,
56  const std::string& url,
57  const std::vector<THeader>& headers,
58  const gsl::span<const std::byte>& contentPayload) override;
59 
61  virtual void tick() noexcept override;
62 
63 private:
64  int32_t _requestsPerCachePrune;
65  std::atomic<int32_t> _requestSinceLastPrune;
66  std::shared_ptr<spdlog::logger> _pLogger;
67  std::shared_ptr<IAssetAccessor> _pAssetAccessor;
68  std::shared_ptr<ICacheDatabase> _pCacheDatabase;
69  ThreadPool _cacheThreadPool;
70  CESIUM_TRACE_DECLARE_TRACK_SET(_pruneSlots, "Prune cache database");
71 };
72 } // namespace CesiumAsync
A system for managing asynchronous requests and tasks.
Definition: AsyncSystem.h:36
A decorator for an IAssetAccessor that caches requests and responses in an ICacheDatabase.
virtual Future< std::shared_ptr< IAssetRequest > > get(const AsyncSystem &asyncSystem, const std::string &url, const std::vector< THeader > &headers) override
Starts a new request for the asset with the given URL. The request proceeds asynchronously without bl...
virtual void tick() noexcept override
Ticks the asset accessor system while the main thread is blocked.
virtual Future< std::shared_ptr< IAssetRequest > > request(const AsyncSystem &asyncSystem, const std::string &verb, const std::string &url, const std::vector< THeader > &headers, const gsl::span< const std::byte > &contentPayload) override
Starts a new request to the given URL, using the provided HTTP verb and the provided content payload.
CachingAssetAccessor(const std::shared_ptr< spdlog::logger > &pLogger, const std::shared_ptr< IAssetAccessor > &pAssetAccessor, const std::shared_ptr< ICacheDatabase > &pCacheDatabase, int32_t requestsPerCachePrune=10000)
Constructs a new instance.
A value that will be available in the future, as produced by AsyncSystem.
Definition: Future.h:29
Provides asynchronous access to assets, usually files downloaded via HTTP.
std::pair< std::string, std::string > THeader
An HTTP header represented as a key/value pair.
An asynchronous request for an asset, usually a file downloaded via HTTP.
Definition: IAssetRequest.h:17
Provides database storage interface to cache completed request.
A thread pool created by AsyncSystem::createThreadPool.
Definition: ThreadPool.h:18
Classes that support asynchronous operations.