QCache Class

template <typename Key, typename T> class QCache

The QCache class is a template class that provides a cache. More...

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

Note: All functions in this class are reentrant.

Public Functions

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

Detailed Description

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 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, which should bear some approximate relationship to the amount of memory taken by the object. When the sum of all objects' costs (totalCost()) exceeds the cache's limit (maxCost()), QCache starts deleting objects in the cache to keep under the limit, starting with less recently accessed objects.

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 object is owned by the QCache, which may delete it at any time to make room for other objects.

To look up objects in the cache, use 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 nullptr.

If you want to remove an object from the cache for a particular key, call remove(). This will also delete the object. If you want to remove an object from the cache without the QCache deleting it, use take().

See also QPixmapCache, QHash, and QMap.

Member Function Documentation

QCache::QCache(qsizetype maxCost = 100)

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

QCache::~QCache()

Destroys the cache. Deletes all the objects in the cache.

void QCache::clear()

Deletes all the objects in the cache.

See also remove() and take().

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

Returns true if the cache contains an object associated with key key; otherwise returns false.

See also take() and remove().

qsizetype QCache::count() const

Same as size().

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

Inserts 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 is greater than maxCost(), the object will be deleted immediately.

The function returns true if the object was inserted into the cache; otherwise it returns false.

See also take() and remove().

bool QCache::isEmpty() const

Returns true if the cache contains no objects; otherwise returns false.

See also size().

QList<Key> QCache::keys() const

Returns a list of the keys in the cache.

qsizetype QCache::maxCost() const

Returns the maximum allowed total cost of the cache.

See also setMaxCost() and totalCost().

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

Returns the object associated with key key, or nullptr if the key does not exist in the cache.

Warning: The returned object is owned by QCache and may be deleted at any time.

See also take() and remove().

bool QCache::remove(const Key &key)

Deletes the object associated with key key. Returns true if the object was found in the cache; otherwise returns false.

See also take() and clear().

void QCache::setMaxCost(qsizetype 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.

See also maxCost() and totalCost().

qsizetype QCache::size() const

Returns the number of objects in the cache.

See also 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.

The ownership of the returned object is passed to the caller.

See also remove().

qsizetype QCache::totalCost() const

Returns the total cost of the objects 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().

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

Returns the object associated with key key, or nullptr if the key does not exist in the cache.

This is the same as object().

Warning: The returned object is owned by QCache and may be deleted at any time.