QxOrm
1.5.0
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _QX_CACHE_H_ 00033 #define _QX_CACHE_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QxCommon/QxBool.h> 00047 #include <QxCommon/QxAny.h> 00048 00049 #include <QxCollection/QxCollection.h> 00050 00051 #include <QxSingleton/QxSingleton.h> 00052 00053 namespace qx { 00054 namespace cache { 00055 namespace detail { 00056 00057 class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache> 00058 { 00059 00060 friend class qx::QxSingleton<QxCache>; 00061 00062 protected: 00063 00064 typedef std::tuple<long, QDateTime, qx::any> type_qx_cache; 00065 typedef qx::QxCollection<QString, type_qx_cache> type_qx_lst_cache; 00066 00067 type_qx_lst_cache m_cache; 00068 QMutex m_oMutexCache; 00069 long m_lMaxCost; 00070 long m_lCurrCost; 00071 00072 public: 00073 00074 QxCache(); 00075 virtual ~QxCache(); 00076 00077 long getCurrCost() const; 00078 long getMaxCost() const; 00079 void setMaxCost(long l); 00080 00081 long count() const; 00082 long size() const; 00083 bool isEmpty() const; 00084 bool exist(const QString & sKey) const; 00085 bool contains(const QString & sKey) const; 00086 qx::any at(const QString & sKey); 00087 long insertionCost(const QString & sKey); 00088 QDateTime insertionDateTime(const QString & sKey); 00089 void clear(); 00090 00091 bool insert(const QString & sKey, const qx::any & anyObj, long lCost = 1, const QDateTime & dt = QDateTime()); 00092 bool remove(const QString & sKey); 00093 00094 private: 00095 00096 void updateCost(); 00097 00098 }; 00099 00100 } // namespace detail 00101 } // namespace cache 00102 } // namespace qx 00103 00104 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::cache::detail::QxCache) 00105 00106 namespace qx { 00107 namespace cache { 00108 00113 inline void max_cost(long l) 00114 { qx::cache::detail::QxCache::getSingleton()->setMaxCost(l); } 00115 00120 inline long max_cost() 00121 { return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); } 00122 00127 inline long current_cost() 00128 { return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); } 00129 00134 inline long count() 00135 { return qx::cache::detail::QxCache::getSingleton()->count(); } 00136 00141 inline bool is_empty() 00142 { return qx::cache::detail::QxCache::getSingleton()->isEmpty(); } 00143 00148 inline void clear() 00149 { qx::cache::detail::QxCache::getSingleton()->clear(); } 00150 00155 inline bool exist(const QString & sKey) 00156 { return qx::cache::detail::QxCache::getSingleton()->exist(sKey); } 00157 00162 inline bool remove(const QString & sKey) 00163 { return qx::cache::detail::QxCache::getSingleton()->remove(sKey); } 00164 00169 template <typename T> 00170 inline bool set(const QString & sKey, T & t, long lCost = 1, const QDateTime & dt = QDateTime()) 00171 { 00172 qx::any obj(t); 00173 return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost, dt); 00174 } 00175 00180 template <typename T> 00181 inline T get(const QString & sKey) 00182 { 00183 qx::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey); 00184 if (obj.empty()) { return T(); } 00185 try { return qx::any_cast<T>(obj); } 00186 catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return T(); } 00187 catch (...) { return T(); } 00188 } 00189 00194 template <typename T> 00195 inline qx_bool get(const QString & sKey, T & t, QDateTime & dt) 00196 { 00197 dt = QDateTime(); 00198 if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] qx::cache : key doesn't exist in cache"); } 00199 qx::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey); 00200 dt = qx::cache::detail::QxCache::getSingleton()->insertionDateTime(sKey); 00201 try { t = qx::any_cast<T>(obj); return qx_bool(true); } 00202 catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] qx::cache : bad any cast exception"); } 00203 catch (...) { return qx_bool(false, 0, "[QxOrm] qx::cache : unknown cast exception"); } 00204 } 00205 00210 template <typename T> 00211 inline qx_bool get(const QString & sKey, T & t) 00212 { 00213 QDateTime dt; 00214 return qx::cache::get<T>(sKey, t, dt); 00215 } 00216 00217 } // namespace cache 00218 } // namespace qx 00219 00220 #endif // _QX_CACHE_H_