- Hands-On High Performance Programming with Qt 5
- Marek Krajewski
- 167字
- 2021-07-02 13:54:08
QCache
This lesser known Qt container is implemented as a hash table for quick lookup times, but it additionally implements a least recently used (LRU) caching strategy, purging the least used elements automatically to make room for new ones.
When inserting an object into QCache, you can specify the cost parameter which should be some approximate estimation of that object's size. When a sum of all objects' costs exceeds the cache limit (maxCost(), by default 100), the cache starts deleting the less recently accessed objects to make room for new ones.
The second Qt class providing cache implementation is QContiguousCache. It adds a requirement that the elements within the cache are stored in a contiguous manner. It was developed to provide efficient caching of items for display in user interface views. Differently from the general QCache class, it doesn't implement the LRU strategy, but rather purges items from the opposite side of the cache relative to the position where the current item was appended or prepended.