Store an object in the cache.
public set ( string $key, string $data, integer $lifetime = null ) | ||
$key | string | Object ID used as the caching key. |
$data | string | Data to store in the cache. |
$lifetime | integer | Object lifetime - i.e. the time before the data becomes available for garbage collection, in seconds. If null use the default Horde GC time. If 0 will not be GC'd. |
/** * TODO */ protected function _getAddressBook(array $fields = array()) { $key = 'turba_fb_getAddressBook|' . $GLOBALS['registry']->getAuth() . '|' . md5(implode('.', $fields)); if ($values = $this->_cache->get($key, 3600)) { return json_decode($values, true); } $cleanfields = implode(', ', $this->_prepareFields($fields)); try { $fql = 'SELECT ' . $cleanfields . ' FROM user WHERE uid IN (' . 'SELECT uid2 FROM friend WHERE uid1=' . $this->_facebook->auth->getLoggedInUser() . ')'; $results = $this->_facebook->fql->run($fql); } catch (Horde_Service_Facebook_Exception $e) { Horde::log($e, 'ERR'); if ($e->getCode() == Horde_Service_Facebook_ErrorCodes::API_EC_PARAM_SESSION_KEY) { throw new Turba_Exception(_("You are not connected to Facebook. Create a Facebook connection in the Global Preferences.")); } throw new Turba_Exception($e); } // Now pull out the results that are arrays $addressbook = array(); foreach ($results as &$result) { $addressbook[$result['uid']] = $this->_fqlToTurba($fields, $result); } $this->_cache->set($key, json_encode($addressbook)); return $addressbook; }
/** * Sets the cached group list. * * @param array $list A group list. * @param string $user A user name. */ protected function _setListCache(array $list, $user = null) { $sig = $this->_sig('list'); if (!is_null($user)) { $sig .= '_' . $user; } try { $this->_cache->set($sig, serialize($list)); } catch (Horde_Cache_Exception $e) { } }
/** * Get user groups */ public function getGroups() { $groups = $this->_cache->get('folksGroups' . $this->_user, $GLOBALS['conf']['cache']['default_lifetime']); if ($groups) { return unserialize($groups); } else { $groups = $this->_getGroups(); if ($groups instanceof PEAR_Error) { return $groups; } $this->_cache->set('folksGroups' . $this->_user, serialize($groups)); return $groups; } }
/** * Returns a Horde_History_Log corresponding to the named history entry, * with the data retrieved appropriately. * * @param string $guid The name of the history entry to retrieve. * * @return Horde_History_Log A Horde_History_Log object. * * @throws Horde_History_Exception */ public function getHistory($guid) { if (!is_string($guid)) { throw new Horde_History_Exception('The guid needs to be a string!'); } if ($this->_cache && ($history = @unserialize($this->_cache->get('horde:history:' . $guid, 0)))) { return $history; } $history = $this->_getHistory($guid); if ($this->_cache) { $this->_cache->set('horde:history:' . $guid, serialize($history), 0); } return $history; }
/** * List the available library descriptions. * * @return array The list of libraries from pear.horde.org. */ public function listDescriptions() { // Cache the list from pear.horde.org for one day if ($descriptions = $this->_cache->get(__CLASS__ . '::descriptions2', 86400)) { return unserialize($descriptions); } $components = $this->_getComponents(); $descriptions = array(); foreach ($components as $component) { $descriptions[$component->name] = $component->description; } ksort($descriptions); $this->_cache->set(__CLASS__ . '::descriptions2', serialize($descriptions)); return $descriptions; }
/** * Updates the cache. */ public function save() { $lifetime = $this->_params['lifetime']; foreach ($this->_update as $mbox => $val) { $s =& $this->_slicemap[$mbox]; try { if (!empty($val['add'])) { if ($s['c'] <= $this->_params['slicesize']) { $val['slice'][] = $s['i']; $this->_loadSlice($mbox, $s['i']); } $val['slicemap'] = true; foreach (array_keys(array_flip($val['add'])) as $uid) { if ($s['c']++ > $this->_params['slicesize']) { $s['c'] = 0; $val['slice'][] = ++$s['i']; $this->_loadSlice($mbox, $s['i']); } $s['s'][$uid] = $s['i']; } } if (!empty($val['slice'])) { $d =& $this->_data[$mbox]; $val['slicemap'] = true; foreach (array_keys(array_flip($val['slice'])) as $slice) { $data = array(); foreach (array_keys($s['s'], $slice) as $uid) { $data[$uid] = is_array($d[$uid]) ? serialize($d[$uid]) : $d[$uid]; } $this->_cache->set($this->_getCid($mbox, $slice), serialize($data), $lifetime); } } if (!empty($val['slicemap'])) { $this->_cache->set($this->_getCid($mbox, 'slicemap'), serialize($s), $lifetime); } } catch (Horde_Exception $e) { } } $this->_update = array(); }
/** * Fetches a template from the specified file and return the parsed * contents. * * @param string $filename The file to fetch the template from. * * @return string The parsed template. */ public function fetch($filename = null) { $file = $this->_basepath . $filename; $force = $this->getOption('forcecompile'); if (!is_null($filename) && $file != $this->_templateFile) { $this->_template = $this->_templateFile = null; } /* First, check for a cached compiled version. */ $parts = array('horde_template', filemtime($file), $file); if ($this->getOption('gettext')) { $parts[] = setlocale(LC_ALL, 0); } $cacheid = implode('|', $parts); if (!$force && is_null($this->_template) && $this->_cache) { $this->_template = $this->_cache->get($cacheid, 0); if ($this->_template === false) { $this->_template = null; } } /* Parse and compile the template. */ if ($force || is_null($this->_template)) { $this->_template = str_replace("\n", " \n", file_get_contents($file)); $this->_parse(); if ($this->_cache) { $this->_cache->set($cacheid, $this->_template); if ($this->_logger) { $this->_logger->log(sprintf('Saved compiled template file for "%s".', $file), 'DEBUG'); } } } $this->_templateFile = $file; /* Template debugging. */ if ($this->getOption('debug')) { echo '<pre>' . htmlspecialchars($this->_template) . '</pre>'; } return $this->parse(); }
/** * TODO * * @param array $opts Options: * - 'file': (string) The filename to process. * REQUIRED for this driver. * - 'range': (array) The patchsets to process. * DEFAULT: None (all patchsets are processed). * - 'timezone': (string) The current timezone. * * @return Horde_Vcs_Patchset Patchset object. */ public function getPatchset($opts = array()) { $class = 'Horde_Vcs_Patchset_' . $this->_driver; if (!is_array($opts)) { $opts = array(); } ksort($opts); $cacheId = implode('|', array($class, $this->sourceroot, serialize($opts), $this->_cacheVersion)); if (!empty($this->_cache)) { if (isset($opts['file']) && file_exists($opts['file'])) { $ctime = time() - filemtime($opts['file']); } else { $ctime = 60; } if ($this->_cache->exists($cacheId, $ctime)) { return unserialize($this->_cache->get($cacheId, $ctime)); } } $ob = new $class($this, $opts); if (!empty($this->_cache)) { $this->_cache->set($cacheId, serialize($ob)); } return $ob; }
/** * Set cache value */ protected function _setCache($key, $value, $thread = 0) { $key = $this->_getCacheKey($key, $thread); return $this->_cache->set($key, $value); }
/** * Load application information from registry config files. */ protected function _loadApplications() { global $cli, $injector; if (!empty($this->_interfaces)) { return; } /* First, try to load from cache. */ if (!isset($cli) && !$this->isTest()) { if (Horde_Util::extensionExists('apc')) { $cstorage = 'Horde_Cache_Storage_Apc'; } elseif (Horde_Util::extensionExists('xcache')) { $cstorage = 'Horde_Cache_Storage_Xcache'; } else { $cstorage = 'Horde_Cache_Storage_File'; } $cache = new Horde_Cache(new $cstorage(array('no_gc' => true, 'prefix' => 'horde_registry_cache_')), array('lifetime' => 0, 'logger' => $injector->getInstance('Horde_Log_Logger'))); if (($cid = $this->_cacheId()) && ($cdata = $cache->get($cid, 0))) { try { list($this->applications, $this->_interfaces) = $injector->getInstance('Horde_Pack')->unpack($cdata); return; } catch (Horde_Pack_Exception $e) { } } } $config = new Horde_Registry_Registryconfig($this); $this->applications = $config->applications; $this->_interfaces = $config->interfaces; if (!isset($cache)) { return; } /* Need to determine hash of generated data, since it is possible that * there is dynamic data in the config files. This only needs to * be done once per session. */ $packed_data = $injector->getInstance('Horde_Pack')->pack(array($this->applications, $this->_interfaces)); $cid = $this->_cacheId($packed_data); if (!$cache->exists($cid, 0)) { $cache->set($cid, $packed_data); } }
/** * Writes values to the cache handler. * * The key is automatically prefixed to avoid collisions when using * different adapters or different configurations. * * @since Horde_Db 2.1.0 * * @param string $key A cache key. * @param string $value A value. */ public function cacheWrite($key, $value) { $this->_cache->set($this->_cacheKey($key), $value); }
/** * Create the generation hashes (arrays) for route lookups * * @return void */ protected function _createGens() { // Checked for a cached generator dictionary for $this->matchList if ($this->cache) { $cacheKey = 'horde.routes.' . sha1(serialize($this->matchList)); $cachedDict = $cache->get($cacheKey, $this->cacheLifetime); if ($gendict = @unserialize($cachedDict)) { $this->_gendict = $gendict; $this->_createdGens = true; return; } } // Use keys temporarily to assemble the list to avoid excessive // list iteration testing with foreach. We include the '*' in the // case that a generate contains a controller/action that has no // hardcodes. $actionList = $controllerList = array('*' => true); // Assemble all the hardcoded/defaulted actions/controllers used foreach ($this->matchList as $route) { if ($route->static) { continue; } if (isset($route->defaults['controller'])) { $controllerList[$route->defaults['controller']] = true; } if (isset($route->defaults['action'])) { $actionList[$route->defaults['action']] = true; } } $actionList = array_keys($actionList); $controllerList = array_keys($controllerList); // Go through our list again, assemble the controllers/actions we'll // add each route to. If its hardcoded, we only add it to that dict key. // Otherwise we add it to every hardcode since it can be changed. $gendict = array(); // Our generated two-deep hash foreach ($this->matchList as $route) { if ($route->static) { continue; } $clist = $controllerList; $alist = $actionList; if (in_array('controller', $route->hardCoded)) { $clist = array($route->defaults['controller']); } if (in_array('action', $route->hardCoded)) { $alist = array($route->defaults['action']); } foreach ($clist as $controller) { foreach ($alist as $action) { if (in_array($controller, array_keys($gendict))) { $actiondict =& $gendict[$controller]; } else { $gendict[$controller] = array(); $actiondict =& $gendict[$controller]; } if (in_array($action, array_keys($actiondict))) { $tmp = $actiondict[$action]; } else { $tmp = array(array(), array()); } $tmp[0][] = $route; $actiondict[$action] = $tmp; } } } if (!isset($gendict['*'])) { $gendict['*'] = array(); } // Write to the cache if ($this->cache) { $this->cache->set($cacheKey, serialize($gendict), $this->cacheLifetime); } $this->_gendict = $gendict; $this->_createdGens = true; }
/** * Cache list data. * * @param string $list_id ID of the connection matching the list. * @param string $data The data to be cached. * * @return NULL */ public function storeList($list_id, $data) { $this->horde_cache->set($list_id, $data, 0); }