Skip to content

Commit

Permalink
Unify CachedFileDownloader into a single cache, make sure it's saved …
Browse files Browse the repository at this point in the history
…and cleaned up.
  • Loading branch information
dkulp committed Dec 10, 2023
1 parent 1500067 commit 951c282
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 30 deletions.
13 changes: 11 additions & 2 deletions xLights/CachedFileDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void CachedFileDownloader::SaveCache()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));

std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
if (!Initialize())
{
return;
Expand Down Expand Up @@ -184,6 +185,7 @@ void CachedFileDownloader::LoadCache()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));

std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
_cacheItems.clear();

if (!Initialize())
Expand Down Expand Up @@ -224,6 +226,7 @@ void CachedFileDownloader::LoadCache()

FileCacheItem* CachedFileDownloader::Find(wxURI url)
{
std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
for (const auto& it : _cacheItems)
{
if (*it == url)
Expand All @@ -236,19 +239,22 @@ FileCacheItem* CachedFileDownloader::Find(wxURI url)
}

static std::unique_ptr<CachedFileDownloader> _defaultCache(nullptr);
static std::mutex _defaultCacheLock;
CachedFileDownloader& CachedFileDownloader::GetDefaultCache() {
std::unique_lock<std::mutex> lock(_defaultCacheLock);
if (_defaultCache.get() == nullptr) {
std::unique_ptr<CachedFileDownloader> tmp(new CachedFileDownloader());
_defaultCache = std::move(tmp);
}
return *_defaultCache.get();
}

CachedFileDownloader::CachedFileDownloader(const std::string cacheDir) : _cacheDir(cacheDir)
CachedFileDownloader::CachedFileDownloader() : _cacheDir("")
{
_initialised = false;
}
bool CachedFileDownloader::Initialize() {
std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
if (_initialised) {
return _enabled;
}
Expand Down Expand Up @@ -293,6 +299,7 @@ void CachedFileDownloader::ClearCache()
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));

logger_base.debug("File Cache cleared.");
std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
for (const auto& it : _cacheItems)
{
it->Delete();
Expand All @@ -304,13 +311,15 @@ void CachedFileDownloader::PurgeAgedItems()
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));

logger_base.debug("File Cache purging aged items.");
std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
for (const auto& it : _cacheItems)
{
it->PurgeIfAged();
}
}

int CachedFileDownloader::size() {
std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
if (!Initialize()) {
return 0;
}
Expand All @@ -320,7 +329,7 @@ int CachedFileDownloader::size() {
std::string CachedFileDownloader::GetFile(wxURI url, CACHEFOR cacheFor, const wxString& forceType, wxProgressDialog* prog, int low, int high, bool keepProgress)
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));

std::lock_guard<std::recursive_mutex> lock(_cacheItemsLock);
if (!Initialize())
{
// because we dont have a valid place to save the cache we cant cache anything beyond this session
Expand Down
4 changes: 3 additions & 1 deletion xLights/CachedFileDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <list>
#include <wx/uri.h>
#include <wx/filename.h>
#include <mutex>

class wxProgressDialog;
class wxXmlNode;
Expand Down Expand Up @@ -53,6 +54,7 @@ class CachedFileDownloader
{
std::string _cacheDir;
std::list<FileCacheItem*> _cacheItems;
std::recursive_mutex _cacheItemsLock;
std::string _cacheFile;
bool _initialised;
bool _enabled;
Expand All @@ -63,9 +65,9 @@ class CachedFileDownloader
bool Initialize();
FileCacheItem* Find(wxURI url);

CachedFileDownloader();
public:

CachedFileDownloader(const std::string cacheDir = "");
virtual ~CachedFileDownloader();
// erase everything from cache
void ClearCache();
Expand Down
6 changes: 1 addition & 5 deletions xLights/ShaderDownloadDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#undef min
#undef max

CachedFileDownloader ShaderDownloadDialog::_cache;

class MShader
{
Expand Down Expand Up @@ -327,9 +326,6 @@ ShaderDownloadDialog::ShaderDownloadDialog(wxWindow* parent, wxWindowID id, cons

SetSize(800, 600);

static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.debug("File cache size: %d", _cache.size());

ListView_Sites->AppendColumn("", wxListColumnFormat::wxLIST_FORMAT_LEFT);
ListView_Sites->InsertItem(0, "www.interactiveshaderformat.com/");
ListView_Sites->InsertItem(0, "www.shadertoy.com/");
Expand Down Expand Up @@ -426,7 +422,7 @@ ShaderDownloadDialog::~ShaderDownloadDialog()
//(*Destroy(ShaderDownloadDialog)
//*)

_cache.Save();
GetCache().Save();

for (const auto& it : _shaders)
{
Expand Down
3 changes: 1 addition & 2 deletions xLights/ShaderDownloadDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class ShaderDownloadDialog: public wxDialog
std::string _shaderFile;
int _currImage = -1;
wxImage _shaderImage;
static CachedFileDownloader _cache;

wxXmlDocument* GetXMLFromURL(wxURI url, std::string& filename, wxProgressDialog* prog, int low, int high) const;
bool LoadTree(wxProgressDialog* prog, int low = 0, int high = 100);
Expand All @@ -57,7 +56,7 @@ class ShaderDownloadDialog: public wxDialog
virtual ~ShaderDownloadDialog();
std::string GetShaderFile() const { return _shaderFile; }
bool DlgInit(wxProgressDialog* prog, int low, int high);
static CachedFileDownloader& GetCache() { return _cache; }
static CachedFileDownloader& GetCache() { return CachedFileDownloader::GetDefaultCache(); }

//(*Declarations(ShaderDownloadDialog)
wxButton* Button_Download;
Expand Down
8 changes: 4 additions & 4 deletions xLights/TipOfTheDayDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class TipOfDayThread : public wxThread
virtual void* Entry() override
{
// download Tip of day content here
CachedFileDownloader cache;
auto file = cache.GetFile(wxURI(TOD_BASE_URL + "tod.xml"), CACHEFOR::CACHETIME_DAY);
auto file = CachedFileDownloader::GetDefaultCache().GetFile(wxURI(TOD_BASE_URL + "tod.xml"), CACHEFOR::CACHETIME_DAY);
CachedFileDownloader::GetDefaultCache().Save();

wxCommandEvent e(EVT_TIPOFDAY_READY);
e.SetString(file);
Expand Down Expand Up @@ -121,8 +121,9 @@ class xlCachedHtmlWindow : public wxHtmlWindow {
url = baseURL + url.substr(baseFileLocation.size());
}
wxURI uri(url);
auto file = cache.GetFile(uri, CACHEFOR::CACHETIME_LONG);
auto file = CachedFileDownloader::GetDefaultCache().GetFile(uri, CACHEFOR::CACHETIME_LONG);
if (file != "") {
CachedFileDownloader::GetDefaultCache().Save();
*redirect = file;
if (baseFileLocation == "") {
baseFileLocation = file.substr(0, file.find_last_of(wxFileName::GetPathSeparator()));
Expand All @@ -131,7 +132,6 @@ class xlCachedHtmlWindow : public wxHtmlWindow {
}
return wxHtmlWindow::OnHTMLOpeningURL(type, url, redirect);
}
mutable CachedFileDownloader cache;
mutable std::string baseURL;
mutable std::string baseFileLocation;
};
Expand Down
10 changes: 2 additions & 8 deletions xLights/VendorModelDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include "UtilFunctions.h"
#include "ExternalHooks.h"

CachedFileDownloader VendorModelDialog::_cache;

class MModel;

class MModelWiring
Expand Down Expand Up @@ -800,9 +798,6 @@ VendorModelDialog::VendorModelDialog(wxWindow* parent, const std::string& showFo

SetSize(800, 600);

static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.debug("File cache size: %d", _cache.size());

PopulateModelPanel((MModel*)nullptr);
PopulateVendorPanel(nullptr);

Expand Down Expand Up @@ -1038,10 +1033,9 @@ VendorModelDialog::~VendorModelDialog()
//(*Destroy(VendorModelDialog)
//*)

_cache.Save();
GetCache().Save();

for (const auto& it : _vendors)
{
for (const auto& it : _vendors) {
delete it;
}
}
Expand Down
3 changes: 1 addition & 2 deletions xLights/VendorModelDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class VendorModelDialog: public wxDialog
int _currImage = -1;
wxImage _vendorImage;
wxImage _modelImage;
static CachedFileDownloader _cache;

wxXmlDocument* GetXMLFromURL(wxURI url, std::string& filename, wxProgressDialog* prog, int low, int high, bool keepProgress) const;
bool LoadTree(wxProgressDialog* prog, int low = 0, int high = 100);
Expand All @@ -70,7 +69,7 @@ class VendorModelDialog: public wxDialog
std::string GetModelFile() const { return _modelFile; }
bool DlgInit(wxProgressDialog* prog, int low, int high);
bool FindModelFile(const std::string &vendor, const std::string &model);
static CachedFileDownloader& GetCache() { return _cache; }
static CachedFileDownloader& GetCache() { return CachedFileDownloader::GetDefaultCache(); }

//(*Declarations(VendorModelDialog)
wxButton* Button_InsertModel;
Expand Down
8 changes: 2 additions & 6 deletions xLights/xLightsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8750,12 +8750,8 @@ void xLightsFrame::SetSnapToTimingMarks(bool b)

void xLightsFrame::PurgeDownloadCache()
{
VendorModelDialog::GetCache().ClearCache();
VendorModelDialog::GetCache().Save();
VendorMusicDialog::GetCache().ClearCache();
VendorMusicDialog::GetCache().Save();
ShaderDownloadDialog::GetCache().ClearCache();
ShaderDownloadDialog::GetCache().Save();
CachedFileDownloader::GetDefaultCache().ClearCache();
CachedFileDownloader::GetDefaultCache().Save();
}

bool xLightsFrame::GetRecycleTips() const
Expand Down

0 comments on commit 951c282

Please sign in to comment.