[Qt-interest] Problem using QCache

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Tue Jul 20 16:24:47 CEST 2010


Hi All,

I'm seeing strange results  (or maybe expected, but still confusing to
me) while using QCache.

I have a program which fetches some data from a remote server. In
order to improve performance I fetch the data in pages and keep the
next and previous ones in my QCache object.

The page objects is my custom class which holds some data. Since I
want to cache the next and previous page data, I set the max cost of
my cache to 3 and each time the user moves from current page to the
next/previous one, the appropriate pages are fetched and kept in the
cache. Each page inserted in the cache has a cost of 1.

I noticed a problem, wherein the QCache object deleted an object other
than what I was expecting. I'll illustrate the scenario below:

* My current cache has pages 2,3 and 4. Current page is 3.
* User requests page 4.
* I serve page 4 and prefetch and insert page 5 in the cache. So now
cache has 3,4 and 5  (current page is 4) and I see that page 2 is
deleted from the cache.
* Now user requests page 3.
* I server page 3 and prefetch page 2 (so that cache will have 2,3 and 4).

Now starts the problem. When page 2 is inserted I see that page 4 is
removed from the cache! Shouldn't it have been page 5?

The QT doc states:

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.

In my case, since all pages have equal cost and since page 5 was never
accessed (I simply did an "insert()" but never accessed the object
with either "object(key)" or "operator[key]") shouldn't that have been
the candidate for deletion rather than 4 which was accessed recently?

Can someone let me know if this is expected or not?

Regards,
-mandeep



More information about the Qt-interest-old mailing list