Verified Commit a4f7b0e6 authored by Linus Jahn's avatar Linus Jahn 🔌 Committed by Linus Jahn
Browse files

Add BitsOfBinaryMemoryCache as implementation of BitsOfBinaryCache

This adds an implementation of a BitsOfBinaryCache that will use the main memory
to cache BitsOfBinaryData.
parent 5bd00c76
......@@ -33,7 +33,8 @@ SOURCES += \
src/gloox-extensions/httpuploadrequest.cpp \
src/gloox-extensions/httpuploadslot.cpp \
src/gloox-extensions/bitsofbinarydata.cpp \
src/gloox-extensions/bitsofbinarymanager.cpp
src/gloox-extensions/bitsofbinarymanager.cpp \
src/gloox-extensions/bitsofbinarymemorycache.cpp
HEADERS += \
src/Database.h \
......@@ -68,7 +69,9 @@ HEADERS += \
src/gloox-extensions/bitsofbinarydata.h \
src/gloox-extensions/bitsofbinarymanager.h \
src/gloox-extensions/bitsofbinaryhandler.h \
src/gloox-extensions/bitsofbinarycache.h
src/gloox-extensions/bitsofbinarycache.h \
src/gloox-extensions/bitsofbinarymemorycache.h
android: INCLUDEPATH += $$PWD/3rdparty/gloox/include
android: LIBS += -L$$PWD/3rdparty/gloox/lib/
......
......@@ -37,4 +37,5 @@ set(KAIDAN_SOURCES
${CURDIR}/gloox-extensions/httpuploadslot.cpp
${CURDIR}/gloox-extensions/bitsofbinarydata.cpp
${CURDIR}/gloox-extensions/bitsofbinarymanager.cpp
${CURDIR}/gloox-extensions/bitsofbinarymemorycache.cpp
)
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2018 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan 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 General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bitsofbinarymemorycache.h"
#include "bitsofbinarydata.h"
#include <time.h>
using namespace gloox;
BitsOfBinaryMemoryCache::BitsOfBinaryMemoryCache()
{
}
void BitsOfBinaryMemoryCache::addBobData(BitsOfBinaryData* data)
{
if (data->valid() && data->maxAge() != 0 &&
cache.count(data->contentId()) == 0) {
CacheDataSet set;
set.data = data;
set.timeAdded = time(NULL); // current time in seconds
cache[data->contentId()] = set;
}
}
BitsOfBinaryData* BitsOfBinaryMemoryCache::getByCid(const std::string& cid)
{
cleanUp();
return cache[cid].data;
}
bool BitsOfBinaryMemoryCache::hasCid(std::string& cid)
{
cleanUp();
// count on maps only returns 0 or 1
return cache.count(cid);
}
void BitsOfBinaryMemoryCache::cleanUp()
{
for (std::pair<std::string, CacheDataSet> set : cache) {
// delete, if time limit was reached
if (set.second.timeAdded + set.second.data->maxAge() > time(NULL)) {
cache.erase(set.first);
}
}
}
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2018 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan 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 General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BITSOFBINARYMEMORYCACHE_H__
#define BITSOFBINARYMEMORYCACHE_H__
#include "bitsofbinarycache.h"
#include <map>
namespace gloox {
/**
* @class BitsOfBinaryMemoryCache An implementation of an @c BitsOfBinaryCache
* using main memory for caching.
*/
class GLOOX_API BitsOfBinaryMemoryCache : gloox::BitsOfBinaryCache
{
public:
/**
* Default constructor
*/
BitsOfBinaryMemoryCache();
/**
* @todo write docs
*
* @param data TODO
* @return TODO
*/
virtual void addBobData(BitsOfBinaryData* data) = 0;
/**
* @todo write docs
*
* @param cid TODO
* @return TODO
*/
virtual BitsOfBinaryData* getByCid(const std::string& cid) = 0;
/**
* @todo write docs
*
* @param cid TODO
* @return TODO
*/
virtual bool hasCid(std::string& cid) = 0;
private:
/**
* Cleans up the cache and deletes all data sets that have passed the
* age limit
*/
void cleanUp();
struct CacheDataSet {
BitsOfBinaryData* data;
long timeAdded;
};
std::map<std::string, CacheDataSet> cache;
};
}
#endif // BITSOFBINARYMEMORYCACHE_H__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment