Пример #1
0
string checkCache (string httpMsg) {
  
  string cacheItem;
  string cacheKey = "";
  cacheKey.append (getReqType(httpMsg));
  cacheKey.append (getUri(httpMsg));
  cacheKey.append (getHost(httpMsg));

  // Critical Section
  pthread_mutex_lock(&cacheLock);
  if (cacheMap.size() != 0) {
    try {
      tr1::unordered_map<string,string>::const_iterator got = cacheMap.find(cacheKey);
      if (got == cacheMap.end()) {
	cacheItem = "";
      } else {
	cacheItem = got->second;
      }
    } catch (...) {
      cacheItem = "";
    }
  } else {
    cacheItem = "";
  }
  pthread_mutex_unlock(&cacheLock);

  return cacheItem;
}
Пример #2
0
void addToCache (string request, string response) {
  
  string cacheKey = "";
  cacheKey.append (getReqType(request));
  cacheKey.append (getUri(request));
  cacheKey.append (getHost(request));

  // Critical Section
  pthread_mutex_lock(&cacheLock);
  try {
  if (cacheMap.size() < MAXCACHESIZE) {
    cacheMap.insert (make_pair<string, string>(cacheKey, response));
  } else {
    tr1::unordered_map<string,string>::const_iterator got = cacheMap.begin();
    cacheMap.erase (got);
    cacheMap.insert (make_pair<string, string>(cacheKey, response));
  }
  } catch (...) {
  }
  pthread_mutex_unlock(&cacheLock);
}