1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2017 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "nel/gui/http_cache.h"
21 using namespace NLMISC
;
27 #if defined(GCC_VERSION) && !defined(CLANG_VERSION) && defined(NL_ISO_CPP0X_AVAILABLE) && (GCC_VERSION <= 40804)
28 // hack to fix std::map::erase wrong return type (void instead of iterator in C++11) in GCC 4.8.4
29 #undef NL_ISO_CPP0X_AVAILABLE
34 CHttpCache
* CHttpCache::instance
= NULL
;
36 CHttpCache
* CHttpCache::getInstance()
40 instance
= new CHttpCache();
46 void CHttpCache::release()
52 CHttpCache::CHttpCache()
57 CHttpCache::~CHttpCache()
62 void CHttpCache::setCacheIndex(const std::string
& fname
)
64 _IndexFilename
= fname
;
68 CHttpCacheObject
CHttpCache::lookup(const std::string
& fname
)
73 if (_List
.count(fname
) > 0)
76 return CHttpCacheObject();
79 void CHttpCache::store(const std::string
& fname
, const CHttpCacheObject
& data
)
87 void CHttpCache::init()
94 if (_IndexFilename
.empty() || !CFile::fileExists(_IndexFilename
))
98 if (!in
.open(_IndexFilename
)) {
99 nlwarning("Unable to open %s for reading", _IndexFilename
.c_str());
106 void CHttpCacheObject::serial(NLMISC::IStream
& f
)
110 f
.serial(LastModified
);
114 void CHttpCache::serial(NLMISC::IStream
& f
)
116 // saved state is ignored when version checks fail
121 f
.serialCheck(NELID("hcaC"));
122 f
.serialCheck(NELID("xdIe"));
130 for (uint k
= 0; k
< numFiles
; ++k
)
135 CHttpCacheObject obj
;
143 uint32 numFiles
= _List
.size();
146 for (THttpCacheMap::iterator it
= _List
.begin(); it
!= _List
.end(); ++it
)
148 std::string
fname(it
->first
);
151 (*it
).second
.serial(f
);
156 nlwarning("Invalid cache index format (%s)", _IndexFilename
.c_str());
161 void CHttpCache::pruneCache()
163 if (_List
.size() < _MaxObjects
)
166 size_t mustDrop
= _List
.size() - _MaxObjects
;
171 // if we over object limit, then start removing expired objects
172 // this does not guarantee that max limit is reached
173 for (THttpCacheMap::iterator it
= _List
.begin(); it
!= _List
.end();)
175 if (it
->second
.Expires
<= currentTime
)
177 #ifdef NL_ISO_CPP0X_AVAILABLE
178 it
= _List
.erase(it
);
180 THttpCacheMap::iterator itToErase
= it
++;
181 _List
.erase(itToErase
);
195 void CHttpCache::flushCache()
197 if (_IndexFilename
.empty())
203 if (!out
.open(_IndexFilename
))
205 nlwarning("Unable to open %s for writing", _IndexFilename
.c_str());