1 /***************************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "client-conf.h"
37 T Cache
<T
>::momentaryRequest(const std::string
& name
)
39 if (conf
.cacheEnabled
) {
40 CacheMapIter it
= map
.find(name
);
41 if (it
!= map
.end()) {
42 // Log::info("Cache", name + ": requested (cached)");
43 CacheEntry
& entry
= it
->second
;
44 // Set lastUsed to now because it won't be used
45 // by the time garbageCollect() gets to it.
46 entry
.lastUsed
= GameWindow::instance().time();
47 return entry
.resource
;
50 Log::info("Cache", name
+ ": requested");
55 T Cache
<T
>::lifetimeRequest(const std::string
& name
)
57 if (conf
.cacheEnabled
) {
58 typename
CacheMap::iterator it
= map
.find(name
);
59 if (it
!= map
.end()) {
60 // Log::info("Cache", name + ": requested (cached)");
61 CacheEntry
& entry
= it
->second
;
62 entry
.lastUsed
= IN_USE_NOW
;
63 return entry
.resource
;
66 Log::info("Cache", name
+ ": requested");
71 void Cache
<T
>::momentaryPut(const std::string
& name
, T data
)
73 if (!conf
.cacheEnabled
)
76 entry
.resource
= data
;
77 time_t now
= GameWindow::instance().time();
83 void Cache
<T
>::lifetimePut(const std::string
& name
, T data
)
85 if (!conf
.cacheEnabled
)
88 entry
.resource
= data
;
89 entry
.lastUsed
= IN_USE_NOW
;
94 void Cache
<T
>::garbageCollect()
96 if (!conf
.cacheEnabled
)
98 time_t now
= GameWindow::instance().time();
99 typedef std::vector
<std::string
> StringVector
;
101 for (CacheMapIter it
= map
.begin(); it
!= map
.end(); it
++) {
102 const std::string
& name
= it
->first
;
103 CacheEntry
& cache
= it
->second
;
104 bool unused
= !cache
.resource
|| cache
.resource
.unique();
107 if (cache
.lastUsed
== IN_USE_NOW
) {
108 cache
.lastUsed
= now
;
109 // Log::info("Cache", name + ": unused");
111 else if (now
> cache
.lastUsed
+ conf
.cacheTTL
*1000) {
112 dead
.push_back(name
);
113 Log::info("Cache", name
+ ": purged");
116 for (StringVector::iterator it
= dead
.begin(); it
!= dead
.end(); it
++)