Use a QTimer for writing scrobbler cache
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
@@ -29,6 +30,7 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include <QTimer>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
@@ -37,17 +39,23 @@
|
|||||||
|
|
||||||
#include "core/song.h"
|
#include "core/song.h"
|
||||||
#include "core/logging.h"
|
#include "core/logging.h"
|
||||||
#include "core/closure.h"
|
|
||||||
|
|
||||||
#include "scrobblercache.h"
|
#include "scrobblercache.h"
|
||||||
#include "scrobblercacheitem.h"
|
#include "scrobblercacheitem.h"
|
||||||
|
|
||||||
ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent) :
|
ScrobblerCache::ScrobblerCache(const QString &filename, QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
|
timer_flush_(new QTimer(this)),
|
||||||
filename_(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + filename),
|
filename_(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + filename),
|
||||||
loaded_(false) {
|
loaded_(false) {
|
||||||
|
|
||||||
ReadCache();
|
ReadCache();
|
||||||
loaded_ = true;
|
loaded_ = true;
|
||||||
|
|
||||||
|
timer_flush_->setSingleShot(true);
|
||||||
|
timer_flush_->setInterval(600000);
|
||||||
|
QObject::connect(timer_flush_, &QTimer::timeout, this, &ScrobblerCache::WriteCache);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScrobblerCache::~ScrobblerCache() {
|
ScrobblerCache::~ScrobblerCache() {
|
||||||
@@ -203,7 +211,9 @@ ScrobblerCacheItemPtr ScrobblerCache::Add(const Song &song, const quint64 ×
|
|||||||
ScrobblerCacheItemPtr item = std::make_shared<ScrobblerCacheItem>(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
|
ScrobblerCacheItemPtr item = std::make_shared<ScrobblerCacheItem>(song.artist(), album, title, song.albumartist(), song.track(), song.length_nanosec(), timestamp);
|
||||||
scrobbler_cache_.insert(timestamp, item);
|
scrobbler_cache_.insert(timestamp, item);
|
||||||
|
|
||||||
if (loaded_) DoInAMinuteOrSo(this, SLOT(WriteCache()));
|
if (loaded_ && !timer_flush_->isActive()) {
|
||||||
|
timer_flush_->start();
|
||||||
|
}
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
|
|
||||||
@@ -247,6 +257,9 @@ void ScrobblerCache::Flush(const QList<quint64> &list) {
|
|||||||
if (!scrobbler_cache_.contains(timestamp)) continue;
|
if (!scrobbler_cache_.contains(timestamp)) continue;
|
||||||
scrobbler_cache_.remove(timestamp);
|
scrobbler_cache_.remove(timestamp);
|
||||||
}
|
}
|
||||||
DoInAMinuteOrSo(this, SLOT(WriteCache()));
|
|
||||||
|
if (!timer_flush_->isActive()) {
|
||||||
|
timer_flush_->start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
#include "scrobblercacheitem.h"
|
#include "scrobblercacheitem.h"
|
||||||
|
|
||||||
|
class QTimer;
|
||||||
class Song;
|
class Song;
|
||||||
|
|
||||||
class ScrobblerCache : public QObject {
|
class ScrobblerCache : public QObject {
|
||||||
@@ -56,6 +57,7 @@ class ScrobblerCache : public QObject {
|
|||||||
void WriteCache();
|
void WriteCache();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QTimer *timer_flush_;
|
||||||
QString filename_;
|
QString filename_;
|
||||||
bool loaded_;
|
bool loaded_;
|
||||||
QHash<quint64, ScrobblerCacheItemPtr> scrobbler_cache_;
|
QHash<quint64, ScrobblerCacheItemPtr> scrobbler_cache_;
|
||||||
|
|||||||
Reference in New Issue
Block a user