[Qt-interest] Problem using QCache

Andre Somers andre at familiesomers.nl
Tue Jul 20 17:05:33 CEST 2010


Op 20-7-2010 16:24, Mandeep Sandhu schreef:
> 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?
>    
On first sight: yes, that would be expected IMHO. Page 5 was inserted 
after page 4 has been accessed. Hence, page 5 is 'younger' than page 4, 
and thus is page 4 the prime candidate for deletion. I think you can 
avoid that by simply accessing the currently displayed page again after 
inserting surrounding pages.
What I am wondering though: why do you bother with using QCache at all? 
The scheme is so simple, and the number of pages so low, that I'd say it 
would be trival to implement yourself in your page fetching object.

André




More information about the Qt-interest-old mailing list