QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxCache.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_CACHE_H_
33#define _QX_CACHE_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QxCommon/QxBool.h>
47#include <QxCommon/QxAny.h>
48
50
52
53namespace qx {
54namespace cache {
55namespace detail {
56
57class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache>
58{
59
60 friend class qx::QxSingleton<QxCache>;
61
62protected:
63
64 typedef std::tuple<long, QDateTime, qx::any> type_qx_cache;
66
71
72public:
73
75 virtual ~QxCache();
76
77 long getCurrCost() const;
78 long getMaxCost() const;
79 void setMaxCost(long l);
80
81 long count() const;
82 long size() const;
83 bool isEmpty() const;
84 bool exist(const QString & sKey) const;
85 bool contains(const QString & sKey) const;
86 qx::any at(const QString & sKey);
87 long insertionCost(const QString & sKey);
88 QDateTime insertionDateTime(const QString & sKey);
89 void clear();
90
91 bool insert(const QString & sKey, const qx::any & anyObj, long lCost = 1, const QDateTime & dt = QDateTime());
92 bool remove(const QString & sKey);
93
94private:
95
96 void updateCost();
97
98};
99
100} // namespace detail
101} // namespace cache
102} // namespace qx
103
105
106namespace qx {
107namespace cache {
108
113inline void max_cost(long l)
115
120inline long max_cost()
121{ return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); }
122
127inline long current_cost()
128{ return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); }
129
134inline long count()
135{ return qx::cache::detail::QxCache::getSingleton()->count(); }
136
141inline bool is_empty()
142{ return qx::cache::detail::QxCache::getSingleton()->isEmpty(); }
143
148inline void clear()
150
155inline bool exist(const QString & sKey)
156{ return qx::cache::detail::QxCache::getSingleton()->exist(sKey); }
157
162inline bool remove(const QString & sKey)
163{ return qx::cache::detail::QxCache::getSingleton()->remove(sKey); }
164
169template <typename T>
170inline bool set(const QString & sKey, T & t, long lCost = 1, const QDateTime & dt = QDateTime())
171{
172 qx::any obj(t);
173 return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost, dt);
174}
175
180template <typename T>
181inline T get(const QString & sKey)
182{
184 if (obj.empty()) { return T(); }
185 try { return qx::any_cast<T>(obj); }
186 catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return T(); }
187 catch (...) { return T(); }
188}
189
194template <typename T>
195inline qx_bool get(const QString & sKey, T & t, QDateTime & dt)
196{
197 dt = QDateTime();
198 if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] qx::cache : key doesn't exist in cache"); }
200 dt = qx::cache::detail::QxCache::getSingleton()->insertionDateTime(sKey);
201 try { t = qx::any_cast<T>(obj); return qx_bool(true); }
202 catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] qx::cache : bad any cast exception"); }
203 catch (...) { return qx_bool(false, 0, "[QxOrm] qx::cache : unknown cast exception"); }
204}
205
210template <typename T>
211inline qx_bool get(const QString & sKey, T & t)
212{
213 QDateTime dt;
214 return qx::cache::get<T>(sKey, t, dt);
215}
216
217} // namespace cache
218} // namespace qx
219
220#endif // _QX_CACHE_H_
qx::any : basic implementation of boost::any (written by Kevlin Henney) when boost dependency is not ...
qx_bool : QxOrm library boolean type with code and description message when an error occured
qx::QxBool qx_bool
Definition QxBool.h:150
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
#define QX_DLL_EXPORT
Definition QxMacro.h:182
#define QX_DLL_EXPORT_QX_SINGLETON_HPP(x)
Definition QxMacro.h:190
Concrete class to define a thread-safe singleton of QxOrm library.
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by in...
qx::QxSingleton<T> : concrete class to define a thread-safe singleton of type T
Definition QxSingleton.h:59
static QxCache * getSingleton()
bool empty() const
Definition QxAny.h:98
long m_lMaxCost
Max cost before deleting object in cache.
Definition QxCache.h:69
long m_lCurrCost
Current cost in cache.
Definition QxCache.h:70
long insertionCost(const QString &sKey)
bool exist(const QString &sKey) const
bool insert(const QString &sKey, const qx::any &anyObj, long lCost=1, const QDateTime &dt=QDateTime())
type_qx_lst_cache m_cache
List of objects in cache under qx::any format.
Definition QxCache.h:67
qx::any at(const QString &sKey)
std::tuple< long, QDateTime, qx::any > type_qx_cache
Definition QxCache.h:64
QDateTime insertionDateTime(const QString &sKey)
QMutex m_oMutexCache
Mutex => 'QxCache' is thread-safe.
Definition QxCache.h:68
bool contains(const QString &sKey) const
qx::QxCollection< QString, type_qx_cache > type_qx_lst_cache
Definition QxCache.h:65
bool remove(const QString &sKey)
bool is_empty()
Return true if the cache contains no object; otherwise return false.
Definition QxCache.h:141
bool exist(const QString &sKey)
Return true if the cache contains an object associated with key sKey; otherwise return false.
Definition QxCache.h:155
long max_cost()
Return the maximum allowed total cost of the cache.
Definition QxCache.h:120
bool set(const QString &sKey, T &t, long lCost=1, const QDateTime &dt=QDateTime())
Insert object t into the cache with key sKey, associated cost lCost and insertion date-time dt....
Definition QxCache.h:170
void clear()
Delete all the objects in the cache.
Definition QxCache.h:148
long current_cost()
Return the current cost used by the cache.
Definition QxCache.h:127
bool remove(const QString &sKey)
Delete the object associated with key sKey. Return true if the object was found in the cache; otherwi...
Definition QxCache.h:162
long count()
Return the number of objects in the cache.
Definition QxCache.h:134
T get(const QString &sKey)
Return the object of type T associated with key sKey, or return default instance of T() if the key do...
Definition QxCache.h:181
Root namespace for all QxOrm library features.
ValueType * any_cast(any *)
Definition QxAny.h:133