Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The QCache class is a template class that provides a cache. More...
#include <QCache>
Note: All the functions in this class are reentrant.
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;
Here's how to insert an object in the 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 items that are inserted into the cache and deletes them to make room for new items, if necessary. When inserting an item into the cache, you can specify a cost, which should bear some approximate relationship to the amount of memory taken by the item. When the sum of all items' costs (totalCost()) exceeds the cache's limit (maxCost()), QCache starts deleting items in the cache to keep under the limit, starting with less recently accessed items.
By default, QCache's maxCost() is 100. You can specify a different value in the QCache constructor:
QCache<int, MyDataStructure> cache(5000);
Each time you call 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 item is owned by the QCache, which may delete it at any time to make room for other items.
To look up items in the cache, use find() or operator[](). This function looks up an item by its key, and returns either a pointer to the cached object (which is owned by the cache) or 0.
If you want to remove an item from the cache for a particular key, call remove(). This will also delete the item. If you want to remove an item from the cache without the QCache deleting it, use take().
See also QPixmapCache, QHash, and QMap.
Constructs a cache whose contents will never have a total cost greater than maxCost.
Destroys the cache. Deletes all the items in the cache.
Deletes all the items in the cache.
Returns true if the cache contains an item associated with key key; otherwise returns false.
Same as size().
Returns the item associated with key key, or 0 if the key does not exist in the cache.
Warning: The returned object is owned by QCache and may be deleted at any time.
Inserts the item data into the cache with key key and associated cost cost. Any item with the same key already in the cache will be removed.
After this call, the object pointed to by data is owned by the QCache and may be deleted at any time. In particular, if cost is greater than maxCost(), the object will be deleted immediately.
Returns true if the cache contains no items; otherwise returns false.
See also size().
Returns the maximum allowed total cost of the cache.
See also setMaxCost() and totalCost().
Deletes the item associated with key key. Returns true if the item was found in the cache; otherwise returns false.
Sets the maximum allowed total cost of the cache to cost. If the current total cost is greater than cost, some items are deleted immediately.
See also maxCost() and totalCost().
Returns the number of items in the cache.
See also isEmpty().
Takes the item associated with key key out of the cache without deleting it. Returns a pointer to the item taken out, or 0 if the key does not exist in the cache.
If an item pointer is returned, ownership of the item is passed to the caller.
See also remove().
Returns the total cost of the items in the cache.
This value is normally below maxCost(), but QCache makes an exception for Qt's implicitly shared 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().
See also setMaxCost().
Returns the item associated with key key, or 0 if the key does not exist in the cache.
This is the same as find().
Warning: The returned object is owned by QCache and may be deleted at any time.
Copyright © 2004 Trolltech. | Trademarks | Qt 4.0.0-tp1 |