-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathxBlogCache.cpp
118 lines (107 loc) · 3.01 KB
/
xBlogCache.cpp
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
/*
* ----------------------------------------------------------------------------
* Copyright (c) 2012-2013, xSky <guozhw at gmail dot com>
* All rights reserved.
* Distributed under GPL license.
* ----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include "xBlogCache.h"
#include "xBlogBase.h"
#include "xConfig.h"
xBlogCache *xBlogCache::GetInstance()
{
static xBlogCache gCache;
return &gCache;
}
string &xBlogCache::GetPage(const uint32 pageid)
{
XLOCK(mPageCacheLock);
mPageCacheDataMap[pageid].count++;
return mPageCacheDataMap[pageid].strData;
}
void xBlogCache::SetPage(const uint32 pageid, const string & data)
{
XLOCK(mPageCacheLock);
mPageCacheDataMap[pageid].strData = data;
}
void xBlogCache::delPage(const uint32 pageid)
{
XLOCK(mPageCacheLock);
if (mPageCacheDataMap.size()>0)
{
log_debug("xBlogCache::delPage cache get =%d \r\n", mPageCacheDataMap.size());
CACHEDATAMAPITER iter = mPageCacheDataMap.find(pageid);
if(iter!=mPageCacheDataMap.end())
{
mPageCacheDataMap.erase(iter);
}
}
}
string &xBlogCache::GetPost(const uint32 postid)
{
XLOCK(mPostCacheLock);
return mPostCacheDataMap[postid].strData;
}
void xBlogCache::SetPost(const uint32 postid, const string & data)
{
XLOCK(mPostCacheLock);
mPostCacheDataMap[postid].strData = data;
}
void xBlogCache::delPost(const uint32 postid)
{
XLOCK(mPostCacheLock);
if (mPostCacheDataMap.size()>0)
{
log_debug("xBlogCache::delPage cache get =%d \r\n", mPostCacheDataMap.size());
CACHEDATAMAPITER iter = mPostCacheDataMap.find(postid);
if(iter!=mPostCacheDataMap.end())
{
mPostCacheDataMap.erase(iter);
}
}
}
void xBlogCache::CacheClear()
{
{
XLOCK(mPostCacheLock);
mPostCacheDataMap.clear();
}
{
XLOCK(mPageCacheLock);
mPageCacheDataMap.clear();
}
}
void xBlogCache::OnTimer()
{
uint32 now = time(NULL);
if(now - timestamp<Config::GetInstance()->xBlogAppConfig.CacheTimer)
{
return;
}
timestamp = now;
{
XLOCK(mPostCacheLock);
if (mPostCacheDataMap.size()>0)
{
log_info("xBlogCache::OnTimer cache get =%d \r\n", mPostCacheDataMap.size());
CACHEDATAMAPITER iter = mPostCacheDataMap.begin();
for(;iter!=mPostCacheDataMap.end();)
{
mPostCacheDataMap.erase(iter++);
}
}
}
{
XLOCK(mPageCacheLock);
if (mPageCacheDataMap.size()>0)
{
log_info("xBlogCache::OnTimer cache get =%d \r\n", mPageCacheDataMap.size());
CACHEDATAMAPITER iter = mPageCacheDataMap.begin();
for (; iter != mPageCacheDataMap.end();)
{
mPageCacheDataMap.erase(iter++);
}
}
}
}