1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-19 15:29:42 +01:00

Self cleaning peerInfo cache, fixes TWK-1396

This commit is contained in:
Uwe L. Korn 2013-06-09 23:37:10 +02:00
parent 9e24fc7ddc
commit 5b0493bd6c
6 changed files with 152 additions and 8 deletions

View File

@ -337,6 +337,7 @@ list(APPEND libSources
sip/SipInfo.cpp
sip/PeerInfo.cpp
sip/SipStatusMessage.cpp
sip/WeakPeerHash.cpp
utils/TomahawkUtils.cpp
utils/Logger.cpp

View File

@ -20,6 +20,7 @@
#include "PeerInfo_p.h"
#include "SipPlugin.h"
#include "WeakPeerHash.h"
#include "utils/TomahawkCache.h"
#include "utils/TomahawkUtilsGui.h"
#include "network/ControlConnection.h"
@ -32,7 +33,7 @@
namespace Tomahawk
{
QHash< QString, peerinfo_wptr > PeerInfo::s_peersByCacheKey = QHash< QString, peerinfo_wptr >();
WeakPeerHash PeerInfo::s_peersByCacheKey = WeakPeerHash();
QHash< SipPlugin*, peerinfo_ptr > PeerInfo::s_selfPeersBySipPlugin = QHash< SipPlugin*, peerinfo_ptr >();
@ -79,9 +80,9 @@ Tomahawk::peerinfo_ptr
PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options )
{
const QString key = peerCacheKey( parent, id );
if ( s_peersByCacheKey.contains( key ) && !s_peersByCacheKey.value( key ).isNull() )
if ( s_peersByCacheKey.hash().contains( key ) && !s_peersByCacheKey.hash().value( key ).isNull() )
{
return s_peersByCacheKey.value( key ).toStrongRef();
return s_peersByCacheKey.hash().value( key ).toStrongRef();
}
// if AutoCreate isn't enabled nothing to do here
@ -92,7 +93,7 @@ PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options )
peerinfo_ptr peerInfo( new PeerInfo( parent, id ) );
peerInfo->setWeakRef( peerInfo.toWeakRef() );
s_peersByCacheKey.insert( key, peerInfo.toWeakRef() );
s_peersByCacheKey.insert( key, peerInfo );
return peerInfo;
}
@ -102,7 +103,7 @@ QList< Tomahawk::peerinfo_ptr >
PeerInfo::getAll()
{
QList< Tomahawk::peerinfo_ptr > strongRefs;
foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.values() )
foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.hash().values() )
{
if ( !wptr.isNull() )
strongRefs << wptr.toStrongRef();
@ -122,8 +123,6 @@ PeerInfo::PeerInfo( SipPlugin* parent, const QString& id )
PeerInfo::~PeerInfo()
{
// tDebug() << Q_FUNC_INFO;
s_peersByCacheKey.remove( s_peersByCacheKey.key( weakRef() ) );
delete m_avatar;
delete m_fancyAvatar;
delete d_ptr;

View File

@ -30,6 +30,7 @@
class ControlConnection;
class SipPlugin;
class SipInfo;
class WeakPeerHash;
namespace Tomahawk
{
@ -129,7 +130,7 @@ private:
Q_DECLARE_PRIVATE( Tomahawk::PeerInfo )
Tomahawk::PeerInfoPrivate* d_ptr;
static QHash< QString, peerinfo_wptr > s_peersByCacheKey;
static WeakPeerHash s_peersByCacheKey;
static QHash< SipPlugin*, peerinfo_ptr > s_selfPeersBySipPlugin;
mutable QPixmap* m_avatar;

View File

@ -0,0 +1,57 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WeakPeerHash_p.h"
#include "PeerInfo.h"
#define WEAKPEERHASH_KEY "WeakPeerHashKey"
WeakPeerHash::WeakPeerHash(QObject *parent)
: QObject(parent)
, d_ptr( new WeakPeerHashPrivate( this ) )
{
}
WeakPeerHash::WeakPeerHash( const WeakPeerHash &hash )
: QObject( hash.parent() )
, d_ptr( new WeakPeerHashPrivate( this ) )
{
d_func()->hash = hash.hash();
}
void
WeakPeerHash::insert(const QString &key, const Tomahawk::peerinfo_ptr &value)
{
value->setProperty( WEAKPEERHASH_KEY, key );
connect( value.data(), SIGNAL( destroyed( QObject* ) ), SLOT( remove( QObject* ) ) );
d_func()->hash.insert( key, value.toWeakRef() );
}
const QHash<QString, Tomahawk::peerinfo_wptr>&
WeakPeerHash::hash()
{
return d_func()->hash;
}
void
WeakPeerHash::remove( QObject *value )
{
const QString key = value->property( WEAKPEERHASH_KEY ).toString();
d_func()->hash.remove( key );
}

View File

@ -0,0 +1,47 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEAKPEERHASH_H
#define WEAKPEERHASH_H
#include "Typedefs.h"
#include <QObject>
class WeakPeerHashPrivate;
class WeakPeerHash : public QObject
{
Q_OBJECT
public:
WeakPeerHash( QObject *parent = 0 );
WeakPeerHash( const WeakPeerHash& hash );
void insert( const QString& key, const Tomahawk::peerinfo_ptr& value );
const QHash< QString, Tomahawk::peerinfo_wptr>& hash();
signals:
private slots:
void remove( QObject* value );
private:
Q_DECLARE_PRIVATE( WeakPeerHash )
WeakPeerHashPrivate* d_ptr;
};
#endif // WEAKPEERHASH_H

View File

@ -0,0 +1,39 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEAKPEERHASH_P_H
#define WEAKPEERHASH_P_H
#include "WeakPeerHash.h"
class WeakPeerHashPrivate
{
public:
WeakPeerHashPrivate( WeakPeerHash* q )
: q_ptr ( q )
{
}
WeakPeerHash* q_ptr;
Q_DECLARE_PUBLIC ( WeakPeerHash )
private:
QHash< QString, Tomahawk::peerinfo_wptr > hash;
};
#endif // WEAKPEERHASH_P_H