Menu

[r1697]: / framework / trunk / src / cache_pool.cpp  Maximize  Restore  History

Download this file

92 lines (82 with data), 3.3 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/cache_pool.h>
#include <cppcms/config.h>
#include "cache_storage.h"
#include "base_cache.h"
#include <cppcms/cppcms_error.h>
#ifndef CPPCMS_NO_TCP_CACHE
#include "cache_over_ip.h"
#endif
#include <cppcms/json.h>
namespace cppcms {
struct cache_pool::_data {
booster::intrusive_ptr<impl::base_cache> module;
};
cache_pool::cache_pool(json::value const &settings) :
d(new _data())
{
std::string type = settings.get("cache.backend","none");
#ifndef CPPCMS_NO_CACHE
if(type=="thread_shared") {
if(settings.get("service.worker_processes",0)>1)
throw cppcms_error(
"Can't use `thread_shared' backend with more then one process ether set "
"service.worker_processes to 0 or 1 or use cache.backend=\"process_shared\"");
unsigned items = settings.get("cache.limit",64);
d->module=impl::thread_cache_factory(items);
}
else if(type=="process_shared") {
#if defined(CPPCMS_WIN32)
throw cppcms_error("The 'process_shared' backend is not supported under MS Windows and Cygwin platforms");
#elif defined(CPPCMS_NO_PREFOK_CACHE)
throw cppcms_error("The 'process_shared' backend is disabled during build procedure");
#else // has prefork cache
size_t memory = settings.get("cache.memory",16384) * 1024;
if(memory < 65536)
throw cppcms_error("'process_shared' cache backend requires at least 64 KB of cache memory: cache.memory >= 64");
d->module=impl::process_cache_factory(memory);
#endif // prefork cache
}
else
#endif // no cache
if(type != "none" ) {
throw cppcms_error("Unsupported cache backend `" + type + "'");
}
#ifndef CPPCMS_NO_TCP_CACHE
if(settings.find("cache.tcp").type()==json::is_object) {
std::vector<std::string> ips=settings.get<std::vector<std::string> >("cache.tcp.ips");
std::vector<int> ports = settings.get<std::vector<int> >("cache.tcp.ports");
if(ips.empty() || ports.empty() || ips.size()!=ports.size()) {
throw cppcms_error("Invalid settings in cache.tcp.ports or cache.tcp.ips");
}
booster::intrusive_ptr<impl::base_cache> l1=d->module;
d->module=impl::tcp_cache_factory(ips,ports,l1);
}
#endif // no tcp cace
}
cache_pool::~cache_pool()
{
}
booster::intrusive_ptr<impl::base_cache> cache_pool::get()
{
return d->module;
}
} // cppcms
MongoDB Logo MongoDB