开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (6)
标签 (5)
master
revert-35-build-nogzip
http_1.1
boost_locale_merge
v1.0_fixes
multiple-event-loops
v1.2.1
v1.2.0
v1.1.1
v1.1.0
v1.0.5
master
分支 (6)
标签 (5)
master
revert-35-build-nogzip
http_1.1
boost_locale_merge
v1.0_fixes
multiple-event-loops
v1.2.1
v1.2.0
v1.1.1
v1.1.0
v1.0.5
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (6)
标签 (5)
master
revert-35-build-nogzip
http_1.1
boost_locale_merge
v1.0_fixes
multiple-event-loops
v1.2.1
v1.2.0
v1.1.1
v1.1.0
v1.0.5
cppcms
/
src
/
cache_storage.cpp
cppcms
/
src
/
cache_storage.cpp
cache_storage.cpp 10.67 KB
一键复制 编辑 原始数据 按行查看 历史
Artyom Beilis 提交于 2017年03月02日 18:32 +08:00 . Cleanup of auto_ptr and some other warnings
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/config.h>
#include <set>
#ifndef CPPCMS_NO_CACHE
#include "cache_storage.h"
#include <booster/thread.h>
#include <iostream>
#if defined CPPCMS_WIN32
# ifndef CPPCMS_NO_PREFOK_CACHE
# define CPPCMS_NO_PREFOK_CACHE
# endif
#endif
#ifndef CPPCMS_NO_PREFOK_CACHE
# include "posix_util.h"
# include "shmem_allocator.h"
#endif
#include <map>
#include <string>
#include <list>
#include <limits>
#include <booster/auto_ptr_inc.h>
#include <time.h>
#include <cppcms/cstdint.h>
#include "hash_map.h"
#include <string.h>
namespace cppcms {
namespace impl {
template<typename String>
struct copy_traits {
static String to_int(std::string const &other)
{
return String(other.c_str(),other.size());
}
static std::string to_std(String const &other)
{
return std::string(other.c_str(),other.size());
}
};
template<>
struct copy_traits<std::string>
{
static std::string to_int(std::string const &a) { return a; }
static std::string to_std(std::string const &a) { return a; }
};
struct string_equal {
template<typename S1,typename S2>
bool operator()(S1 const &s1,S2 const &s2) const
{
return s1.size() == s2.size() && memcmp(s1.c_str(),s2.c_str(),s1.size()) == 0;
}
};
#ifndef CPPCMS_NO_PREFOK_CACHE
struct process_settings {
static shmem_control *process_memory;
typedef ::cppcms::impl::mutex mutex_type;
typedef ::cppcms::impl::shared_mutex shared_mutex_type;
typedef mutex_type::guard lock_guard;
typedef shared_mutex_type::shared_guard rdlock_guard;
typedef shared_mutex_type::unique_guard wrlock_guard;
typedef shmem_allocator<char,process_memory> allocator;
static bool not_enough_memory()
{
return process_memory->max_available() < process_memory->size() / 10;
}
static size_t size_limit()
{
return process_memory->size() / 20;
}
static void init(size_t size)
{
if(process_memory)
return;
process_memory=new shmem_control(size);
}
static const bool process_shared = true;
};
shmem_control *process_settings::process_memory=0;
#endif
struct thread_settings {
typedef booster::mutex mutex_type;
typedef booster::shared_mutex shared_mutex_type;
typedef booster::unique_lock<booster::mutex> lock_guard;
typedef booster::shared_lock<booster::shared_mutex> rdlock_guard;
typedef booster::unique_lock<booster::shared_mutex> wrlock_guard;
typedef std::allocator<char> allocator;
static bool not_enough_memory() { return false; }
static size_t size_limit() { return std::numeric_limits<size_t>::max(); }
static const bool process_shared = false;
};
template<typename Setup>
class mem_cache : public base_cache {
typedef typename Setup::mutex_type mutex_type;
typedef typename Setup::shared_mutex_type shared_mutex_type;
std::auto_ptr<mutex_type> lru_mutex;
std::auto_ptr<shared_mutex_type> access_lock;
typedef typename Setup::allocator allocator;
typedef typename Setup::lock_guard lock_guard;
typedef typename Setup::rdlock_guard rdlock_guard;
typedef typename Setup::wrlock_guard wrlock_guard;
typedef typename allocator::template rebind<mem_cache>::other this_allocator;
bool not_enough_memory()
{
return Setup::not_enough_memory();
}
size_t size_limit()
{
return Setup::size_limit();
}
struct container;
typedef std::basic_string<char,std::char_traits<char>,allocator > string_type;
typedef hash_map<
string_type,
container,
string_hash,
string_equal,
typename allocator::template rebind<std::pair<const string_type,container> >::other
> map_type;
typedef typename map_type::iterator pointer;
typedef std::list<
pointer,
typename allocator::template rebind<pointer>::other
> pointer_list_type;
typedef hash_map<
string_type,
pointer_list_type,
string_hash,
string_equal,
typename allocator::template rebind<std::pair<const string_type,pointer_list_type> >::other
> triggers_map_type;
typedef std::pair<
typename triggers_map_type::iterator,
typename pointer_list_type::iterator
> trigger_ptr_type;
typedef std::list<
trigger_ptr_type,
typename allocator::template rebind<trigger_ptr_type>::other
> triggers_list_type;
typedef std::multimap<
time_t,
pointer,
std::less<time_t>,
typename allocator::template rebind<std::pair<const time_t,pointer> >::other
> timeout_mmap_type;
struct container {
string_type data;
typedef typename map_type::iterator pointer;
typename pointer_list_type::iterator lru;
triggers_list_type triggers;
typename timeout_mmap_type::iterator timeout;
uint64_t generation;
};
map_type primary;
triggers_map_type triggers;
typedef typename triggers_map_type::iterator triggers_ptr;
timeout_mmap_type timeout;
typedef typename timeout_mmap_type::iterator timeout_ptr;
pointer_list_type lru;
typedef typename pointer_list_type::iterator lru_ptr;
unsigned limit;
size_t size;
size_t triggers_count;
int refs;
uint64_t generation;
string_type to_int(std::string const &other)
{
return copy_traits<string_type>::to_int(other);
}
std::string to_std(string_type const &other)
{
return copy_traits<string_type>::to_std(other);
}
void delete_node(pointer p)
{
lru.erase(p->second.lru);
timeout.erase(p->second.timeout);
typename triggers_list_type::iterator i;
for(i=p->second.triggers.begin();i!=p->second.triggers.end();i++) {
i->first->second.erase(i->second);
triggers_count --;
if(i->first->second.empty())
triggers.erase(i->first);
}
primary.erase(p);
size--;
}
public:
mem_cache(unsigned pages=0) :
lru_mutex(new mutex_type()),
access_lock(new shared_mutex_type()),
limit(pages),
size(0),
refs(0),
generation(0)
{
nl_clear();
}
~mem_cache()
{
}
void set_size(unsigned l)
{
limit = l;
nl_clear();
};
virtual bool fetch(std::string const &key,std::string *a,std::set<std::string> *triggers,time_t *timeout_out,uint64_t *gen)
{
rdlock_guard lock(*access_lock);
pointer p;
time_t now;
time(&now);
if((p=primary.find(key))==primary.end() || p->second.timeout->first < now) {
return false;
}
{ // Update LRU
lock_guard lock(*lru_mutex);
lru.erase(p->second.lru);
lru.push_front(p);
p->second.lru=lru.begin();
}
if(a)
*a=to_std(p->second.data);
if(triggers) {
typename triggers_list_type::iterator tp;
for(tp=p->second.triggers.begin();tp!=p->second.triggers.end();tp++) {
triggers->insert(to_std(tp->first->first));
}
}
if(timeout_out) {
*timeout_out=p->second.timeout->first;
}
if(gen)
*gen=p->second.generation;
return true;
}
virtual void rise(std::string const &trigger)
{
wrlock_guard lock(*access_lock);
triggers_ptr p = triggers.find(trigger);
if(p==triggers.end())
return;
std::list<pointer> kill_list;
for(typename pointer_list_type::iterator it=p->second.begin();it!=p->second.end();++it) {
kill_list.push_back(*it);
}
typename std::list<pointer>::iterator lptr;
for(lptr=kill_list.begin();lptr!=kill_list.end();lptr++) {
delete_node(*lptr);
}
}
void nl_clear()
{
timeout.clear();
lru.clear();
primary.clear();
primary.rehash(limit);
triggers.clear();
triggers.rehash(limit);
size = 0;
triggers_count = 0;
}
virtual void clear()
{
wrlock_guard lock(*access_lock);
nl_clear();
}
virtual void stats(unsigned &keys,unsigned &triggers)
{
rdlock_guard lock(*access_lock);
keys=size;
triggers = triggers_count;
}
void check_limits()
{
pointer main=primary.end();
time_t now;
time(&now);
while(size > 0 && (not_enough_memory() || (size>=limit && limit>0)))
{
if(!timeout.empty() && timeout.begin()->first<now) {
main=timeout.begin()->second;
}
else if(!lru.empty()){
main=*lru.rbegin();
}
else
break;
delete_node(main);
}
}
virtual void remove(std::string const &key)
{
wrlock_guard lock(*access_lock);
pointer p=primary.find(key);
if(p==primary.end())
return;
delete_node(p);
}
void add_trigger(pointer p,std::string const &key)
{
std::pair<string_type,pointer_list_type> tr(to_int(key),pointer_list_type());
std::pair<triggers_ptr,bool> r=triggers.insert(tr);
triggers_ptr it = r.first;
it->second.push_front(p);
p->second.triggers.push_back(trigger_ptr_type(it,it->second.begin()));
triggers_count++;
}
virtual void store( std::string const &key,
std::string const &a,
std::set<std::string> const &triggers_in,
time_t timeout_in,
uint64_t const *gen)
{
string_type ar;
try {
string_type tmp = to_int(a);
ar.swap(tmp);
}
catch(std::bad_alloc const &) {
return;
}
wrlock_guard lock(*access_lock);
try {
pointer main;
main=primary.find(key);
if(main!=primary.end())
delete_node(main);
if(size > size_limit())
return;
check_limits();
string_type int_key = to_int(key);
std::pair<pointer,bool> res=primary.insert(std::pair<string_type,container>(int_key,container()));
size ++;
main=res.first;
container &cont=main->second;
cont.data.swap(ar);
if(gen)
cont.generation=*gen;
else
cont.generation=generation++;
lru.push_front(main);
cont.lru=lru.begin();
cont.timeout=timeout.insert(std::pair<time_t,pointer>(timeout_in,main));
if(triggers_in.find(key)==triggers_in.end()){
add_trigger(main,key);
}
std::set<std::string>::const_iterator si;
for(si=triggers_in.begin();si!=triggers_in.end();si++) {
add_trigger(main,*si);
}
}
catch(std::bad_alloc const &e)
{
nl_clear();
}
}
virtual void add_ref()
{
if(Setup::process_shared)
return;
wrlock_guard lock(*access_lock);
refs++;
}
virtual bool del_ref()
{
// This object should not be deleted because it is created only once
// and exists in memory
if(Setup::process_shared)
return false;
wrlock_guard lock(*access_lock);
refs--;
if(refs==0)
return true;
return false;
}
void *operator new(size_t /*n*/)
{
return this_allocator().allocate(1);
}
void operator delete(void *p)
{
this_allocator().deallocate(reinterpret_cast<mem_cache *>(p),1);
}
}; // mem cache
booster::intrusive_ptr<base_cache> thread_cache_factory(unsigned items)
{
return new mem_cache<thread_settings>(items);
}
#if !defined(CPPCMS_NO_PREFOK_CACHE)
booster::intrusive_ptr<base_cache> process_cache_factory(size_t memory,unsigned items)
{
process_settings::init(memory);
return new mem_cache<process_settings>(items);
}
#endif
} // impl
} // cppcms
#endif // CPPCMS_NO_PREFOK_CACHE
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

CppCMS Framework
暂无标签
MIT
使用 MIT 开源许可协议
, MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yyxtyk/cppcms.git
git@gitee.com:yyxtyk/cppcms.git
yyxtyk
cppcms
cppcms
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /