Qt 4.8
Classes | Public Functions | Public Variables | Properties | List of all members
QHostInfoCache Class Reference

#include <qhostinfo_p.h>

Classes

struct  QHostInfoCacheElement
 

Public Functions

void clear ()
 
QHostInfo get (const QString &name, bool *valid)
 
bool isEnabled ()
 
void put (const QString &name, const QHostInfo &info)
 
 QHostInfoCache ()
 
void setEnabled (bool e)
 

Public Variables

const int max_age
 

Properties

QCache< QString, QHostInfoCacheElementcache
 
bool enabled
 
QMutex mutex
 

Detailed Description

Definition at line 141 of file qhostinfo_p.h.

Constructors and Destructors

◆ QHostInfoCache()

QHostInfoCache::QHostInfoCache ( )

Definition at line 765 of file qhostinfo.cpp.

765  : max_age(60), enabled(true), cache(128)
766 {
767 #ifdef QT_QHOSTINFO_CACHE_DISABLED_BY_DEFAULT
768  enabled = false;
769 #endif
770 }
QCache< QString, QHostInfoCacheElement > cache
Definition: qhostinfo_p.h:159
const int max_age
Definition: qhostinfo_p.h:145

Functions

◆ clear()

void QHostInfoCache::clear ( )

Definition at line 819 of file qhostinfo.cpp.

Referenced by QHostInfoLookupManager::clear().

820 {
821  QMutexLocker locker(&this->mutex);
822  cache.clear();
823 }
QCache< QString, QHostInfoCacheElement > cache
Definition: qhostinfo_p.h:159
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ get()

QHostInfo QHostInfoCache::get ( const QString name,
bool *  valid 
)

Definition at line 785 of file qhostinfo.cpp.

Referenced by QHostInfo::lookupHost(), qt_qhostinfo_lookup(), and QHostInfoRunnable::run().

786 {
787  QMutexLocker locker(&this->mutex);
788 
789  *valid = false;
790  if (cache.contains(name)) {
791  QHostInfoCacheElement *element = cache.object(name);
792  if (element->age.elapsed() < max_age*1000)
793  *valid = true;
794  return element->info;
795 
796  // FIXME idea:
797  // if too old but not expired, trigger a new lookup
798  // to freshen our cache
799  }
800 
801  return QHostInfo();
802 }
QCache< QString, QHostInfoCacheElement > cache
Definition: qhostinfo_p.h:159
The QHostInfo class provides static functions for host name lookups.
Definition: qhostinfo.h:58
const int max_age
Definition: qhostinfo_p.h:145
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ isEnabled()

bool QHostInfoCache::isEnabled ( )

Definition at line 772 of file qhostinfo.cpp.

Referenced by QHostInfo::lookupHost(), qt_qhostinfo_lookup(), and QHostInfoRunnable::run().

773 {
774  return enabled;
775 }

◆ put()

void QHostInfoCache::put ( const QString name,
const QHostInfo info 
)

Definition at line 804 of file qhostinfo.cpp.

Referenced by QHostInfo::fromName(), QHostInfoPrivate::fromName(), and QHostInfoRunnable::run().

805 {
806  // if the lookup failed, don't cache
807  if (info.error() != QHostInfo::NoError)
808  return;
809 
810  QHostInfoCacheElement* element = new QHostInfoCacheElement();
811  element->info = info;
812  element->age = QElapsedTimer();
813  element->age.start();
814 
815  QMutexLocker locker(&this->mutex);
816  cache.insert(name, element); // cache will take ownership
817 }
static mach_timebase_info_data_t info
HostInfoError error() const
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError...
Definition: qhostinfo.cpp:413
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
QCache< QString, QHostInfoCacheElement > cache
Definition: qhostinfo_p.h:159
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

◆ setEnabled()

void QHostInfoCache::setEnabled ( bool  e)

Definition at line 779 of file qhostinfo.cpp.

Referenced by qt_qhostinfo_enable_cache().

780 {
781  enabled = e;
782 }

Properties

◆ cache

QCache<QString,QHostInfoCacheElement> QHostInfoCache::cache
private

Definition at line 159 of file qhostinfo_p.h.

Referenced by clear(), get(), and put().

◆ enabled

bool QHostInfoCache::enabled
private

Definition at line 154 of file qhostinfo_p.h.

Referenced by isEnabled(), QHostInfoCache(), and setEnabled().

◆ max_age

const int QHostInfoCache::max_age

Definition at line 145 of file qhostinfo_p.h.

Referenced by get().

◆ mutex

QMutex QHostInfoCache::mutex
private

Definition at line 160 of file qhostinfo_p.h.

Referenced by clear(), get(), and put().


The documentation for this class was generated from the following files: