Some cleanup related to Cache Management

This commit is contained in:
Mark Vejvoda 2010-05-01 01:22:48 +00:00
parent 33084f8dbe
commit ff592be457
2 changed files with 20 additions and 11 deletions

View File

@ -31,7 +31,6 @@ class CacheManager
{ {
protected: protected:
static Mutex mutexCache; static Mutex mutexCache;
static std::map<std::string, bool> masterCacheList;
typedef enum { typedef enum {
cacheItemGet, cacheItemGet,
@ -40,13 +39,23 @@ protected:
template <typename T> template <typename T>
static T & manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) { static T & manageCachedItem(string cacheKey, T *value,CacheAccessorType accessor) {
// Here is the actual type-safe instantiation
static std::map<string, T > itemCache; static std::map<string, T > itemCache;
if(accessor == cacheItemSet) { if(accessor == cacheItemSet) {
if(value == NULL) { if(value == NULL) {
throw runtime_error("Unexpected NULL object setting cache value!"); try {
mutexCache.p();
if(itemCache.find(cacheKey) != itemCache.end()) {
itemCache.erase(cacheKey);
}
mutexCache.v();
}
catch(const std::exception &ex) {
mutexCache.v();
throw runtime_error(ex.what());
}
} }
masterCacheList[cacheKey] = true;
try { try {
mutexCache.p(); mutexCache.p();
itemCache[cacheKey] = *value; itemCache[cacheKey] = *value;
@ -57,17 +66,14 @@ protected:
throw runtime_error(ex.what()); throw runtime_error(ex.what());
} }
} }
// If this is the first access we return a default object of the type
return itemCache[cacheKey]; return itemCache[cacheKey];
} }
public: public:
CacheManager() { CacheManager() { }
~CacheManager() { }
}
~CacheManager() {
}
template <typename T> template <typename T>
static void setCachedItem(string cacheKey, const T value) { static void setCachedItem(string cacheKey, const T value) {
@ -77,6 +83,10 @@ public:
static T & getCachedItem(string cacheKey) { static T & getCachedItem(string cacheKey) {
return manageCachedItem<T>(cacheKey,NULL,cacheItemGet); return manageCachedItem<T>(cacheKey,NULL,cacheItemGet);
} }
template <typename T>
static void clearCachedItem(string cacheKey) {
return manageCachedItem<T>(cacheKey,NULL,cacheItemSet);
}
}; };

View File

@ -14,6 +14,5 @@
namespace Shared { namespace PlatformCommon { namespace Shared { namespace PlatformCommon {
Mutex CacheManager::mutexCache; Mutex CacheManager::mutexCache;
std::map<string, bool> CacheManager::masterCacheList;
}}//end namespace }}//end namespace