QCache Class

template <typename Key, typename T> class QCache

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

Header: #include <QCache>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数都是可重入的

公共函数

QCache(qsizetype maxCost = 100)
~QCache()
void clear()
bool contains(const Key &key) const
qsizetype count() const
bool insert(const Key &key, T *object, qsizetype cost = 1)
bool isEmpty() const
QList<Key> keys() const
qsizetype maxCost() const
T *object(const Key &key) const
bool remove(const Key &key)
void setMaxCost(qsizetype cost)
qsizetype size() const
T *take(const Key &key)
qsizetype 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);

与其他基于键的数据结构(如QMapQHash )相比,使用 QCache 的优势在于,QCache 会自动获取插入缓存的对象的所有权,并在必要时删除这些对象,为新对象腾出空间。在将对象插入缓存时,可以指定一个成本,该成本应与对象占用的内存量有某种近似关系。当所有对象的成本总和(totalCost()) 超过缓存的限制(maxCost()) 时,QCache 就会开始删除缓存中的对象,以保持在限制范围内,并从最近访问次数较少的对象开始删除。

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

QCache<int, MyDataStructure> cache(5000);

每次调用insert() 时,都可以指定一个成本作为第三个参数(在键和要插入对象的指针之后)。调用后,插入的对象归 QCache 所有,QCache 可以随时删除它,为其他对象腾出空间。

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

如果要从缓存中删除某个键的对象,请调用remove() 。这也会删除对象。如果想在 QCache 不删除对象的情况下从缓存中删除对象,请使用take() 。

另请参阅 QPixmapCache,QHash, 和QMap

成员函数文档

[explicit noexcept] QCache::QCache(qsizetype maxCost = 100)

构建一个缓存,其内容的总成本永远不会大于maxCost

QCache::~QCache()

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

[noexcept(...)] void QCache::clear()

删除缓存中的所有对象。

注: std::is_nothrow_destructible_v<Node>true 时,此函数为 noexcept。

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

[noexcept] bool QCache::contains(const Key &key) const

如果缓存中包含与 keykey 相关联的对象,则返回true ;否则返回false

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

[noexcept] qsizetype QCache::count() const

size() 相同。

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

object 插入缓存,其关键字为key ,相关费用为cost 。缓存中已存在的具有相同键值的对象将被删除。

调用后,objectQCache 所有,可随时删除。特别是,如果cost 大于maxCost() ,该对象将立即被删除。

如果对象已插入缓存,函数返回true ;否则返回false

另请参见 take() 和remove()。

[noexcept] bool QCache::isEmpty() const

如果缓存中没有对象,则返回true ;否则返回false

另请参阅 size() 。

QList<Key> QCache::keys() const

返回缓存中的键列表。

[noexcept] qsizetype QCache::maxCost() const

返回缓存允许的最大总费用。

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

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

返回与密钥key 相关联的对象,如果缓存中不存在该密钥,则返回nullptr

警告: 返回的对象归QCache 所有,可随时删除。

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

[noexcept(...)] bool QCache::remove(const Key &key)

删除与 keykey 相关联的对象。如果在缓存中找到对象,则返回true ;否则返回false

注: std::is_nothrow_destructible_v<Node>true 时,此函数为 noexcept。

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

[noexcept(...)] void QCache::setMaxCost(qsizetype cost)

将缓存允许的最大总代价设置为cost 。如果当前总代价大于cost ,则会立即删除某些对象。

注意: std::is_nothrow_destructible_v<Node>true 时,此函数为 noexcept。

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

[noexcept] qsizetype QCache::size() const

返回缓存中的对象数量。

另请参阅 isEmpty().

[noexcept(...)] T *QCache::take(const Key &key)

将与key 关键字相关联的对象从缓存中取出,但不删除。如果缓存中不存在键,则返回指向取出对象的指针或 0。

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

注: std::is_nothrow_destructible_v<Key>true 时,此函数为 noexcept。

另请参阅 remove() 。

[noexcept] qsizetype QCache::totalCost() const

返回缓存中对象的总成本。

该值通常低于maxCost() ,但QCache 对 Qt 的隐式共享类例外。如果缓存对象与另一个实例共享内部数据,QCache 可能会保留该对象,从而可能导致 totalCost() 大于maxCost()。

另请参见 setMaxCost()。

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

返回与密钥key 相关联的对象,如果缓存中不存在该密钥,则返回nullptr

这与object() 相同。

警告: 返回的对象归QCache 所有,可随时删除。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.