Skip to content

Instantly share code, notes, and snippets.

@payden
Created July 15, 2014 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save payden/9afa6e0da88ec7b5178c to your computer and use it in GitHub Desktop.
Save payden/9afa6e0da88ec7b5178c to your computer and use it in GitHub Desktop.
2 layer caching.
protected function _get_contact_info($contact_id) {
//check local process cache first
if (!isset($this->_contact_info_cache[$contact_id])) {
$memcache_key = "contact_info_cache_" . $contact_id;
$memcache_res = $this->cache->get($memcache_key);
//then check memcache
if ($memcache_res) {
$this->_contact_info_cache[$contact_id] = json_decode($memcache_res, true);
} else {
//fall back to DB retrieval and store in both memcache and local process cache.
$contact_info = $this->contacts->get_contact_info($contact_id);
$this->_contact_info_cache[$contact_id] = $contact_info;
$this->cache->set($memcache_key, json_encode($contact_info), 3600 * 12);
}
}
return $this->_contact_info_cache[$contact_id];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment