Scroll to navigation

QCache(3qt) QCache(3qt)

NAME

QCache - Template class that provides a cache based on QString keys

SYNOPSIS

#include <qcache.h>

Inherits QPtrCollection.

Public Members


QCache ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )
~QCache ()
int maxCost () const
int totalCost () const
void setMaxCost ( int m )
virtual uint count () const
uint size () const
bool isEmpty () const
virtual void clear ()
bool insert ( const QString & k, const type * d, int c = 1, int p = 0 )
bool remove ( const QString & k )
type * take ( const QString & k )
type * find ( const QString & k, bool ref = TRUE ) const
type * operator[] ( const QString & k ) const
void statistics () const

Important Inherited Members


bool autoDelete () const
void setAutoDelete ( bool enable )

DESCRIPTION

The QCache class is a template class that provides a cache based on QString keys.

A cache is a least recently used (LRU) list of cache items. Each cache item has a key and a certain cost. The sum of item costs, totalCost(), never exceeds the maximum cache cost, maxCost(). If inserting a new item would cause the total cost to exceed the maximum cost, the least recently used items in the cache are removed.

QCache is a template class. QCache<X> defines a cache that operates on pointers to X, or X*.

Apart from insert(), by far the most important function is find() (which also exists as operator[]()). This function looks up an item, returns it, and by default marks it as being the most recently used item.

There are also methods to remove() or take() an object from the cache. Calling setAutoDelete(TRUE) for a cache tells it to delete items that are removed. The default is to not delete items when they are removed (i.e., remove() and take() are equivalent).

When inserting an item into the cache, only the pointer is copied, not the item itself. This is called a shallow copy. It is possible to make the cache copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a cache and reimplement newItem() if you want deep copies.

When removing a cache item, the virtual function QPtrCollection::deleteItem() is called. The default implementation deletes the item if auto-deletion is enabled, and does nothing otherwise.

There is a QCacheIterator that can be used to traverse the items in the cache in arbitrary order.

In QCache, the cache items are accessed via QString keys, which are Unicode strings. If you want to use non-Unicode, plain 8-bit char* keys, use the QAsciiCache template. A QCache has the same performance as a QAsciiCache.

See also QCacheIterator, QAsciiCache, QIntCache, Collection Classes, and Non-GUI Classes.

MEMBER FUNCTION DOCUMENTATION

QCache::QCache ( int maxCost = 100, int size = 17, bool caseSensitive = TRUE )

Constructs a cache whose contents will never have a total cost greater than maxCost and which is expected to contain less than size items.

size is actually the size of an internal hash array; it's usually best to make it a prime number and at least 50% bigger than the largest expected number of items in the cache.

Each inserted item has an associated cost. When inserting a new item, if the total cost of all items in the cache will exceed maxCost, the cache will start throwing out the older (least recently used) items until there is enough room for the new item to be inserted.

If caseSensitive is TRUE (the default), the cache keys are case sensitive; if it is FALSE, they are case-insensitive. Case-insensitive comparison considers all Unicode letters.

QCache::~QCache ()

Removes all items from the cache and destroys it. All iterators that access this cache will be reset.

bool QPtrCollection::autoDelete () const

Returns the setting of the auto-delete option. The default is FALSE.

See also setAutoDelete().

void QCache::clear () [virtual]

Removes all items from the cache and deletes them if auto-deletion has been enabled.

All cache iterators that operate this on cache are reset.

See also remove() and take().

Reimplemented from QPtrCollection.

uint QCache::count () const [virtual]

Returns the number of items in the cache.

See also totalCost().

Reimplemented from QPtrCollection.

type * QCache::find ( const QString & k, bool ref = TRUE ) const

Returns the item associated with key k, or 0 if the key does not exist in the cache. If ref is TRUE (the default), the item is moved to the front of the least recently used list.

If there are two or more items with equal keys, the one that was inserted last is returned.

bool QCache::insert ( const QString & k, const type * d, int c = 1, int p = 0 )

Inserts the item d into the cache with key k and associated cost, c. Returns TRUE if it is successfully inserted; otherwise returns FALSE.

The cache's size is limited, and if the total cost is too high, QCache will remove old, least recently used items until there is room for this new item.

The parameter p is internal and should be left at the default value (0).

Warning: If this function returns FALSE (which could happen, e.g. if the cost of this item alone exceeds maxCost()) you must delete d yourself. Additionally, be very careful about using d after calling this function because any other insertions into the cache, from anywhere in the application or within Qt itself, could cause the object to be discarded from the cache and the pointer to become invalid.

bool QCache::isEmpty () const

Returns TRUE if the cache is empty; otherwise returns FALSE.

int QCache::maxCost () const

Returns the maximum allowed total cost of the cache.

See also setMaxCost() and totalCost().

type * QCache::operator[] ( const QString & k ) const

Returns the item associated with key k, or 0 if k does not exist in the cache, and moves the item to the front of the least recently used list.

If there are two or more items with equal keys, the one that was inserted last is returned.

This is the same as find( k, TRUE ).

See also find().

bool QCache::remove ( const QString & k )

Removes the item associated with k, and returns TRUE if the item was present in the cache; otherwise returns FALSE.

The item is deleted if auto-deletion has been enabled, i.e., if you have called setAutoDelete(TRUE).

If there are two or more items with equal keys, the one that was inserted last is removed.

All iterators that refer to the removed item are set to point to the next item in the cache's traversal order.

See also take() and clear().

void QPtrCollection::setAutoDelete ( bool enable )

Sets the collection to auto-delete its contents if enable is TRUE and to never delete them if enable is FALSE.

If auto-deleting is turned on, all the items in a collection are deleted when the collection itself is deleted. This is convenient if the collection has the only pointer to the items.

The default setting is FALSE, for safety. If you turn it on, be careful about copying the collection - you might find yourself with two collections deleting the same items.

Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item.

See also autoDelete().

Examples:

void QCache::setMaxCost ( int m )

Sets the maximum allowed total cost of the cache to m. If the current total cost is greater than m, some items are deleted immediately.

See also maxCost() and totalCost().

uint QCache::size () const

Returns the size of the hash array used to implement the cache. This should be a bit bigger than count() is likely to be.

void QCache::statistics () const

A debug-only utility function. Prints out cache usage, hit/miss, and distribution information using qDebug(). This function does nothing in the release library.

type * QCache::take ( const QString & k )

Takes the item associated with k out of the cache without deleting it, and returns a pointer to the item taken out, or 0 if the key does not exist in the cache.

If there are two or more items with equal keys, the one that was inserted last is taken.

All iterators that refer to the taken item are set to point to the next item in the cache's traversal order.

See also remove() and clear().

int QCache::totalCost () const

Returns the total cost of the items in the cache. This is an integer in the range 0 to maxCost().

See also setMaxCost().

SEE ALSO

http://doc.trolltech.com/qcache.html http://www.trolltech.com/faq/tech.html

COPYRIGHT

Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR

Generated automatically from the source code.

BUGS

If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you.

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qcache.3qt) and the Qt version (3.3.8).

2 February 2007 Trolltech AS