-
Notifications
You must be signed in to change notification settings - Fork 7
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
Stephen Plaza
committed
Aug 29, 2015
1 parent
477cdaf
commit 6faa9f5
Showing
5 changed files
with
203 additions
and
4 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
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,39 @@ | ||
/*! | ||
* Caches requests from DVID. | ||
*/ | ||
|
||
#ifndef DVIDCACHE | ||
#define DVIDCACHE | ||
|
||
#include <tr1/unordered_map> | ||
#include <boost/thread/mutex.hpp> | ||
#include "BinaryData.h" | ||
|
||
namespace libdvid { | ||
|
||
class DVIDCache { | ||
public: | ||
static DVIDCache* get_cache(); | ||
void set_cache_size(unsigned long long max_size_); | ||
void set(std::string key, BinaryDataPtr data); | ||
BinaryDataPtr get(std::string key); | ||
|
||
private: | ||
DVIDCache() : cache_stamp(0), max_size(0), curr_size(0) {} | ||
void delete_old_cache(); | ||
void clear_cache(); | ||
|
||
static boost::mutex mutex; | ||
|
||
std::tr1::unordered_map<std::string, BinaryDataPtr> cache; | ||
std::tr1::unordered_map<std::string, unsigned long long> cache_stamps; | ||
unsigned long long cache_stamp; | ||
unsigned long long max_size; | ||
unsigned long long curr_size; | ||
|
||
}; | ||
|
||
} | ||
|
||
|
||
#endif |
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,90 @@ | ||
#include "DVIDCache.h" | ||
#include <map> | ||
|
||
using std::map; using std::string; | ||
using std::tr1::unordered_map; | ||
|
||
namespace libdvid { | ||
|
||
boost::mutex DVIDCache::mutex; | ||
|
||
DVIDCache* DVIDCache::get_cache() | ||
{ | ||
boost::mutex::scoped_lock lock(mutex); | ||
static DVIDCache cache; | ||
return &cache; | ||
} | ||
|
||
void DVIDCache::set_cache_size(unsigned long long max_size_) | ||
{ | ||
boost::mutex::scoped_lock lock(mutex); | ||
clear_cache(); | ||
max_size = max_size_; | ||
} | ||
|
||
void DVIDCache::set(std::string key, BinaryDataPtr data) | ||
{ | ||
boost::mutex::scoped_lock lock(mutex); | ||
|
||
if (max_size > 0) { | ||
BinaryDataPtr ptr = cache[key]; | ||
|
||
if (ptr) { | ||
curr_size -= ptr->length(); | ||
} | ||
curr_size += data->length(); | ||
cache[key] = data; | ||
cache_stamps[key] = cache_stamp; | ||
++cache_stamp; | ||
|
||
if (curr_size > max_size) { | ||
delete_old_cache(); | ||
} | ||
} | ||
} | ||
|
||
BinaryDataPtr DVIDCache::get(std::string key) | ||
{ | ||
boost::mutex::scoped_lock lock(mutex); | ||
BinaryDataPtr ptr; | ||
|
||
if (max_size > 0) { | ||
if (cache.find(key) != cache.end()) { | ||
ptr = cache[key]; | ||
cache_stamps[key] = cache_stamp; | ||
++cache_stamp; | ||
} | ||
} | ||
return ptr; | ||
} | ||
|
||
void DVIDCache::clear_cache() | ||
{ | ||
cache.clear(); | ||
cache_stamps.clear(); | ||
cache_stamp = 0; | ||
max_size = 0; | ||
curr_size = 0; | ||
} | ||
|
||
|
||
void DVIDCache::delete_old_cache() | ||
{ | ||
map<unsigned long long, string> cache_order; | ||
unsigned long long delsize = 0; | ||
|
||
for (unordered_map<string, unsigned long long>::iterator iter = cache_stamps.begin(); iter != cache_stamps.end(); ++iter) { | ||
cache_order[iter->second] = iter->first; | ||
} | ||
|
||
for (map<unsigned long long, string>::iterator iter = cache_order.begin(); iter != cache_order.end(); ++iter) { | ||
delsize += cache[iter->second]->length(); | ||
cache.erase(iter->second); | ||
cache_stamps.erase(iter->second); | ||
if (delsize > (max_size/2)) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
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