• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KDECore

Public Types | Public Member Functions | Static Public Member Functions

KSharedDataCache Class Reference

A simple data cache which uses shared memory to quickly access data stored on disk. More...

#include <kshareddatacache.h>

List of all members.

Public Types

enum  EvictionPolicy { NoEvictionPreference = 0, EvictLeastRecentlyUsed, EvictLeastOftenUsed, EvictOldest }

Public Member Functions

 KSharedDataCache (const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize=0)
 ~KSharedDataCache ()
void clear ()
bool contains (const QString &key) const
EvictionPolicy evictionPolicy () const
bool find (const QString &key, QByteArray *destination) const
unsigned freeSize () const
bool insert (const QString &key, const QByteArray &data)
void setEvictionPolicy (EvictionPolicy newPolicy)
void setTimestamp (unsigned newTimestamp)
unsigned timestamp () const
unsigned totalSize () const

Static Public Member Functions

static void deleteCache (const QString &cacheName)

Detailed Description

A simple data cache which uses shared memory to quickly access data stored on disk.

This class is meant to be used with KImageCache and similar classes but can be used directly if used with care.

Example usage:

 QString loadTranslatedDocument(KSharedDataCache *cache) {

   // Find the data
   QByteArray document;

   if (!cache->find("translated-doc-template", &document)) {
     // Entry is not cached, manually generate and then add to cache.
     document = translateDocument(globalTemplate());
     cache->insert(document);
   }

   // Don't forget to encode/decode properly
   return QString::fromUtf8(document);
 }
Author:
Michael Pyne <mpyne@kde.org>
See also:
KImageCache
Since:
4.5

Definition at line 58 of file kshareddatacache.h.


Member Enumeration Documentation

enum KSharedDataCache::EvictionPolicy
Enumerator:
NoEvictionPreference 
EvictLeastRecentlyUsed 
EvictLeastOftenUsed 
EvictOldest 

Definition at line 83 of file kshareddatacache.h.


Constructor & Destructor Documentation

KSharedDataCache::KSharedDataCache ( const QString &  cacheName,
unsigned  defaultCacheSize,
unsigned  expectedItemSize = 0 
)

Attaches to a shared cache, creating it if necessary.

If supported, this data cache will be shared across all processes using this cache (with subsequent memory savings). If shared memory is unsupported or a failure occurs, caching will still be supported, but only in the same process, and only using the same KSharedDataCache object.

Parameters:
cacheNameName of the cache to use/share.
defaultCacheSizeAmount of data to be able to store, in bytes. The actual size will be slightly larger on disk due to accounting overhead. If the cache already existed then it will not be resized. For this reason you should specify some reasonable size.
expectedItemSizeThe average size of an item that would be stored in the cache, in bytes. Choosing an average size of zero bytes causes KSharedDataCache to use whatever it feels is the best default for the system.

Definition at line 1295 of file kshareddatacache.cpp.

KSharedDataCache::~KSharedDataCache ( )

Definition at line 1302 of file kshareddatacache.cpp.


Member Function Documentation

void KSharedDataCache::clear ( )

Removes all entries from the cache.

Definition at line 1494 of file kshareddatacache.cpp.

bool KSharedDataCache::contains ( const QString &  key) const

Returns true if the cache currently contains the image for the given filename.

NOTE: Calling this function is threadsafe, but it is in general not possible to guarantee the image stays cached immediately afterwards, so if you need the result use find().

Definition at line 1503 of file kshareddatacache.cpp.

void KSharedDataCache::deleteCache ( const QString &  cacheName) [static]

Removes the underlying file from the cache.

Note that this is *all* that this function does. The shared memory segment is still attached and will still contain all the data until all processes currently attached remove the mapping.

In order to remove the data see clear().

Definition at line 1513 of file kshareddatacache.cpp.

KSharedDataCache::EvictionPolicy KSharedDataCache::evictionPolicy ( ) const
Returns:
The removal policy in use by the shared cache.
See also:
EvictionPolicy

Definition at line 1544 of file kshareddatacache.cpp.

bool KSharedDataCache::find ( const QString &  key,
QByteArray *  destination 
) const

Returns the data in the cache named by key (even if it's some other process's data named with the same key!), stored in destination.

If there is no entry named by key then destination is left unchanged. The return value is used to tell what happened.

If you simply want to verify whether an entry is present in the cache then see contains().

Parameters:
keyThe key to find in the cache.
destinationIs set to the value of key in the cache if key is present, left unchanged otherwise.
Returns:
true if key was present in the cache (destination will also be updated), false if key was not present (destination will be unchanged).

Definition at line 1456 of file kshareddatacache.cpp.

unsigned KSharedDataCache::freeSize ( ) const

Returns the amount of free space in the cache, in bytes.

Due to implementation details it is possible to still not be able to fit an entry in the cache at any given time even if it is smaller than the amount of space remaining.

Definition at line 1534 of file kshareddatacache.cpp.

bool KSharedDataCache::insert ( const QString &  key,
const QByteArray &  data 
)

Attempts to insert the entry data into the shared cache, named by key, and returns true only if successful.

Note that even if the insert was successful, that the newly added entry may be evicted by other processes contending for the cache.

Definition at line 1317 of file kshareddatacache.cpp.

void KSharedDataCache::setEvictionPolicy ( KSharedDataCache::EvictionPolicy  newPolicy)

Sets the entry removal policy for the shared cache to newPolicy.

The default is EvictionPolicy::NoEvictionPreference.

See also:
EvictionPolicy

Definition at line 1553 of file kshareddatacache.cpp.

void KSharedDataCache::setTimestamp ( unsigned  newTimestamp)

Sets the shared timestamp of the cache.

Timestamping is supported to allow applications to more effectively "version" the data stored in the cache. However, the timestamp is shared with all applications using the cache so you should always be prepared for invalid timestamps.

When the cache is first created (note that this is different from attaching to an existing shared cache on disk), the cache timestamp is initialized to the time returned by time(2). KSharedDataCache will not update the timestamp again, it is only updated through this method.

Example:

 QImage loadCachedImage(const QString &key)
 {
     // Check timestamp
     if (m_sharedCache->timestamp() < m_currentThemeTimestamp) {
         // Cache is stale, clean it out.
         m_sharedCache->clear();
         m_sharedCache->setTimestamp(m_currentThemeTimestamp);
     }

     // Check cache and load image as usual...
 }
Parameters:
newTimestampThe new timestamp to mark the shared cache with.
See also:
timestamp()
Since:
4.6

Definition at line 1569 of file kshareddatacache.cpp.

unsigned KSharedDataCache::timestamp ( ) const
Returns:
The shared timestamp of the cache. The interpretation of the timestamp returned is up to the application. KSharedDataCache will initialize the timestamp to the time returned by time(2) on cache creation, but KSharedDataCache will not touch the timestamp again.
See also:
setTimestamp()
Since:
4.6

Definition at line 1560 of file kshareddatacache.cpp.

unsigned KSharedDataCache::totalSize ( ) const

Returns the usable cache size in bytes.

The actual amount of memory used will be slightly larger than this to account for required accounting overhead.

Definition at line 1524 of file kshareddatacache.cpp.


The documentation for this class was generated from the following files:
  • kshareddatacache.h
  • kshareddatacache.cpp
  • kshareddatacache_win.cpp

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal