Permalink
Browse files

cache optimization and ifset() added

  • Loading branch information...
1 parent cdc2169 commit bee8121a6943e210ca2b21aafe27cf263e9478cf @infusion infusion committed Jun 23, 2011
Showing with 76 additions and 96 deletions.
  1. +10 −10 Cache/Apc.php
  2. +32 −67 Cache/Disk.php
  3. +2 −2 Cache/Memcached.php
  4. +2 −3 Cache/Redis.php
  5. +16 −14 Model.php
  6. +14 −0 common.php
View
@@ -16,9 +16,9 @@ class PPI_Cache_Apc implements PPI_Cache_Interface {
* @param string $p_sKey The Key
* @return mixed
*/
- function get($p_sKey) { return apc_fetch($p_sKey); }
+ public function get($p_sKey) { return apc_fetch($p_sKey); }
- function init() {}
+ public function init() {}
/**
* Set a value in the cache
@@ -27,7 +27,7 @@ function init() {}
* @param mixed $p_iTTL The Time To Live. Integer or String (strtotime)
* @return boolean True on succes, False on failure.
*/
- function set($p_sKey, $p_mData, $p_mTTL = 0) {
+ public function set($p_sKey, $p_mData, $p_mTTL = 0) {
return apc_store($p_sKey, $p_mData, (is_numeric($p_mTTL) ? $p_mTTL : strtotime($p_mTTL)));
}
@@ -36,21 +36,21 @@ function set($p_sKey, $p_mData, $p_mTTL = 0) {
* @param string $p_mKey The Key
* @return boolean
*/
- function exists($p_mKey) { return apc_exists($p_mKey); }
+ public function exists($p_mKey) { return apc_exists($p_mKey); }
/**
* Remove a key from the cache
* @param string $p_sKey The Key
* @return boolean
*/
- function remove($p_sKey) { return apc_delete($p_sKey); }
+ public function remove($p_sKey) { return apc_delete($p_sKey); }
/**
* Wipe the cache contents
*
* @return unknown
*/
- function clear() { return apc_clear_cache('user'); }
+ public function clear() { return apc_clear_cache('user'); }
/**
* Increment a numerical value
@@ -59,7 +59,7 @@ function clear() { return apc_clear_cache('user'); }
* @param numeric $p_iInc The incremental value
* @return numeric
*/
- function increment($p_sKey, $p_iInc = 1) { return apc_inc($p_sKey, $p_iInc); }
+ public function increment($p_sKey, $p_iInc = 1) { return apc_inc($p_sKey, $p_iInc); }
/**
* Enter description here...
@@ -68,15 +68,15 @@ function increment($p_sKey, $p_iInc = 1) { return apc_inc($p_sKey, $p_iInc); }
* @param numeric $p_iDec The decremental value
* @return numeric
*/
- function decrement($p_sKey, $p_iDec = 1) { return apc_dec($p_sKey, $p_iDec); }
+ public function decrement($p_sKey, $p_iDec = 1) { return apc_dec($p_sKey, $p_iDec); }
/**
* Check if the APC extension has been loaded and is enabled in its configuration.
*
* @return boolean
*/
- function enabled() {
- return extension_loaded('apc') && (php_sapi_name() === 'cli' && ini_get('apc.enabled'));
+ public function enabled() {
+ return extension_loaded('apc') && ini_get('apc.enabled') && in_array(php_sapi_name(), array('fpm', 'cli', 'cgi'));
}
}
View
@@ -24,51 +24,38 @@ class PPI_Cache_Disk implements PPI_Cache_Interface {
*/
protected $_options = array();
- function __construct(array $p_aOptions = array()) {
+ public function __construct(array $p_aOptions = array()) {
$this->_options = $p_aOptions;
- $this->_cacheDir = isset($p_aOptions['cache_dir'])
- ? $p_aOptions['cache_dir'] : APPFOLDER . 'Cache/Disk/';
+ $this->_cacheDir = ifset($p_aOptions['cache_dir'], APPFOLDER . 'Cache/Disk/');
}
- function init() {}
-
- /**
- * Get the data from the specified path
- *
- * @param string $p_sKey
- * @return mixed
- */
- protected function getData($p_sPath) {
- $sContent = file_exists($p_sPath) ? file_get_contents($p_sPath) : '';
- return $sContent != '' ? unserialize($sContent) : '';
- }
+ public function init() {}
/**
* Remove a key from the cache
* @param string $p_sKey The Key
+ * @param bool $p_bExist flag if we know of the existence
* @return boolean
*/
- function remove($p_sKey) {
- return $this->removeKeyByPath($this->getKeyCachePath($p_sKey))
- && $this->removeKeyByPath($this->getKeyMetaCachePath($p_sKey));
- }
+ public function remove($p_sKey, $p_bExists=false) {
- /**
- * Remove a file by path
- * @param string $p_sPath
- * @return boolean
- */
- function removeKeyByPath($p_sPath) {
- return file_exists($p_sPath) && unlink($p_sPath);
- }
+ $sPath = $this->getKeyCachePath($p_sKey);
+ if ($p_bExists || $this->exists($sPath)) {
+ unlink($sPath);
+ unlink($this->getKeyMetaCachePath($p_sKey));
+ return true;
+ }
+ return false;
+ }
/**
* Get the full path to a cache item
* @param string $p_sKey
* @return string
*/
protected function getKeyCachePath($p_sKey) {
+ // @TODO robert: add leveled key paths to avoid slow disk seeks
return $this->_cacheDir . 'default--' . $p_sKey;
}
@@ -83,41 +70,20 @@ protected function getKeyMetaCachePath($p_sKey) {
}
/**
- * Get the metadata for a chosen cache file
- *
- * @param string $p_sKey
- * @return array
- */
- protected function getMetaData($p_sKey) {
- return $this->getData($this->getKeyMetaCachePath($p_sKey));
- }
-
- /**
- * Set data by writing it to disk
- *
- * @param string $p_sPath
- * @param mixed $p_mData
- * @return integer
- */
- protected function setData($p_sPath, $p_mData) {
- return file_put_contents($p_sPath, serialize($p_mData), LOCK_EX) > 0;
- }
-
- /**
* Check if a key exists in the cache
* @param string $p_mKey The Key(s)
* @return boolean
*/
- function exists($p_sKey) {
+ public function exists($p_sKey) {
$sPath = $this->getKeyCachePath($p_sKey);
- if(file_exists($sPath) === false) {
+ if(false === file_exists($sPath)) {
return false;
}
- $aMetaData = $this->getMetaData($p_sKey);
+ $aMetaData = unserialize(file_get_contents($this->getKeyMetaCachePath($p_sKey)));
// See if the item has a ttl and if it has expired then we delete it.
- if(is_array($aMetaData) && ($aMetaData['ttl'] > 0 && (int) $aMetaData['expire_time'] < time())) {
+ if(is_array($aMetaData) && $aMetaData['ttl'] > 0 && $aMetaData['expire_time'] < time()) {
// Remove the cache item and its metadata file.
- $this->remove($p_sKey);
+ $this->remove($p_sKey, true); // if we don't expect the existence, we could get an endless loop!
return false;
}
return true;
@@ -130,12 +96,11 @@ function exists($p_sKey) {
* @param integer $p_iTTL The Time To Live
* @return boolean
*/
- function set($p_sKey, $p_mData, $p_iTTL = 0) {
+ public function set($p_sKey, $p_mData, $p_iTTL = 0) {
$sPath = $this->getKeyCachePath($p_sKey);
- if($this->exists($p_sKey)) {
- $this->remove($p_sKey);
- }
+
+ $this->remove($p_sKey);
$sCacheDir = dirname($sPath);
if(!is_dir($sCacheDir)) {
@@ -145,10 +110,10 @@ function set($p_sKey, $p_mData, $p_iTTL = 0) {
throw new PPI_Exception('Unable to create directory:<br>(' . $sCacheDir . ')');
}
}
- if(is_writeable($sCacheDir) === false) {
+ if(false === is_writeable($sCacheDir)) {
$aFileInfo = pathinfo(dirname($sPath));
chmod($sCacheDir, 775);
- if(is_writable($sCacheDir) === false) {
+ if(false === is_writable($sCacheDir)) {
throw new PPI_Exception('Unable to create cache file: ' . $p_sKey. '. Cache directory not writeable.<br>(' . $this->_cacheDir . ')<br>Current permissions: ');
}
}
@@ -158,28 +123,28 @@ function set($p_sKey, $p_mData, $p_iTTL = 0) {
'ttl' => $p_iTTL
);
- return $this->setData($sPath, $p_mData)
- && $this->setData($this->getKeyMetaCachePath($p_sKey), $aMetaData);
+ return file_put_contents($sPath, serialize($p_mData), LOCK_EX) > 0
+ && file_put_contents($this->getKeyMetaCachePath($p_sKey), serialize($aMetaData), LOCK_EX) > 0;
}
/**
* Get a value from cache
* @param string $p_sKey The Key
* @return mixed
*/
- function get($p_sKey, $p_mDefault = null) {
- if($this->exists($p_sKey) === false) {
+ public function get($p_sKey, $p_mDefault = null) {
+ if(false === $this->exists($p_sKey)) {
return $p_mDefault;
}
- return $this->getData($this->getKeyCachePath($p_sKey));
+ return unserialize(file_get_contents($this->getKeyCachePath($p_sKey)));
}
/**
* Check if this adapter is enabled or not.
*
* @return boolean
*/
- function enabled() { return true; }
+ public function enabled() { return true; }
/**
* Increment the value in the cache
@@ -188,7 +153,7 @@ function enabled() { return true; }
* @param $p_mIncrement The value to increment by
* @return void
*/
- function increment($p_sKey, $p_mIncrement) { }
+ public function increment($p_sKey, $p_mIncrement) { }
/**
* Decrement the value in the cache
@@ -197,6 +162,6 @@ function increment($p_sKey, $p_mIncrement) { }
* @param $p_mDecrement The value to decrement by
* @return void
*/
- function decrement($p_sKey, $p_mDecrement) { }
+ public function decrement($p_sKey, $p_mDecrement) { }
}
View
@@ -51,7 +51,7 @@ function init() {}
* @return mixed
*/
function get($p_sKey) {
- if($this->_serverAdded === false) {
+ if(false === $this->_serverAdded) {
$this->addServer('localhost');
}
return $this->_handler->get($p_sKey);
@@ -66,7 +66,7 @@ function get($p_sKey) {
* @return boolean
*/
function set($p_sKey, $p_mData, $p_mTTL = 0) {
- if($this->_serverAdded === false) {
+ if(false === $this->_serverAdded) {
$this->addServer('localhost');
}
return $this->_handler->set($p_sKey, $p_mData, (is_numeric($p_mTTL) ? $p_mTTL : strtotime($p_mTTL)));
View
@@ -53,11 +53,10 @@ function get($p_mKey) {
* @return boolean True on succes, False on failure.
*/
function set($p_sKey, $p_mData, $p_mTTL = null) {
- if($p_mTTL !== null && is_string($p_mData)) {
+ if(null !== $p_mTTL && is_string($p_mData)) {
$p_mTTL = strtotime($p_mTTL);
}
- $p_mTTL = $p_mTTL !== null ? $p_mTTL : $this->_defaults['expiry'];
- return $this->_handler->set($p_sKey, $p_mData, $p_mTTL);
+ return $this->_handler->set($p_sKey, $p_mData, ifset($p_mTTL, $this->_defaults['expiry']));
}
/**
Oops, something went wrong.

0 comments on commit bee8121

Please sign in to comment.