Skip to content

Instantly share code, notes, and snippets.

@payden
Created July 15, 2014 15:57
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/1c029a85288e52db20cb to your computer and use it in GitHub Desktop.
Save payden/1c029a85288e52db20cb to your computer and use it in GitHub Desktop.
Yet another thing
protected function _set_item_in_memcache($type, $id, $obj) {
$mc_key = "cache_{$type}_{$id}";
$this->cache->set($mc_key, json_encode($obj));
}
protected function _populate_item_from_memcache($type, $id) {
$mc_key = "cache_{$type}_{$id}";
$rv = $this->cache->get($mc_key);
if ($rv === false) return;
$this->_caches[$type][$id] = json_decode($rv, true);
}
protected function _get_item_from_cache($callable, $type, $id) {
if (!isset($this->_caches[$type])) {
$this->_caches[$type] = array();
}
if (!isset($this->_caches[$type][$id])) {
$this->_populate_item_from_memcache($type, $id);
if (!isset($this->_caches[$type][$id])) {
$this->_caches[$type][$id] = call_user_func_array($callable, array($id));
$this->_set_item_in_memcache($type, $id, $this->_caches[$type][$id]);
}
}
}
protected function _get_contact_info($contact_id) {
//check local process cache first
$retrieval_func = $this->contacts->get_contact_info;
return $this->_get_item_from_cache($retrieval_func, 'contact', $contact_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment