Skip to content

Commit

Permalink
Fixed caching in Cesium AssetAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbeverage committed Sep 15, 2023
1 parent 2611449 commit 289192c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/osgEarthCesium/AssetAccessor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <CesiumAsync/IAssetAccessor.h>
#include <CesiumAsync/IAssetResponse.h>
#include <osgDB/Options>
#include <osgEarth/Cache>

namespace osgEarth {
namespace Cesium
Expand Down Expand Up @@ -72,6 +74,8 @@ namespace osgEarth {
class AssetAccessor : public CesiumAsync::IAssetAccessor
{
public:
AssetAccessor();

virtual CesiumAsync::Future<std::shared_ptr<CesiumAsync::IAssetRequest>>
get(const CesiumAsync::AsyncSystem& asyncSystem,
const std::string& url,
Expand All @@ -87,6 +91,10 @@ namespace osgEarth {
const gsl::span<const std::byte>& contentPayload) override;

virtual void tick() noexcept override;

private:
osg::ref_ptr< osgDB::Options > _options;
osg::ref_ptr<CacheSettings> _cacheSettings;
};
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/osgEarthCesium/AssetAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <osgEarth/Notify>
#include <osgEarth/URI>
#include <osgEarth/Registry>

using namespace osgEarth;
using namespace osgEarth::Cesium;
Expand Down Expand Up @@ -85,25 +86,35 @@ gsl::span<const std::byte> AssetResponse::data() const

/**********************************************/

AssetAccessor::AssetAccessor()
{
_options = new osgDB::Options;
_cacheSettings = new CacheSettings;
_cacheSettings->setCache(Registry::instance()->getDefaultCache());
_cacheSettings->store(_options.get());
}

CesiumAsync::Future<std::shared_ptr<CesiumAsync::IAssetRequest>>
AssetAccessor::get(const CesiumAsync::AsyncSystem& asyncSystem,
const std::string& url,
const std::vector<CesiumAsync::IAssetAccessor::THeader>& headers)
{
osg::ref_ptr<osgDB::Options> options = _options.get();
auto request = std::make_shared<AssetRequest>("GET", url, headers);
return asyncSystem.createFuture<std::shared_ptr<CesiumAsync::IAssetRequest>>(
[&](const auto& promise)
{
asyncSystem.runInWorkerThread([promise, request, url, headers]() {
asyncSystem.runInWorkerThread([promise, request, url, headers, options]() {
// This should run in another thread.
URIContext uriContext;
for (auto header : headers)
{
uriContext.addHeader(header.first, header.second);
}

URI uri(url, uriContext);
auto httpResponse = uri.readString();

auto httpResponse = uri.readString(options.get());
std::unique_ptr< AssetResponse > response = std::make_unique< AssetResponse >();

if (httpResponse.code() == ReadResult::RESULT_OK)
Expand Down

0 comments on commit 289192c

Please sign in to comment.