Menu

[r938]: / framework / branches / refactoring / cache_interface.cpp  Maximize  Restore  History

Download this file

169 lines (139 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#define CPPCMS_SOURCE
#include "cache_interface.h"
#include "base_cache.h"
#include "cache_pool.h"
#include "http_context.h"
#include "http_response.h"
#include "json.h"
#include "service.h"
#include "cppcms_error.h"
#include <sstream>
#include <iostream>
namespace cppcms {
using namespace std;
namespace {
const time_t infty=
(sizeof(time_t)==4 ? 0x7FFFFFFF: 0x7FFFFFFFFFFFFFFFULL ) -
3600*24;
time_t deadtime(int sec)
{
if(sec<0)
return infty;
else {
time_t tmp;
time(&tmp);
if(tmp+sec<tmp) {
throw cppcms_error("Year 2038 problem?");
}
return tmp+sec;
}
}
}
struct cache_interface::data {};
cache_interface::cache_interface(http::context &context) :
context_(&context),
page_compression_used_(0)
{
cache_module_ = context_->service().cache_pool().get();
}
cache_interface::~cache_interface()
{
}
bool cache_interface::fetch_page(string const &key)
{
if(nocache()) return false;
bool gzip = context_->response().need_gzip();
page_compression_used_ = gzip;
std::string r_key = (gzip ? "_Z:" : "_U:") + key;
std::string tmp;
if(cache_module_->fetch(r_key,tmp,0)) {
if(gzip)
context_->response().content_encoding("gzip");
context_->response().out().write(tmp.c_str(),tmp.size());
return true;
}
else {
context_->response().copy_to_cache();
return false;
}
}
void cache_interface::store_page(string const &key,int timeout)
{
if(nocache()) return;
context_->response().finalize();
std::string r_key = (page_compression_used_ ? "_Z:" : "_U:") + key;
triggers_.insert(key);
cache_module_->store(r_key,context_->response().copied_data(),triggers_,deadtime(timeout));
}
void cache_interface::store_frame(std::string const &key,std::string const &frame,std::set<std::string> const &triggers,int timeout,bool notriggers)
{
store(key,frame,triggers,timeout,notriggers);
}
void cache_interface::store_frame(std::string const &key,
std::string const &frame,
int timeout,
bool notriggers)
{
store_frame(key,frame,set<std::string>(),timeout,notriggers);
}
bool cache_interface::nocache()
{
return cache_module_.get()==0;
}
bool cache_interface::has_cache()
{
return !nocache();
}
void cache_interface::add_trigger(string const &t)
{
if(nocache()) return;
triggers_.insert(t);
}
void cache_interface::rise(string const &t)
{
if(nocache()) return;
cache_module_->rise(t);
}
bool cache_interface::fetch_frame(string const &key,string &result,bool notriggers)
{
return fetch(key,result,notriggers);
}
bool cache_interface::fetch(string const &key,string &result,bool notriggers)
{
if(nocache()) return false;
set<string> new_trig;
if(cache_module_->fetch(key,result, (notriggers ? 0 : &new_trig))) {
if(!notriggers)
triggers_.insert(new_trig.begin(),new_trig.end());
return true;
}
return false;
}
void cache_interface::store(string const &key,string const &data,
set<string> const &triggers,
int timeout,
bool notriggers)
{
if(nocache()) return;
if(!notriggers) {
this->triggers_.insert(triggers.begin(),triggers.end());
this->triggers_.insert(key);
}
cache_module_->store(key,data,triggers,deadtime(timeout));
}
void cache_interface::clear()
{
if(nocache()) return;
cache_module_->clear();
}
bool cache_interface::stats(unsigned &k,unsigned &t)
{
if(nocache()) return false;
cache_module_->stats(k,t);
return true;
}
void cache_interface::reset()
{
triggers_.clear();
}
} // End of namespace cppcms
MongoDB Logo MongoDB