QxCache.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef _QX_CACHE_H_
  32. #define _QX_CACHE_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxCache.h
  38. * \author Lionel Marty
  39. * \ingroup QxCache
  40. * \brief qx::cache : based on singleton pattern, provide basic thread-safe cache feature to backup and restore any kind of objects (for example, object fetched from database)
  41. */
  42. #include <QxCommon/QxBool.h>
  43. #include <QxCommon/QxAny.h>
  44. #include <QxCollection/QxCollection.h>
  45. #include <QxSingleton/QxSingleton.h>
  46. namespace qx {
  47. namespace cache {
  48. namespace detail {
  49. class QX_DLL_EXPORT QxCache : public qx::QxSingleton<QxCache>
  50. {
  51. friend class qx::QxSingleton<QxCache>;
  52. protected:
  53. typedef std::tuple<long, QDateTime, qx::any> type_qx_cache;
  54. typedef qx::QxCollection<QString, type_qx_cache> type_qx_lst_cache;
  55. type_qx_lst_cache m_cache; //!< List of objects in cache under qx::any format
  56. QMutex m_oMutexCache; //!< Mutex => 'QxCache' is thread-safe
  57. long m_lMaxCost; //!< Max cost before deleting object in cache
  58. long m_lCurrCost; //!< Current cost in cache
  59. public:
  60. QxCache();
  61. virtual ~QxCache();
  62. long getCurrCost() const;
  63. long getMaxCost() const;
  64. void setMaxCost(long l);
  65. long count() const;
  66. long size() const;
  67. bool isEmpty() const;
  68. bool exist(const QString & sKey) const;
  69. bool contains(const QString & sKey) const;
  70. qx::any at(const QString & sKey);
  71. long insertionCost(const QString & sKey);
  72. QDateTime insertionDateTime(const QString & sKey);
  73. void clear();
  74. bool insert(const QString & sKey, const qx::any & anyObj, long lCost = 1, const QDateTime & dt = QDateTime());
  75. bool remove(const QString & sKey);
  76. private:
  77. void updateCost();
  78. };
  79. } // namespace detail
  80. } // namespace cache
  81. } // namespace qx
  82. QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::cache::detail::QxCache)
  83. namespace qx {
  84. namespace cache {
  85. /*!
  86. * \ingroup QxCache
  87. * \brief Set the maximum allowed total cost of the cache to l. If the current total cost is greater than l, some objects are deleted immediately
  88. */
  89. inline void max_cost(long l)
  90. { qx::cache::detail::QxCache::getSingleton()->setMaxCost(l); }
  91. /*!
  92. * \ingroup QxCache
  93. * \brief Return the maximum allowed total cost of the cache
  94. */
  95. inline long max_cost()
  96. { return qx::cache::detail::QxCache::getSingleton()->getMaxCost(); }
  97. /*!
  98. * \ingroup QxCache
  99. * \brief Return the current cost used by the cache
  100. */
  101. inline long current_cost()
  102. { return qx::cache::detail::QxCache::getSingleton()->getCurrCost(); }
  103. /*!
  104. * \ingroup QxCache
  105. * \brief Return the number of objects in the cache
  106. */
  107. inline long count()
  108. { return qx::cache::detail::QxCache::getSingleton()->count(); }
  109. /*!
  110. * \ingroup QxCache
  111. * \brief Return true if the cache contains no object; otherwise return false
  112. */
  113. inline bool is_empty()
  114. { return qx::cache::detail::QxCache::getSingleton()->isEmpty(); }
  115. /*!
  116. * \ingroup QxCache
  117. * \brief Delete all the objects in the cache
  118. */
  119. inline void clear()
  120. { qx::cache::detail::QxCache::getSingleton()->clear(); }
  121. /*!
  122. * \ingroup QxCache
  123. * \brief Return true if the cache contains an object associated with key sKey; otherwise return false
  124. */
  125. inline bool exist(const QString & sKey)
  126. { return qx::cache::detail::QxCache::getSingleton()->exist(sKey); }
  127. /*!
  128. * \ingroup QxCache
  129. * \brief Delete the object associated with key sKey. Return true if the object was found in the cache; otherwise return false
  130. */
  131. inline bool remove(const QString & sKey)
  132. { return qx::cache::detail::QxCache::getSingleton()->remove(sKey); }
  133. /*!
  134. * \ingroup QxCache
  135. * \brief Insert object t into the cache with key sKey, associated cost lCost and insertion date-time dt. Any object with the same key already in the cache will be removed
  136. */
  137. template <typename T>
  138. inline bool set(const QString & sKey, T & t, long lCost = 1, const QDateTime & dt = QDateTime())
  139. {
  140. qx::any obj(t);
  141. return qx::cache::detail::QxCache::getSingleton()->insert(sKey, obj, lCost, dt);
  142. }
  143. /*!
  144. * \ingroup QxCache
  145. * \brief Return the object of type T associated with key sKey, or return default instance of T() if the key does not exist in the cache
  146. */
  147. template <typename T>
  148. inline T get(const QString & sKey)
  149. {
  150. qx::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
  151. if (obj.empty()) { return T(); }
  152. try { return qx::any_cast<T>(obj); }
  153. catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return T(); }
  154. catch (...) { return T(); }
  155. }
  156. /*!
  157. * \ingroup QxCache
  158. * \brief Return true if object t can be fetched with associated key sKey and insertion date-time dt; otherwise return false with an error description
  159. */
  160. template <typename T>
  161. inline qx_bool get(const QString & sKey, T & t, QDateTime & dt)
  162. {
  163. dt = QDateTime();
  164. if (! qx::cache::exist(sKey)) { return qx_bool(false, 0, "[QxOrm] qx::cache : key doesn't exist in cache"); }
  165. qx::any obj = qx::cache::detail::QxCache::getSingleton()->at(sKey);
  166. dt = qx::cache::detail::QxCache::getSingleton()->insertionDateTime(sKey);
  167. try { t = qx::any_cast<T>(obj); return qx_bool(true); }
  168. catch (const qx::bad_any_cast & err) { Q_UNUSED(err); return qx_bool(false, 0, "[QxOrm] qx::cache : bad any cast exception"); }
  169. catch (...) { return qx_bool(false, 0, "[QxOrm] qx::cache : unknown cast exception"); }
  170. }
  171. /*!
  172. * \ingroup QxCache
  173. * \brief Return true if object t can be fetched with associated key sKey; otherwise return false with an error description
  174. */
  175. template <typename T>
  176. inline qx_bool get(const QString & sKey, T & t)
  177. {
  178. QDateTime dt;
  179. return qx::cache::get<T>(sKey, t, dt);
  180. }
  181. } // namespace cache
  182. } // namespace qx
  183. #endif // _QX_CACHE_H_