The QCache class is a template class that provides a cache. 更多...
头: | #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 * | 对象 (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 |
The QCache class is a template class that provides a cache.
QCache <Key, T> defines a cache that stores objects of type T associated with keys of type Key. For example, here's the definition of a cache that stores objects of type Employee associated with an integer key:
QCache<int, Employee> cache;
这里是如何将对象插入缓存:
Employee *employee = new Employee; employee->setId(37); employee->setName("Richard Schmit"); ... cache.insert(employee->id(), employee);
The advantage of using QCache over some other key-based data structure (such as QMap or QHash ) is that QCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary. When inserting an object into the cache, you can specify a cost ,应该与对象所占用的内存量有一些近似关系。当所有对象的开销之和 ( totalCost ()) 超过缓存限制 ( maxCost ()), QCache starts deleting objects in the cache to keep under the limit, starting with less recently accessed objects.
默认情况下, QCache 's maxCost () is 100. You can specify a different value in the QCache 构造函数:
QCache<int, MyDataStructure> cache(5000);
每次调用 insert (), you can specify a cost as third argument (after the key and a pointer to the object to insert). After the call, the inserted object is owned by the QCache , which may delete it at any time to make room for other objects.
要在缓存中查找对象,使用 对象 () or operator[](). This function looks up an object by its key, and returns either a pointer to the cached object (which is owned by the cache) or 0.
若想要从缓存移除特定键对象,调用 remove (). This will also delete the object. If you want to remove an object from the cache without the QCache deleting it, use take ().
另请参阅 QPixmapCache , QHash ,和 QMap .
Constructs a cache whose contents will never have a total cost greater than maxCost .
销毁缓存。删除缓存中的所有对象。
删除缓存中的所有对象。
返回
true
若缓存包含的对象关联键
key
;否则返回
false
.
如同 size ().
插入 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
.
返回
true
若缓存不包含对象;否则返回
false
.
另请参阅 size ().
返回缓存中的键列表。
返回缓存的最大允许总开销。
另请参阅 setMaxCost () 和 totalCost ().
返回的对象关联键 key , or 0 if the key does not exist in the cache.
警告: 返回的对象归 QCache 且可以在任何时候被删除。
删除的对象关联键
key
。返回
true
若对象在缓存中被找到;否则返回
false
.
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 ().
返回缓存中的对象数。
另请参阅 isEmpty ().
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 ().
返回缓存中对象的总开销。
此值通常低于 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 ().
返回的对象关联键 key , or 0 if the key does not exist in the cache.
这如同 对象 ().
警告: 返回的对象归 QCache 且可以在任何时候被删除。