QCache 类

template <typename Key, typename T> class QCache

QCache 类是提供缓存的模板类。 更多...

头: #include <QCache>
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共函数

QCache (int maxCost = 100)
~QCache ()
void clear ()
bool contains (const Key & key ) const
int count () const
bool insert (const Key & key , T * object , int cost = 1)
bool isEmpty () const
QList<Key> keys () const
int maxCost () const
T * object (const Key & key ) const
bool remove (const Key & key )
void setMaxCost (int cost )
int size () const
T * take (const Key & key )
int totalCost () const
T * operator[] (const Key & key ) const

详细描述

QCache<Key, T> 定义存储 Key 类型键关联的 T 类型对象的缓存。例如,这里是存储整数键关联的 Employee 类型对象的缓存定义:

QCache<int, Employee> cache;
					

这里是如何将对象插入缓存:

Employee *employee = new Employee;
employee->setId(37);
employee->setName("Richard Schmit");
...
cache.insert(employee->id(), employee);
					

使用 QCache 优于某些其它基于键的数据结构 (譬如 QMap or QHash ),QCache 自动获取插入缓存中对象的所有权,并删除它们以便为新对象腾出空间,若有必要。在将对象插入缓存时,可以指定 cost ,应该与对象所占用的内存量有一些近似关系。当所有对象的开销之和 ( totalCost ()) 超过缓存限制 ( maxCost ()),QCache 开始删除缓存中的对象以保持在限制之下,从最近访问较少的对象开始。

默认情况下,QCache 的 maxCost () 为 100。可以在 QCache 构造函数中指定不同值:

QCache<int, MyDataStructure> cache(5000);
					

每次调用 insert (),可以将开销指定作为第 3 自变量 (在键和指向要插入对象的指针之后)。调用后,插入对象归 QCache 所有,QCache 可以随时删除它以便为其它对象腾出空间。

要在缓存中查找对象,使用 object () 或 operator[]()。此函数通过键查找对象,并返回指向缓存对象 (由缓存拥有) 的指针或 nullptr .

若想要从缓存移除特定键对象,调用 remove ()。这还会删除对象。若想要从缓存中移除对象而不被 QCache 删除,使用 take ().

另请参阅 QPixmapCache , QHash ,和 QMap .

成员函数文档编制

QCache:: QCache ( int maxCost = 100)

Constructs a cache whose contents will never have a total cost greater than maxCost .

QCache:: ~QCache ()

销毁缓存。删除缓存中的所有对象。

void QCache:: clear ()

删除缓存中的所有对象。

另请参阅 remove () 和 take ().

bool QCache:: contains (const Key & key ) const

返回 true 若缓存包含的对象关联键 key ;否则返回 false .

另请参阅 take () 和 remove ().

int QCache:: count () const

如同 size ().

bool QCache:: insert (const Key & key , T * object , int cost = 1)

插入 object into the cache with key key and associated cost cost . Any object with the same key already in the cache will be removed.

After this call, object is owned by the QCache and may be deleted at any time. In particular, if cost 大于 maxCost (), the object will be deleted immediately.

函数返回 true 若对象被插入缓存;否则它返回 false .

另请参阅 take () 和 remove ().

bool QCache:: isEmpty () const

返回 true 若缓存不包含对象;否则返回 false .

另请参阅 size ().

QList < Key > QCache:: keys () const

返回缓存中的键列表。

int QCache:: maxCost () const

返回缓存的最大允许总开销。

另请参阅 setMaxCost () 和 totalCost ().

T *QCache:: object (const Key & key ) const

返回的对象关联键 key ,或 nullptr 若键未存在于缓存中。

警告: 返回的对象归 QCache 且可以随时被删除。

另请参阅 take () 和 remove ().

bool QCache:: remove (const Key & key )

删除的对象关联键 key 。返回 true 若对象在缓存中被找到;否则返回 false .

另请参阅 take () 和 clear ().

void QCache:: setMaxCost ( int cost )

Sets the maximum allowed total cost of the cache to cost . If the current total cost is greater than cost , some objects are deleted immediately.

另请参阅 maxCost () 和 totalCost ().

int QCache:: size () const

返回缓存中的对象数。

另请参阅 isEmpty ().

T *QCache:: take (const Key & key )

Takes the object associated with key key out of the cache without deleting it. Returns a pointer to the object taken out, or 0 if the key does not exist in the cache.

返回对象的所有权被传递给调用者。

另请参阅 remove ().

int QCache:: totalCost () const

返回缓存中对象的总开销。

此值通常低于 maxCost (),但 QCache makes an exception for Qt's 隐式共享 classes. If a cached object shares its internal data with another instance, QCache may keep the object lying around, possibly contributing to making totalCost() larger than maxCost ().

另请参阅 setMaxCost ().

T *QCache:: operator[] (const Key & key ) const

返回的对象关联键 key ,或 nullptr 若键未存在于缓存中。

这如同 object ().

警告: 返回的对象归 QCache 且可以随时被删除。