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 * | 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 |
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.
要在緩存中查找對象,使用 object () 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.
這如同 object ().
警告: 返迴的對象歸 QCache 且可以在任何時候被刪除。