-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
702cc90
commit b3a450d
Showing
12 changed files
with
221 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#pragma once | ||
|
||
#include "stdafx.h" | ||
#include <list> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
template <typename K, typename V> | ||
class LruCache { | ||
public: | ||
LruCache(unsigned int capacity) : capacity_(capacity) {}; | ||
void set(const K& k, const V& v); | ||
bool get(const K& k, V& v); | ||
bool erase(const K& k); | ||
void setCacheSize(unsigned int capacity) { capacity_ = capacity; }; | ||
int getCacheSize(); | ||
private: | ||
unsigned int capacity_; | ||
std::list<std::pair <K, V>> cacheList; | ||
using list_iterator = typename std::list <std::pair <K, V>> ::iterator; | ||
std::unordered_map <K, list_iterator>cacheMap; | ||
}; | ||
|
||
template <typename K, typename V> | ||
int LruCache<K, V>::getCacheSize() { | ||
int cacheSize = cacheList.size(); | ||
|
||
return cacheSize; | ||
} | ||
|
||
template <typename K, typename V> | ||
void LruCache <K, V>::set(const K& k, const V& v) { | ||
auto itr = cacheMap.find(k); | ||
|
||
if (itr != cacheMap.end()) { | ||
itr->second->second = v; | ||
cacheList.splice(cacheList.begin(), cacheList, itr->second); | ||
itr->second = cacheList.begin(); | ||
} | ||
else { | ||
while (cacheMap.size() >= capacity_) { | ||
cacheMap.erase(cacheList.back().first); | ||
cacheList.pop_back(); | ||
} | ||
cacheList.emplace_front(k, v); | ||
cacheMap.emplace(k, cacheList.begin()); | ||
} | ||
} | ||
|
||
template <typename K, typename V> | ||
bool LruCache <K, V>::get(const K& k, V& v) { | ||
auto itr = cacheMap.find(k); | ||
bool ret = false; | ||
|
||
if (itr != cacheMap.end()) { | ||
cacheList.splice(cacheList.begin(), cacheList, itr->second); | ||
itr->second = cacheList.begin(); // update iterator | ||
v = itr->second->second; | ||
ret = true; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
template <typename K, typename V> | ||
bool LruCache <K, V>::erase(const K& k) { | ||
auto itr = cacheMap.find(k); | ||
bool ret = false; | ||
|
||
if (itr != cacheMap.end()) { | ||
cacheList.erase(itr->second); | ||
cacheMap.erase(itr); | ||
ret = true; | ||
} | ||
|
||
return ret; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "stdafx.h" | ||
#include "thread_pool.h" | ||
|
||
class mb_initquit : public initquit { | ||
public: | ||
void on_init() { | ||
} | ||
void on_quit() { | ||
simple_thread_pool::instance().exit(); | ||
} | ||
}; | ||
|
||
static initquit_factory_t<mb_initquit> g_myinitquit_factory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "stdafx.h" | ||
#include "query_cache.h" | ||
|
||
// cache entries expire after 1 hour | ||
#define CACHE_EXPIRATION 3600 | ||
|
||
namespace mb { | ||
|
||
bool query_cache::get(str8 url, str8* val) | ||
{ | ||
int hash = hashCode(url); | ||
CacheObj cache_obj; | ||
|
||
bool hit = cache.get(hash, cache_obj); | ||
if (hit) { | ||
if (pfc::fileTimeWtoU(cache_obj.cached_time) > pfc::fileTimeWtoU(pfc::fileTimeNow()) - CACHE_EXPIRATION) { | ||
*val = cache_obj.response; | ||
} | ||
else { | ||
// stale | ||
hit = false; | ||
} | ||
} | ||
return hit; | ||
} | ||
|
||
void query_cache::set(str8 url, str8 response) | ||
{ | ||
int hash = hashCode(url); | ||
CacheObj cache_obj; | ||
|
||
cache_obj.cached_time = pfc::fileTimeNow(); | ||
cache_obj.response = response; | ||
|
||
cache.set(hash, cache_obj); | ||
} | ||
|
||
int query_cache::hashCode(str8 text) { | ||
int hash = 0, strlen = text.length(), i; | ||
char character; | ||
if (strlen == 0) | ||
return hash; | ||
for (i = 0; i < strlen; i++) { | ||
character = text[i]; | ||
hash = (31 * hash) + (character); | ||
} | ||
return hash; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
#include "LruCache.cpp" | ||
|
||
namespace mb { | ||
struct CacheObj { | ||
t_filetimestamp cached_time; | ||
str8 response; | ||
}; | ||
|
||
class query_cache | ||
{ | ||
public: | ||
query_cache() : cache(100) {}; // setting max size of cache to 100 | ||
bool get(str8 url, str8* val); | ||
void set(str8 url, str8 response); | ||
|
||
private: | ||
int hashCode(str8 text); | ||
LruCache<int, CacheObj> cache; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters