Projekt

Allgemein

Profil

Herunterladen als
Herunterladen (2,89 KB) Statistiken
| Zweig: | Revision:
c22391b2 Christian Ehringfeld
/*
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
d84c91e7 Christian Ehringfeld
#include "cache.h"
e8d1537c Christian Ehringfeld
#include "entityhelper.h"
d84c91e7 Christian Ehringfeld
using namespace CuteEntityManager;
Cache::Cache() {
}
a873a3ba Christian Ehringfeld
QHash<QString, QWeakPointer<Entity>> Cache::getCache() const {
11bbe9a6 Christian Ehringfeld
return cache;
}

bool Cache::contains(qint64 id, const QString &classname) {
QString key = this->generateKey(id, classname);
return this->contains(key);
}

bool Cache::contains(const QString &key) {
if (!key.isEmpty()) {
return this->cache.contains(key);
}
return false;
}

a873a3ba Christian Ehringfeld
void Cache::clear() {
5ca225ed Christian Ehringfeld
this->mutex.lock();
a873a3ba Christian Ehringfeld
this->cache.clear();
5ca225ed Christian Ehringfeld
this->mutex.unlock();
a873a3ba Christian Ehringfeld
}

62427ee0 Christian Ehringfeld
void Cache::insert(QSharedPointer<Entity> &entity) {
dfaffebe Christian Ehringfeld
if (entity && entity->getId() > -1) {
abb9e8c5 Christian Ehringfeld
QString key = this->generateKey(entity->getId(),
e8d1537c Christian Ehringfeld
EntityHelper::getClassName(entity.data()));
62427ee0 Christian Ehringfeld
if (this->cache.contains(key)) {
QSharedPointer<Entity> ptr = this->cache.value(key).toStrongRef();
dfaffebe Christian Ehringfeld
if (ptr) {
5ca225ed Christian Ehringfeld
this->mutex.lock();
dfaffebe Christian Ehringfeld
ptr.swap(entity);
5ca225ed Christian Ehringfeld
this->mutex.unlock();
ptr = this->cache.value(key).toStrongRef();
dfaffebe Christian Ehringfeld
return;
62427ee0 Christian Ehringfeld
}
}
5ca225ed Christian Ehringfeld
this->mutex.lock();
4cde82a2 Christian Ehringfeld
this->cache.insert(key, entity.toWeakRef());
5ca225ed Christian Ehringfeld
this->mutex.unlock();
11bbe9a6 Christian Ehringfeld
}
}

3820ae33 Christian Ehringfeld
void Cache::remove(const QSharedPointer<Entity> &entity) {
abb9e8c5 Christian Ehringfeld
if (entity.data() && entity->getId() > -1) {
5ca225ed Christian Ehringfeld
this->mutex.lock();
e8d1537c Christian Ehringfeld
this->remove(entity->getId(), EntityHelper::getClassName(entity.data()));
5ca225ed Christian Ehringfeld
this->mutex.unlock();
11bbe9a6 Christian Ehringfeld
}
}

void Cache::remove(const qint64 &id, const QString &classname) {
this->cache.remove(this->generateKey(id, classname));
}

QSharedPointer<Entity> Cache::get(qint64 id, const QString &classname) {
QString key = this->generateKey(id, classname);
if (this->contains(key)) {
5ca225ed Christian Ehringfeld
this->mutex.lock();
3820ae33 Christian Ehringfeld
QSharedPointer<Entity> ptr = this->cache.value(key).toStrongRef();
dfaffebe Christian Ehringfeld
if (!ptr) {
3820ae33 Christian Ehringfeld
this->remove(id, classname);
}
5ca225ed Christian Ehringfeld
this->mutex.unlock();
3820ae33 Christian Ehringfeld
return ptr;
11bbe9a6 Christian Ehringfeld
}
return QSharedPointer<Entity>();
}

0d155b40 Christian Ehringfeld
QString Cache::generateKey(qint64 id, const QString &classname) const {
11bbe9a6 Christian Ehringfeld
if (id > -1) {
return QString::number(id).append("[").append(classname).append("]");
}
return QString("");
}