1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-15 10:33:59 +02:00

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).

This commit is contained in:
Jeff Mitchell
2011-04-07 10:35:29 -04:00
parent 196397f210
commit db6a93e55c
4 changed files with 47 additions and 17 deletions

View File

@@ -112,7 +112,7 @@ public:
virtual void getInfo( const QString &caller, const InfoType type, const QVariant &data, InfoCustomData customData ) = 0; virtual void getInfo( const QString &caller, const InfoType type, const QVariant &data, InfoCustomData customData ) = 0;
signals: 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 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 info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData );
void finished( QString, Tomahawk::InfoSystem::InfoType ); void finished( QString, Tomahawk::InfoSystem::InfoType );

View File

@@ -178,7 +178,7 @@ LastFmPlugin::fetchCoverArt( const QString &caller, const InfoType type, const Q
criteria["artist"] = hash["artist"].toString(); criteria["artist"] = hash["artist"].toString();
criteria["album"] = hash["album"].toString(); criteria["album"] = hash["album"].toString();
emit getCachedInfo( criteria, caller, type, data, customData ); emit getCachedInfo( criteria, 2419200000, caller, type, data, customData );
} }
void void

View File

@@ -53,10 +53,10 @@ InfoSystemCache::~InfoSystemCache()
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
qDebug() << "Saving infosystemcache to disk"; qDebug() << "Saving infosystemcache to disk";
QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation ); QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation );
for( int i = 0; i <= InfoNoInfo; i++ ) for ( int i = 0; i <= InfoNoInfo; i++ )
{ {
InfoType type = (InfoType)(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 ); QString cacheDir = cacheBaseDir + "/InfoSystemCache/" + QString::number( i );
saveCache( type, cacheDir ); saveCache( type, cacheDir );
@@ -66,29 +66,54 @@ InfoSystemCache::~InfoSystemCache()
void 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; 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 ); emit notInCache( criteria, caller, type, input, customData );
return; 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 ); emit info( caller, type, input, m_dataCache[type][criteria], customData );
} }
void 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; qDebug() << Q_FUNC_INFO;
QHash< InfoCacheCriteria, QVariant > typedatacache = m_dataCache[type]; 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; typedatacache[criteria] = output;
typetimecache[criteria] = QDateTime::currentDateTimeUtc(); typeinserttimecache[criteria] = QDateTime::currentDateTimeUtc();
typemaxtimecache[criteria] = QDateTime::fromMSecsSinceEpoch( QDateTime::currentMSecsSinceEpoch() + maxAge );
m_dataCache[type] = typedatacache; m_dataCache[type] = typedatacache;
m_timeCache[type] = typetimecache; m_insertTimeCache[type] = typeinserttimecache;
m_maxTimeCache[type] = typemaxtimecache;
m_dirtySet.insert( type ); m_dirtySet.insert( type );
} }
@@ -102,7 +127,8 @@ InfoSystemCache::loadCache( InfoType type, const QString &cacheFile )
foreach( QString group, cachedSettings.childGroups() ) foreach( QString group, cachedSettings.childGroups() )
{ {
QHash< InfoCacheCriteria, QVariant > dataHash = m_dataCache[type]; 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; InfoCacheCriteria criteria;
cachedSettings.beginGroup( group ); cachedSettings.beginGroup( group );
int numCriteria = cachedSettings.beginReadArray( "criteria" ); int numCriteria = cachedSettings.beginReadArray( "criteria" );
@@ -115,10 +141,12 @@ InfoSystemCache::loadCache( InfoType type, const QString &cacheFile )
} }
cachedSettings.endArray(); cachedSettings.endArray();
dataHash[criteria] = cachedSettings.value( "data" ); 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(); cachedSettings.endGroup();
m_dataCache[type] = dataHash; 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.endArray();
cachedSettings.setValue( "data", m_dataCache[type][criteria] ); 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(); cachedSettings.endGroup();
++criteriaNumber; ++criteriaNumber;
} }

View File

@@ -45,15 +45,16 @@ signals:
void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ); void info( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData );
public slots: public slots:
void getCachedInfoSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData ); 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, Tomahawk::InfoSystem::InfoType type, QVariant output ); void updateCacheSlot( Tomahawk::InfoSystem::InfoCacheCriteria criteria, qint64 maxAge, Tomahawk::InfoSystem::InfoType type, QVariant output );
private: private:
void loadCache( InfoType type, const QString &cacheFile ); void loadCache( InfoType type, const QString &cacheFile );
void saveCache( InfoType type, const QString &cacheDir ); void saveCache( InfoType type, const QString &cacheDir );
QHash< InfoType, QHash< InfoCacheCriteria, QVariant > > m_dataCache; 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; QSet< InfoType > m_dirtySet;
}; };