File indexing completed on 2024-05-12 03:54:54

0001 /*
0002     This file is part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2010 Michael Pyne <mpyne@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 /**
0010  * This is a horrifically simple implementation of KSharedDataCache that is
0011  * basically missing the "shared" part to it, for use on Windows or other platforms
0012  * that don't support POSIX.
0013  */
0014 #include "kshareddatacache.h"
0015 
0016 #include <QByteArray>
0017 #include <QCache>
0018 #include <QString>
0019 
0020 class Q_DECL_HIDDEN KSharedDataCache::Private
0021 {
0022 public:
0023     KSharedDataCache::EvictionPolicy evictionPolicy;
0024     QCache<QString, QByteArray> cache;
0025 };
0026 
0027 KSharedDataCache::KSharedDataCache(const QString &cacheName, unsigned defaultCacheSize, unsigned expectedItemSize)
0028     : d(new Private)
0029 {
0030     d->cache.setMaxCost(defaultCacheSize);
0031 
0032     Q_UNUSED(cacheName);
0033     Q_UNUSED(expectedItemSize);
0034 }
0035 
0036 KSharedDataCache::~KSharedDataCache()
0037 {
0038     delete d;
0039 }
0040 
0041 KSharedDataCache::EvictionPolicy KSharedDataCache::evictionPolicy() const
0042 {
0043     return d->evictionPolicy;
0044 }
0045 
0046 void KSharedDataCache::setEvictionPolicy(KSharedDataCache::EvictionPolicy newPolicy)
0047 {
0048     d->evictionPolicy = newPolicy;
0049 }
0050 
0051 bool KSharedDataCache::insert(const QString &key, const QByteArray &data)
0052 {
0053     return d->cache.insert(key, new QByteArray(data));
0054 }
0055 
0056 bool KSharedDataCache::find(const QString &key, QByteArray *destination) const
0057 {
0058     QByteArray *value = d->cache.object(key);
0059 
0060     if (value) {
0061         if (destination) {
0062             *destination = *value;
0063         }
0064         return true;
0065     } else {
0066         return false;
0067     }
0068 }
0069 
0070 void KSharedDataCache::clear()
0071 {
0072     d->cache.clear();
0073 }
0074 
0075 void KSharedDataCache::deleteCache(const QString &cacheName)
0076 {
0077     Q_UNUSED(cacheName);
0078 }
0079 
0080 bool KSharedDataCache::contains(const QString &key) const
0081 {
0082     return d->cache.contains(key);
0083 }
0084 
0085 unsigned KSharedDataCache::totalSize() const
0086 {
0087     return static_cast<unsigned>(d->cache.maxCost());
0088 }
0089 
0090 unsigned KSharedDataCache::freeSize() const
0091 {
0092     if (d->cache.totalCost() < d->cache.maxCost()) {
0093         return static_cast<unsigned>(d->cache.maxCost() - d->cache.totalCost());
0094     } else {
0095         return 0;
0096     }
0097 }
0098 
0099 unsigned KSharedDataCache::timestamp() const
0100 {
0101     return 0;
0102 }
0103 
0104 void KSharedDataCache::setTimestamp(unsigned newTimestamp)
0105 {
0106 }