Пример #1
0
int thp::LayerLRUCache::add(std::tr1::shared_ptr<Bundle> sp)
{
	lockForWrite();

	// 检查资源已经存在
	QString qsName = sp->getName();
	QHash<QString,int>::iterator it = m_hmapResources.find(qsName);
	if(it != m_hmapResources.end())
	{
		unlock(false);
		return 0;
	}

	if(m_unUsedKBCount < m_unMaxKBCount)
	{
		// 加入资源到lru头部
		m_hmapResources.insert(qsName, 0);
		m_listHotColdResources.push_front(sp);

#ifdef _THP_TJ
		unsigned int nbKb = sp->getMaxKB();
		QString qsInfo = QString( "%0,%1,%2" ).arg( GB("LRU") ).arg( GB(sp->getPath()) ).arg( nbKb );
		m_pLogWriter->debugLog(qsInfo);
#endif

		m_unUsedKBCount += sp->getMaxKB();
		unlock(false);
		return 0;
	}
	
	while(m_unUsedKBCount > m_unMaxKBCount)
	{
		sPtr sp = m_listHotColdResources.back();
		while( sp->getTemperature() > 2)
		{
			sp->setTemperature(0);
			m_listHotColdResources.move_head_forward();
			continue;
		}

		if( m_unUsedKBCount < sp->getMaxKB() )
		{
			m_pLogWriter->errorLog("LRU错误");
			break;
		}

		m_unUsedKBCount -= sp->getMaxKB();
	
		m_listHotColdResources.pop_back();
		m_hmapResources.remove(qsName);
	}

	// 加入资源
	m_listHotColdResources.insert_n(m_listHotColdResources.size()/2, sp);
	m_hmapResources.insert(qsName, 0);

	// 维护占用值
	m_unUsedKBCount += sp->getMaxKB();

#ifdef _THP_TJ
	// CLASS, MAX, CURRENT, BUNDLE个数
	QString qsInfo = QString( GB("%0,%1,%2,%3") ).arg( GB("LRUSTATUS") ).arg( m_unMaxKBCount ).arg( m_unUsedKBCount ).arg( m_listHotColdResources.size() );
	m_pLogWriter->debugLog(qsInfo);
#endif

	unlock(false);
	return 0;
}