-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHttpLruCacheStrategy.cpp
More file actions
123 lines (108 loc) · 3.98 KB
/
Copy pathHttpLruCacheStrategy.cpp
File metadata and controls
123 lines (108 loc) · 3.98 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
/*
* Copyright 2017 Sony Corporation
*/
#include "easyhttpcpp/common/CoreLogger.h"
#include "HttpCacheInfo.h"
#include "HttpLruCacheStrategy.h"
using easyhttpcpp::common::CacheInfoWithDataSize;
using easyhttpcpp::common::LruCacheByDataSizeStrategy;
namespace easyhttpcpp {
static const std::string Tag = "HttpLruCacheStrategy";
HttpLruCacheStrategy::HttpLruCacheStrategy(size_t maxSize) :
LruCacheByDataSizeStrategy(maxSize)
{
}
HttpLruCacheStrategy::~HttpLruCacheStrategy()
{
}
bool HttpLruCacheStrategy::update(const std::string& key, CacheInfoWithDataSize::Ptr pCacheInfo)
{
bool ret = LruCacheByDataSizeStrategy::update(key, pCacheInfo);
if (ret) {
// check reserved remove.
HttpCacheInfo* pHttpCacheInfo = static_cast<HttpCacheInfo*> (pCacheInfo.get());
if (pHttpCacheInfo->getDataRefCount() == 0 && pHttpCacheInfo->isReservedRemove()) {
EASYHTTPCPP_LOG_D(Tag, "update: remove reserved cache. key=[%s]", key.c_str());
ret = LruCacheByDataSizeStrategy::remove(key);
}
}
return ret;
}
bool HttpLruCacheStrategy::remove(const std::string& key)
{
LruListMap::Iterator it = m_lruListMap.find(key);
if (it != m_lruListMap.end()) {
// check dataRefCount.
CacheInfoWithDataSize::Ptr pCacheInfo = *(it->second);
HttpCacheInfo* pHttpCacheInfo = static_cast<HttpCacheInfo*> (pCacheInfo.get());
if (pHttpCacheInfo->getDataRefCount() > 0) {
if (!pHttpCacheInfo->isReservedRemove()) {
// reserving remove.
pHttpCacheInfo->setReservedRemove(true);
EASYHTTPCPP_LOG_D(Tag, "remove: reserve remove. key=[%s]", key.c_str());
LruCacheByDataSizeStrategy::update(key, pCacheInfo);
}
return true;
}
return LruCacheByDataSizeStrategy::remove(key);
} else {
return false;
}
}
bool HttpLruCacheStrategy::clear(bool mayDeleteIfBusy)
{
LruKeyList keys;
bool ret = createRemoveList(keys, mayDeleteIfBusy);
for (LruKeyList::const_iterator it = keys.begin(); it != keys.end(); it++) {
if (!LruCacheByDataSizeStrategy::remove(*it)) {
EASYHTTPCPP_LOG_D(Tag, "clear: Failed to remove. key=[%s]", it->c_str());
ret = false;
}
}
return ret;
}
bool HttpLruCacheStrategy::createRemoveList(LruKeyList& keys, bool mayDeleteIfBusy)
{
bool ret = true;
for (LruList::const_iterator it = m_lruList.begin(); it != m_lruList.end(); it++) {
CacheInfoWithDataSize::Ptr pCacheInfo = *it;
if (mayDeleteIfBusy) {
keys.push_back(pCacheInfo->getKey());
} else {
HttpCacheInfo* pHttpCacheInfo = static_cast<HttpCacheInfo*> (pCacheInfo.get());
if (pHttpCacheInfo->getDataRefCount() == 0) {
keys.push_back(pCacheInfo->getKey());
} else {
ret = false;
}
}
}
return ret;
}
bool HttpLruCacheStrategy::createRemoveLruDataList(LruKeyList& keys, size_t removeSize)
{
size_t targetSize = 0;
LruList::reverse_iterator it = m_lruList.rbegin();
while (targetSize < removeSize) {
if (it == m_lruList.rend()) {
return false;
}
CacheInfoWithDataSize::Ptr pCacheInfo = *it;
HttpCacheInfo* pHttpCacheInfo = static_cast<HttpCacheInfo*> (pCacheInfo.get());
if (pHttpCacheInfo->getDataRefCount() == 0) {
keys.push_front(pCacheInfo->getKey());
targetSize += pCacheInfo->getDataSize();
EASYHTTPCPP_LOG_D(Tag, "createRemoveLruDataList: dataSize=%zu key=%s", pCacheInfo->getDataSize(),
pCacheInfo->getKey().c_str());
}
++it;
}
return true;
}
CacheInfoWithDataSize::Ptr HttpLruCacheStrategy::newCacheInfo(CacheInfoWithDataSize::Ptr pCacheInfo)
{
CacheInfoWithDataSize::Ptr pNewCacheInfo =
new HttpCacheInfo(*(static_cast<HttpCacheInfo*>(pCacheInfo.get())));
return pNewCacheInfo;
}
} /* namespace easyhttpcpp */