ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

249 lines
4.7 KiB

  1. #include "cache_interface.h"
  2. #include "worker_thread.h"
  3. #include "global_config.h"
  4. #include <boost/iostreams/filtering_stream.hpp>
  5. #include <boost/iostreams/filter/gzip.hpp>
  6. #include <sstream>
  7. #include <iostream>
  8. #include "manager.h"
  9. namespace cppcms {
  10. using namespace std;
  11. namespace {
  12. const time_t infty=
  13. (sizeof(time_t)==4 ? 0x7FFFFFFF: 0x7FFFFFFFFFFFFFFFULL ) -
  14. 3600*24;
  15. time_t deadtime(int sec)
  16. {
  17. if(sec<0)
  18. return infty;
  19. else {
  20. time_t tmp;
  21. time(&tmp);
  22. if(tmp+sec<tmp) {
  23. throw cppcms_error("Year 2038 problem?");
  24. }
  25. return tmp+sec;
  26. }
  27. }
  28. }
  29. #ifdef CPPCMS_EMBEDDED
  30. void deflate(string const &text,ostream &stream,long level,long length)
  31. {
  32. throw cppcms_error("gzip compression is not supported for embedded system");
  33. }
  34. string deflate(string const &text,long level,long length)
  35. {
  36. throw cppcms_error("gzip compression is not supported for embedded system");
  37. }
  38. bool cache_iface::fetch_page(string const &key)
  39. {
  40. return false;
  41. }
  42. void cache_iface::store_page(string const &key,int timeout)
  43. {
  44. }
  45. void cache_iface::add_trigger(string const &t)
  46. {
  47. }
  48. void cache_iface::rise(string const &t)
  49. {
  50. }
  51. bool cache_iface::fetch_data(string const &key,serializable &data,bool notriggers)
  52. {
  53. return false;
  54. }
  55. void cache_iface::store_data(string const &key,serializable const &data,
  56. set<string> const &triggers,
  57. int timeout,
  58. bool notriggers)
  59. {
  60. }
  61. bool cache_iface::fetch_frame(string const &key,string &result,bool notriggers)
  62. {
  63. return false;
  64. }
  65. void cache_iface::store_frame(string const &key,string const &data,
  66. set<string> const &triggers,
  67. int timeout,
  68. bool notriggers)
  69. {
  70. }
  71. void cache_iface::clear()
  72. {
  73. }
  74. bool cache_iface::stats(unsigned &k,unsigned &t)
  75. {
  76. return false;
  77. }
  78. #else // Normal System
  79. void deflate(string const &text,ostream &stream,long level,long length)
  80. {
  81. using namespace boost::iostreams;
  82. gzip_params params;
  83. if(level!=-1){
  84. params.level=level;
  85. }
  86. filtering_ostream zstream;
  87. if(length!=-1){
  88. zstream.push(gzip_compressor(params,length));
  89. }
  90. else {
  91. zstream.push(gzip_compressor(params));
  92. }
  93. zstream.push(stream);
  94. zstream<<text;
  95. }
  96. string deflate(string const &text,long level,long length)
  97. {
  98. ostringstream sstream;
  99. deflate(text,sstream,level,length);
  100. return sstream.str();
  101. }
  102. bool cache_iface::fetch_page(string const &key)
  103. {
  104. if(!cms->caching_module) return false;
  105. string tmp;
  106. if(cms->caching_module->fetch_page(key,tmp,cms->gzip)) {
  107. cms->cout<<tmp;
  108. cms->gzip_done=true;
  109. return true;
  110. }
  111. return false;
  112. }
  113. void cache_iface::store_page(string const &key,int timeout)
  114. {
  115. if(!cms->caching_module) return;
  116. archive a;
  117. int level=cms->app.config.get<int>("gzip.level",-1);
  118. int length=cms->app.config.get<int>("gzip.buffer",-1);
  119. string tmp=cms->out_buf.str();
  120. string compr=deflate(tmp,level,length);
  121. a<<tmp<<compr;
  122. if(cms->gzip){
  123. cms->out_buf.str("");
  124. cms->cout<<compr;
  125. cms->gzip_done=true;
  126. }
  127. cms->caching_module->store(key,triggers,deadtime(timeout),a);
  128. }
  129. void cache_iface::add_trigger(string const &t)
  130. {
  131. if(!cms->caching_module) return;
  132. triggers.insert(t);
  133. }
  134. void cache_iface::rise(string const &t)
  135. {
  136. if(!cms->caching_module) return;
  137. cms->caching_module->rise(t);
  138. }
  139. bool cache_iface::fetch_data(string const &key,serializable &data,bool notriggers)
  140. {
  141. if(!cms->caching_module) return false;
  142. archive a;
  143. set<string> new_trig;
  144. if(cms->caching_module->fetch(key,a,new_trig)) {
  145. data.load(a);
  146. if(!notriggers){
  147. triggers.insert(new_trig.begin(),new_trig.end());
  148. }
  149. return true;
  150. }
  151. return false;
  152. }
  153. void cache_iface::store_data(string const &key,serializable const &data,
  154. set<string> const &triggers,
  155. int timeout,
  156. bool notriggers)
  157. {
  158. if(!cms->caching_module) return;
  159. archive a;
  160. data.save(a);
  161. if(!notriggers) {
  162. this->triggers.insert(triggers.begin(),triggers.end());
  163. this->triggers.insert(key);
  164. }
  165. cms->caching_module->store(key,triggers,deadtime(timeout),a);
  166. }
  167. bool cache_iface::fetch_frame(string const &key,string &result,bool notriggers)
  168. {
  169. if(!cms->caching_module) return false;
  170. archive a;
  171. set<string> new_trig;
  172. if(cms->caching_module->fetch(key,a,new_trig)) {
  173. a>>result;
  174. if(!notriggers)
  175. triggers.insert(new_trig.begin(),new_trig.end());
  176. return true;
  177. }
  178. return false;
  179. }
  180. void cache_iface::store_frame(string const &key,string const &data,
  181. set<string> const &triggers,
  182. int timeout,
  183. bool notriggers)
  184. {
  185. if(!cms->caching_module) return;
  186. archive a;
  187. a<<data;
  188. if(!notriggers) {
  189. this->triggers.insert(triggers.begin(),triggers.end());
  190. this->triggers.insert(key);
  191. }
  192. cms->caching_module->store(key,triggers,deadtime(timeout),a);
  193. }
  194. void cache_iface::clear()
  195. {
  196. if(cms->caching_module)
  197. cms->caching_module->clear();
  198. }
  199. bool cache_iface::stats(unsigned &k,unsigned &t)
  200. {
  201. if(!cms->caching_module)
  202. return false;
  203. cms->caching_module->stats(k,t);
  204. return true;
  205. }
  206. #endif
  207. } // End of namespace cppcms