cache.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifdef _WIN32
  2. #include <winsock2.h>
  3. #endif
  4. #include <map>
  5. #include "cache.h"
  6. void AddorUpdateCache(cacheItem *item, const time_t now, const bool compressed,
  7. const uniString::utf8 &header, const uniString::utf8 &response,
  8. CacheMap_t &cache, AOL_namespace::mutex &lock,
  9. const streamData::streamID_t sid)
  10. {
  11. if (lock.timedLock(3000))
  12. {
  13. // if we've already got an instance then we update
  14. if (item == NULL)
  15. {
  16. item = cache[sid] = new cacheItem();
  17. }
  18. if (item != NULL)
  19. {
  20. if (compressed)
  21. {
  22. item->generatedGZIP = now;
  23. item->responseGZIP = response;
  24. item->headerGZIP = header;
  25. }
  26. else
  27. {
  28. item->generatedRaw = now;
  29. item->responseRaw = response;
  30. item->headerRaw = header;
  31. }
  32. }
  33. lock.unlock();
  34. }
  35. }
  36. bool GetFromCache(const cacheItem *item, AOL_namespace::mutex &lock,
  37. const time_t now, const bool compressed,
  38. const bool headRequest, uniString::utf8 &response,
  39. const int limit)
  40. {
  41. if (lock.timedLock(3000))
  42. {
  43. if (item != NULL)
  44. {
  45. if (((now - item->generatedGZIP) < limit) && compressed)
  46. {
  47. if (item->responseGZIP.size() > 0)
  48. {
  49. response = (!headRequest ? item->responseGZIP : item->headerGZIP);
  50. lock.unlock();
  51. return true;
  52. }
  53. }
  54. else if (((now - item->generatedRaw) < limit) && !compressed)
  55. {
  56. if (item->responseRaw.size() > 0)
  57. {
  58. response = (!headRequest ? item->responseRaw : item->headerRaw);
  59. lock.unlock();
  60. return true;
  61. }
  62. }
  63. }
  64. lock.unlock();
  65. }
  66. return false;
  67. }
  68. void DeleteCache(CacheMap_t &cache)
  69. {
  70. if (!cache.empty())
  71. {
  72. for (CacheMap_t::const_iterator i = cache.begin(); i != cache.end(); ++i)
  73. {
  74. delete (*i).second;
  75. }
  76. cache.clear();
  77. }
  78. }
  79. void DeleteAllCaches()
  80. {
  81. DeleteCache(m_xmlStatsCache);
  82. DeleteCache(m_xmlStatisticsCache);
  83. DeleteCache(m_jsonStatsCache);
  84. DeleteCache(m_jsonStatisticsCache);
  85. DeleteCache(m_7Cache);
  86. DeleteCache(m_PLSCache);
  87. DeleteCache(m_M3UCache);
  88. DeleteCache(m_ASXCache);
  89. DeleteCache(m_QTLCache);
  90. DeleteCache(m_XSPFCache);
  91. DeleteCache(m_xmlTracksCache);
  92. DeleteCache(m_jsonTracksCache);
  93. DeleteCache(m_xmlPlayedCache);
  94. DeleteCache(m_jsonPlayedCache);
  95. DeleteCache(m_htmlPlayedCache);
  96. DeleteCache(m_streamArtCache);
  97. DeleteCache(m_playingArtCache);
  98. DeleteCache(m_crossdomainCache);
  99. }