Commit 45bafea1 authored by Pavel Vainerman's avatar Pavel Vainerman

(UInterface): убрал использование "дорогого" timestamp в кэше, перешёл на подсчёт обращений.

parent b6bccc10
......@@ -256,12 +256,12 @@ class UInterface
class CacheOfResolve
{
public:
CacheOfResolve(unsigned int maxsize, int cleantime):
MaxSize(maxsize), CleanTime(cleantime){};
CacheOfResolve( unsigned int maxsize, int cleancount=20 ):
MaxSize(maxsize), minCallCount(cleancount){};
~CacheOfResolve(){};
UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const throw(UniSetTypes::NameNotFound);
void cache( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node, UniSetTypes::ObjectVar ptr )const;
void cache( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node, UniSetTypes::ObjectVar ptr ) const;
void erase( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
inline void setMaxSize( unsigned int ms )
......@@ -269,8 +269,6 @@ class UInterface
MaxSize = ms;
};
// void setCleanTime();
protected:
CacheOfResolve(){};
......@@ -283,52 +281,25 @@ class UInterface
mcache.clear();
};
/*!
\todo можно добавить поле CleanTime для каждой ссылки отдельно...
*/
struct Info
{
Info( UniSetTypes::ObjectVar ptr, time_t tm=0 ):
ptr(ptr)
{
if(!tm)
timestamp = time(NULL);
}
Info():
ptr(NULL), timestamp(0){};
Info( UniSetTypes::ObjectVar ptr ):ptr(ptr),ncall(0){}
Info():ptr(NULL),ncall(0){}
UniSetTypes::ObjectVar ptr;
time_t timestamp; // время последнего обращения
unsigned long ncall; // счётчик обращений
bool operator<( const CacheOfResolve::Info& rhs ) const
{
return this->timestamp < rhs.timestamp;
return this->ncall > rhs.ncall;
}
};
typedef std::map<int, Info> CacheMap;
mutable CacheMap mcache;
mutable UniSetTypes::uniset_rwmutex cmutex;
unsigned int MaxSize; /*!< максимальный размер кэша */
unsigned int CleanTime; /*!< период устаревания ссылок [мин] */
/*
// В последствии написать функцию для использования
// remove_if
typedef std::pair<int, Info> CacheItem;
// функция-объект для поиска устаревших(по времени) ссылок
struct OldRef_eq: public unary_function<CacheItem, bool>
{
OldRef_eq(time_t tm):tm(tm){}
bool operator()( const CacheItem& inf ) const
{
return inf.timestamp < tm;
}
time_t tm;
};
*/
unsigned int minCallCount; /*!< минимальное количество вызовов, меньше которого ссылка считается устаревшей */
};
void initBackId( UniSetTypes::ObjectId backid );
......
......@@ -44,7 +44,7 @@ UInterface::UInterface( const UniSetTypes::Configuration* _uconf ):
rep(_uconf),
myid(UniSetTypes::DefaultObjectId),
orb(CORBA::ORB::_nil()),
rcache(100,5),
rcache(100,20),
oind(_uconf->oind),
uconf(_uconf)
{
......@@ -55,7 +55,7 @@ UInterface::UInterface( const ObjectId backid, CORBA::ORB_var orb, ObjectIndex*
rep(UniSetTypes::conf),
myid(backid),
orb(orb),
rcache(200,120),
rcache(200,40),
oind(_oind),
uconf(UniSetTypes::conf)
{
......@@ -1065,14 +1065,11 @@ ObjectPtr UInterface::CacheOfResolve::resolve( const ObjectId id, const ObjectId
{
UniSetTypes::uniset_rwmutex_rlock l(cmutex);
//#warning Временно отключён кэш
// throw UniSetTypes::NameNotFound();
CacheMap::iterator it = mcache.find( key(id,node) );
if( it == mcache.end() )
throw UniSetTypes::NameNotFound();
it->second.timestamp = time(NULL); // фиксируем время последнего обращения
it->second.ncall++;
// т.к. функция возвращает указатель
// и тот кто вызывает отвечает за освобождение памяти
......@@ -1087,15 +1084,16 @@ ObjectPtr UInterface::CacheOfResolve::resolve( const ObjectId id, const ObjectId
void UInterface::CacheOfResolve::cache( const ObjectId id, const ObjectId node, ObjectVar ptr ) const
{
UniSetTypes::uniset_rwmutex_wrlock l(cmutex);
UniSetTypes::KeyType k(key(id,node));
UniSetTypes::KeyType k( key(id,node) );
CacheMap::iterator it = mcache.find(k);
if( it==mcache.end() )
if( it == mcache.end() )
mcache.insert(CacheMap::value_type(k,Info(ptr)));
else
{
it->second.ptr = ptr; // CORBA::Object::_duplicate(ptr);
it->second.timestamp = time(NULL);
it->second.ncall++;
}
}
// ------------------------------------------------------------------------------------------------------------
......@@ -1105,11 +1103,9 @@ bool UInterface::CacheOfResolve::clean()
uinfo << "UI: clean cache...."<< endl;
time_t tm = time(NULL)-CleanTime*60;
// remove_if(mcache.begin(), mcache.end(),OldRef_eq(tm));
for( CacheMap::iterator it=mcache.begin(); it!=mcache.end();)
{
if( it->second.timestamp < tm )
if( it->second.ncall <= minCallCount )
mcache.erase(it++);
else
++it;
......@@ -1125,9 +1121,6 @@ bool UInterface::CacheOfResolve::clean()
void UInterface::CacheOfResolve::erase( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const
{
UniSetTypes::uniset_rwmutex_wrlock l(cmutex);
//#warning Временно отключён кэш
// return;
CacheMap::iterator it = mcache.find( key(id,node) );
if( it != mcache.end() )
mcache.erase(it);
......@@ -1156,7 +1149,7 @@ bool UInterface::isExist( const UniSetTypes::ObjectId id ) const
string nm = oind->getNameById(id);
return rep.isExist(nm);
}
catch(UniSetTypes::Exception& ex)
catch( UniSetTypes::Exception& ex )
{
// uwarn << "UI(isExist): " << ex << endl;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment