From cc6c7430d4302693fe057c1461a0e42c5725b91f Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sat, 30 Jan 2021 21:48:45 +0100 Subject: [PATCH] Use a QTimer for writing scrobbler cache --- src/scrobbler/scrobblercache.cpp | 19 ++++++++++++++++--- src/scrobbler/scrobblercache.h | 2 ++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/scrobbler/scrobblercache.cpp b/src/scrobbler/scrobblercache.cpp index 04f8113d4..509da8cdc 100644 --- a/src/scrobbler/scrobblercache.cpp +++ b/src/scrobbler/scrobblercache.cpp @@ -20,6 +20,7 @@ #include "config.h" #include +#include #include #include @@ -29,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -37,17 +39,23 @@ #include "core/song.h" #include "core/logging.h" -#include "core/closure.h" #include "scrobblercache.h" #include "scrobblercacheitem.h" ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent) : QObject(parent), + timer_flush_(new QTimer(this)), filename_(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + filename), loaded_(false) { + ReadCache(); loaded_ = true; + + timer_flush_->setSingleShot(true); + timer_flush_->setInterval(600000); + QObject::connect(timer_flush_, &QTimer::timeout, this, &ScrobblerCache::WriteCache); + } ScrobblerCache::~ScrobblerCache() { @@ -203,7 +211,9 @@ ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 × ScrobblerCacheItemPtr item = std::make_shared(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp); scrobbler_cache_.insert(timestamp, item); - if (loaded_) DoInAMinuteOrSo(this, SLOT(WriteCache())); + if (loaded_ && !timer_flush_->isActive()) { + timer_flush_->start(); + } return item; @@ -247,6 +257,9 @@ void ScrobblerCache::Flush(const QList &list) { if (!scrobbler_cache_.contains(timestamp)) continue; scrobbler_cache_.remove(timestamp); } - DoInAMinuteOrSo(this, SLOT(WriteCache())); + + if (!timer_flush_->isActive()) { + timer_flush_->start(); + } } diff --git a/src/scrobbler/scrobblercache.h b/src/scrobbler/scrobblercache.h index 5121212d5..4cc66ae37 100644 --- a/src/scrobbler/scrobblercache.h +++ b/src/scrobbler/scrobblercache.h @@ -32,6 +32,7 @@ #include "scrobblercacheitem.h" +class QTimer; class Song; class ScrobblerCache : public QObject { @@ -56,6 +57,7 @@ class ScrobblerCache : public QObject { void WriteCache(); private: + QTimer *timer_flush_; QString filename_; bool loaded_; QHash scrobbler_cache_;