From db6a93e55c7d7cdbdfc77b5316a83e7e7f47a709 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Thu, 7 Apr 2011 10:35:29 -0400 Subject: [PATCH] Now when inserting into the cache a max age time is specified. It can be refreshed by issuing a new max age time when doing a getCachedInfo call (optional). --- include/tomahawk/infosystem.h | 2 +- src/infosystem/infoplugins/lastfmplugin.cpp | 2 +- src/infosystem/infosystemcache.cpp | 53 ++++++++++++++++----- src/infosystem/infosystemcache.h | 7 +-- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/include/tomahawk/infosystem.h b/include/tomahawk/infosystem.h index 1409dabf9..e36af20d3 100644 --- a/include/tomahawk/infosystem.h +++ b/include/tomahawk/infosystem.h @@ -112,7 +112,7 @@ public: virtual void getInfo( const QString &caller, const InfoType type, const QVariant &data, InfoCustomData customData ) = 0; signals: - void getCachedInfo( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); + void getCachedInfo( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 newMaxAge, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); void updateCache( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ); void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); void finished( QString, Tomahawk::InfoSystem::InfoType ); diff --git a/src/infosystem/infoplugins/lastfmplugin.cpp b/src/infosystem/infoplugins/lastfmplugin.cpp index 652d87414..fcff625bf 100644 --- a/src/infosystem/infoplugins/lastfmplugin.cpp +++ b/src/infosystem/infoplugins/lastfmplugin.cpp @@ -178,7 +178,7 @@ LastFmPlugin::fetchCoverArt( const QString &caller, const InfoType type, const Q criteria["artist"] = hash["artist"].toString(); criteria["album"] = hash["album"].toString(); - emit getCachedInfo( criteria, caller, type, data, customData ); + emit getCachedInfo( criteria, 2419200000, caller, type, data, customData ); } void diff --git a/src/infosystem/infosystemcache.cpp b/src/infosystem/infosystemcache.cpp index 7d73c6ade..e6e7b04f5 100644 --- a/src/infosystem/infosystemcache.cpp +++ b/src/infosystem/infosystemcache.cpp @@ -53,10 +53,10 @@ InfoSystemCache::~InfoSystemCache() qDebug() << Q_FUNC_INFO; qDebug() << "Saving infosystemcache to disk"; QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation ); - for( int i = 0; i <= InfoNoInfo; i++ ) + for ( int i = 0; i <= InfoNoInfo; i++ ) { InfoType type = (InfoType)(i); - if( m_dirtySet.contains( type ) && m_dataCache.contains( type ) ) + if ( m_dirtySet.contains( type ) && m_dataCache.contains( type ) ) { QString cacheDir = cacheBaseDir + "/InfoSystemCache/" + QString::number( i ); saveCache( type, cacheDir ); @@ -66,29 +66,54 @@ InfoSystemCache::~InfoSystemCache() void -InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) +InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 newMaxAge, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ) { qDebug() << Q_FUNC_INFO; - if( !m_dataCache.contains( type ) || !m_dataCache[type].contains( criteria ) ) + if ( !m_dataCache.contains( type ) || !m_dataCache[type].contains( criteria ) ) { emit notInCache( criteria, caller, type, input, customData ); return; } + QHash< InfoCacheCriteria, QDateTime > typemaxtimecache = m_maxTimeCache[type]; + + if ( typemaxtimecache[criteria].toMSecsSinceEpoch() < QDateTime::currentMSecsSinceEpoch() ) + { + QHash< InfoCacheCriteria, QVariant > typedatacache = m_dataCache[type]; + QHash< InfoCacheCriteria, QDateTime > typeinserttimecache = m_insertTimeCache[type]; + typemaxtimecache.remove( criteria ); + typedatacache.remove( criteria ); + typeinserttimecache.remove( criteria ); + m_dirtySet.insert( type ); + emit notInCache( criteria, caller, type, input, customData ); + return; + } + + if ( newMaxAge > 0 ) + { + QHash< InfoCacheCriteria, QDateTime > typemaxtimecache = m_maxTimeCache[type]; + typemaxtimecache[criteria] = QDateTime::fromMSecsSinceEpoch( QDateTime::currentMSecsSinceEpoch() + newMaxAge ); + m_maxTimeCache[type] = typemaxtimecache; + m_dirtySet.insert( type ); + } + emit info( caller, type, input, m_dataCache[type][criteria], customData ); } void -InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ) +InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output ) { qDebug() << Q_FUNC_INFO; QHash< InfoCacheCriteria, QVariant > typedatacache = m_dataCache[type]; - QHash< InfoCacheCriteria, QDateTime > typetimecache = m_timeCache[type]; + QHash< InfoCacheCriteria, QDateTime > typeinserttimecache = m_insertTimeCache[type]; + QHash< InfoCacheCriteria, QDateTime > typemaxtimecache = m_maxTimeCache[type]; typedatacache[criteria] = output; - typetimecache[criteria] = QDateTime::currentDateTimeUtc(); + typeinserttimecache[criteria] = QDateTime::currentDateTimeUtc(); + typemaxtimecache[criteria] = QDateTime::fromMSecsSinceEpoch( QDateTime::currentMSecsSinceEpoch() + maxAge ); m_dataCache[type] = typedatacache; - m_timeCache[type] = typetimecache; + m_insertTimeCache[type] = typeinserttimecache; + m_maxTimeCache[type] = typemaxtimecache; m_dirtySet.insert( type ); } @@ -102,7 +127,8 @@ InfoSystemCache::loadCache( InfoType type, const QString &cacheFile ) foreach( QString group, cachedSettings.childGroups() ) { QHash< InfoCacheCriteria, QVariant > dataHash = m_dataCache[type]; - QHash< InfoCacheCriteria, QDateTime > dateHash = m_timeCache[type]; + QHash< InfoCacheCriteria, QDateTime > insertDateHash = m_insertTimeCache[type]; + QHash< InfoCacheCriteria, QDateTime > maxDateHash = m_maxTimeCache[type]; InfoCacheCriteria criteria; cachedSettings.beginGroup( group ); int numCriteria = cachedSettings.beginReadArray( "criteria" ); @@ -115,10 +141,12 @@ InfoSystemCache::loadCache( InfoType type, const QString &cacheFile ) } cachedSettings.endArray(); dataHash[criteria] = cachedSettings.value( "data" ); - dateHash[criteria] = cachedSettings.value( "time" ).toDateTime(); + insertDateHash[criteria] = cachedSettings.value( "inserttime" ).toDateTime(); + maxDateHash[criteria] = cachedSettings.value( "maxtime" ).toDateTime(); cachedSettings.endGroup(); m_dataCache[type] = dataHash; - m_timeCache[type] = dateHash; + m_insertTimeCache[type] = insertDateHash; + m_maxTimeCache[type] = maxDateHash; } } @@ -156,7 +184,8 @@ InfoSystemCache::saveCache( InfoType type, const QString &cacheDir ) } cachedSettings.endArray(); cachedSettings.setValue( "data", m_dataCache[type][criteria] ); - cachedSettings.setValue( "time", m_timeCache[type][criteria] ); + cachedSettings.setValue( "inserttime", m_insertTimeCache[type][criteria] ); + cachedSettings.setValue( "maxtime", m_maxTimeCache[type][criteria] ); cachedSettings.endGroup(); ++criteriaNumber; } diff --git a/src/infosystem/infosystemcache.h b/src/infosystem/infosystemcache.h index ca347f7bc..0ccad6d19 100644 --- a/src/infosystem/infosystemcache.h +++ b/src/infosystem/infosystemcache.h @@ -45,15 +45,16 @@ signals: void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); public slots: - void getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); - void updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, Tomahawk::InfoSystem::InfoType type, QVariant output ); + void getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 newMaxAge, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); + void updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output ); private: void loadCache( InfoType type, const QString &cacheFile ); void saveCache( InfoType type, const QString &cacheDir ); QHash< InfoType, QHash< InfoCacheCriteria, QVariant > > m_dataCache; - QHash< InfoType, QHash< InfoCacheCriteria, QDateTime > > m_timeCache; + QHash< InfoType, QHash< InfoCacheCriteria, QDateTime > > m_insertTimeCache; + QHash< InfoType, QHash< InfoCacheCriteria, QDateTime > > m_maxTimeCache; QSet< InfoType > m_dirtySet; };