1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-25 02:09:48 +01:00

Add a cache versioning system, and invalidate the cache in the upgrade

to version 1
This commit is contained in:
Jeff Mitchell 2011-07-11 16:01:53 -04:00
parent 3bd6bd540f
commit 28d34a84ad
5 changed files with 94 additions and 28 deletions

View File

@ -102,11 +102,13 @@ enum InfoType { // as items are saved in cache, mark them here to not change the
InfoNowResumed = 82,
InfoNowStopped = 83,
InfoNoInfo = 90,
InfoLove = 91,
InfoUnLove = 92,
InfoLove = 90,
InfoUnLove = 91,
InfoNotifyUser = 100
InfoNotifyUser = 100,
InfoNoInfo = 101 //WARNING: *ALWAYS* keep this last!
};
struct InfoRequestData {

View File

@ -23,6 +23,7 @@
#include <QCryptographicHash>
#include "infosystemcache.h"
#include "tomahawksettings.h"
namespace Tomahawk
@ -35,9 +36,26 @@ namespace InfoSystem
InfoSystemCache::InfoSystemCache( QObject* parent )
: QObject( parent )
, m_cacheBaseDir( QDesktopServices::storageLocation( QDesktopServices::CacheLocation ) + "/InfoSystemCache/" )
, m_cacheVersion( 1 )
{
qDebug() << Q_FUNC_INFO;
TomahawkSettings *s = TomahawkSettings::instance();
if( s->infoSystemCacheVersion() != m_cacheVersion )
{
qDebug() << "Cache version outdated, old:" << s->infoSystemCacheVersion()
<< "new:" << m_cacheVersion
<< "Doing upgrade, if any...";
uint current = s->infoSystemCacheVersion();
while( current < m_cacheVersion )
{
doUpgrade( current, current + 1 );
current++;
}
s->setInfoSystemCacheVersion( m_cacheVersion );
}
m_pruneTimer.setInterval( 300000 );
m_pruneTimer.setSingleShot( false );
connect( &m_pruneTimer, SIGNAL( timeout() ), SLOT( pruneTimerFired() ) );
@ -50,6 +68,29 @@ InfoSystemCache::~InfoSystemCache()
qDebug() << Q_FUNC_INFO;
}
void
InfoSystemCache::doUpgrade( uint oldVersion, uint newVersion )
{
Q_UNUSED( newVersion );
qDebug() << Q_FUNC_INFO;
if ( oldVersion == 0 )
{
qDebug() << Q_FUNC_INFO << "Wiping cache";
for ( int i = 0; i <= InfoNoInfo; i++ )
{
InfoType type = (InfoType)(i);
const QString cacheDirName = m_cacheBaseDir + QString::number( (int)type );
QFileInfoList fileList = QDir( cacheDirName ).entryInfoList( QDir::Files | QDir::NoDotAndDotDot );
foreach ( QFileInfo file, fileList )
{
if ( !QFile::remove( file.canonicalFilePath() ) )
qDebug() << "During upgrade, failed to remove cache file " << file.canonicalFilePath();
}
}
}
}
void
InfoSystemCache::pruneTimerFired()
@ -94,6 +135,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
if ( !fileLocationHash.isEmpty() )
{
//We already know of some values, so no need to re-read the directory again as it's already happened
qDebug() << Q_FUNC_INFO << " notInCache -- filelocationhash empty";
emit notInCache( requestId, criteria, requestData );
return;
}
@ -103,6 +145,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
if ( !dir.exists() )
{
//Dir doesn't exist so clearly not in cache
qDebug() << Q_FUNC_INFO << " notInCache -- dir doesn't exist";
emit notInCache( requestId, criteria, requestData );
return;
}
@ -118,7 +161,8 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
m_fileLocationCache[ requestData.type ] = fileLocationHash;
if ( !fileLocationHash.contains( criteriaHashVal ) )
{
//Still didn't fine it? It's really not in the cache then
//Still didn't find it? It's really not in the cache then
qDebug() << Q_FUNC_INFO << " notInCache -- filelocationhash doesn't contain criteria val";
emit notInCache( requestId, criteria, requestData );
return;
}
@ -137,7 +181,8 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
fileLocationHash.remove( criteriaHashVal );
m_fileLocationCache[ requestData.type ] = fileLocationHash;
m_dataCache.remove( criteriaHashVal );
qDebug() << Q_FUNC_INFO << " notInCache -- file was stale";
emit notInCache( requestId, criteria, requestData );
return;
}
@ -147,7 +192,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
if ( !QFile::rename( file.canonicalFilePath(), newFilePath ) )
{
qDebug() << "Failed to move old cache file to new location!";
qDebug() << Q_FUNC_INFO << " notInCache -- failed to move old cache file to new location";
emit notInCache( requestId, criteria, requestData );
return;
}
@ -161,12 +206,12 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
QSettings cachedSettings( fileLocationHash[ criteriaHashVal ], QSettings::IniFormat );
QVariant output = cachedSettings.value( "data" );
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );
emit info( requestId, requestData, output );
}
else
{
emit info( requestId, requestData, QVariant( *(m_dataCache[criteriaHashVal]) ) );
emit info( requestId, requestData, QVariant( *( m_dataCache[ criteriaHashVal ] ) ) );
}
}
@ -180,18 +225,18 @@ InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteri
const QString cacheDir = m_cacheBaseDir + QString::number( (int)type );
const QString settingsFilePath( cacheDir + '/' + criteriaHashVal + '.' + QString::number( QDateTime::currentMSecsSinceEpoch() + maxAge ) );
QHash< QString, QString > fileLocationHash = m_fileLocationCache[type];
QHash< QString, QString > fileLocationHash = m_fileLocationCache[ type ];
if ( fileLocationHash.contains( criteriaHashVal ) )
{
if ( !QFile::rename( fileLocationHash[criteriaHashVal], settingsFilePath ) )
if ( !QFile::rename( fileLocationHash[ criteriaHashVal ], settingsFilePath ) )
{
qDebug() << "Failed to move old cache file to new location!";
return;
}
fileLocationHash[criteriaHashVal] = settingsFilePath;
m_fileLocationCache[type] = fileLocationHash;
fileLocationHash[ criteriaHashVal ] = settingsFilePath;
m_fileLocationCache[ type ] = fileLocationHash;
QSettings cachedSettings( fileLocationHash[criteriaHashVal], QSettings::IniFormat );
QSettings cachedSettings( fileLocationHash[ criteriaHashVal ], QSettings::IniFormat );
cachedSettings.setValue( "data", output );
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );

View File

@ -54,12 +54,15 @@ private slots:
void pruneTimerFired();
private:
void doUpgrade( uint oldVersion, uint newVersion );
const QString criteriaMd5( const Tomahawk::InfoSystem::InfoCriteriaHash &criteria ) const;
QString m_cacheBaseDir;
QHash< InfoType, QHash< QString, QString > > m_fileLocationCache;
QTimer m_pruneTimer;
QCache< QString, QVariant > m_dataCache;
uint m_cacheVersion;
};
} //namespace InfoSystem

View File

@ -147,6 +147,34 @@ TomahawkSettings::doUpgrade( int oldVersion, int newVersion )
}
void
TomahawkSettings::setAcceptedLegalWarning( bool accept )
{
setValue( "acceptedLegalWarning", accept );
}
bool
TomahawkSettings::acceptedLegalWarning() const
{
return value( "acceptedLegalWarning", false ).toBool();
}
void
TomahawkSettings::setInfoSystemCacheVersion( uint version )
{
setValue( "infosystemcacheversion", version );
}
uint
TomahawkSettings::infoSystemCacheVersion() const
{
return value( "infosystemcacheversion", 0 ).toUInt();
}
QStringList
TomahawkSettings::scannerPaths()
{
@ -238,20 +266,6 @@ TomahawkSettings::setWatchForChanges( bool watch )
}
void
TomahawkSettings::setAcceptedLegalWarning( bool accept )
{
setValue( "acceptedLegalWarning", accept );
}
bool
TomahawkSettings::acceptedLegalWarning() const
{
return value( "acceptedLegalWarning", false ).toBool();
}
bool
TomahawkSettings::httpEnabled() const
{

View File

@ -49,6 +49,8 @@ public:
void setScannerMode( ScannerMode mode );
uint scannerTime() const;
void setScannerTime( uint time );
uint infoSystemCacheVersion() const;
void setInfoSystemCacheVersion( uint version );
bool watchForChanges() const;
void setWatchForChanges( bool watch );