Skip to content

Commit

Permalink
Part of request #18734: use setAllowedOptions() in some containers (p…
Browse files Browse the repository at this point in the history
…atches by Ivo Nascimento)

git-svn-id: http://svn.php.net/repository/pear/packages/Cache/trunk@315100 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
cweiske committed Aug 17, 2011
1 parent b1f03df commit 24f53b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
10 changes: 4 additions & 6 deletions Container/db.php
Expand Up @@ -81,15 +81,13 @@ class Cache_Container_db extends Cache_Container

function Cache_Container_db($options)
{
if (!is_array($options) || !isset($options['dsn'])) {
$this->setAllowedOptions(array('dsn', 'cache_table'));
$this->setOptions($options);
if (!$this->hasBeenSet('dsn'))
{
return new Cache_Error('No dsn specified!', __FILE__, __LINE__);
}

$this->setOptions($options, array_merge($this->allowed_options, array('dsn', 'cache_table')));

if (!$this->dsn) {
return new Cache_Error('No dsn specified!', __FILE__, __LINE__);
}
$this->db = DB::connect($this->dsn, true);
if (PEAR::isError($this->db)) {
return new Cache_Error('DB::connect failed: ' . DB::errorMessage($this->db), __FILE__, __LINE__);
Expand Down
17 changes: 10 additions & 7 deletions Container/dbx.php
Expand Up @@ -111,14 +111,17 @@ class Cache_Container_dbx extends Cache_Container

function Cache_Container_dbx($options)
{
if (!is_array($options) ) {
return new Cache_Error('No options specified!', __FILE__, __LINE__);
}

$this->setOptions($options, array_merge($this->allowed_options, array('module','host','db','username','password', 'cache_table', 'persistent')));

if (!$this->module)
$this->setAllowedOptions(
array(
'module','host','db','username',
'password', 'cache_table', 'persistent'
)
);
$this->setOptions($options);

if (!$this->hasBeenSet('module')) {
return new Cache_Error('No module specified!', __FILE__, __LINE__);
}

$this->db = dbx_connect($this->module, $this->host, $this->db, $this->username, $this->password, $this->persistent);

Expand Down
51 changes: 26 additions & 25 deletions Container/file.php
Expand Up @@ -66,18 +66,18 @@ class Cache_Container_file extends Cache_Container
* @var string
*/
var $filename_prefix = '';


/**
* List of cache entries, used within a gc run
*
*
* @var array
*/
var $entries;

/**
* Total number of bytes required by all cache entries, used within a gc run.
*
*
* @var int
*/
var $total_size = 0;
Expand All @@ -86,7 +86,7 @@ class Cache_Container_file extends Cache_Container
/**
* Max Line Length of userdata
*
* If set to 0, it will take the default
* If set to 0, it will take the default
* ( 1024 in php 4.2, unlimited in php 4.3)
* see http://ch.php.net/manual/en/function.fgets.php
* for details
Expand All @@ -100,13 +100,14 @@ class Cache_Container_file extends Cache_Container
*
* @param array Config options: ["cache_dir" => ..., "filename_prefix" => ...]
*/
function Cache_Container_file($options = '')
{
if (is_array($options)) {
$this->setOptions($options, array_merge($this->allowed_options, array('cache_dir', 'filename_prefix', 'max_userdata_linelength')));
}
function Cache_Container_file($options = null)
{
$this->setAllowedOptions(
array('cache_dir', 'filename_prefix', 'max_userdata_linelength')
);
$this->setOptions($options);
clearstatcache();
if ($this->cache_dir) {
if ($this->cache_dir) {
// make relative paths absolute for use in deconstructor.
// it looks like the deconstructor has problems with relative paths
if (OS_UNIX && '/' != $this->cache_dir{0} )
Expand All @@ -121,7 +122,7 @@ function Cache_Container_file($options = '')
}
$this->entries = array();
$this->group_dirs = array();

} // end func contructor

function fetch($id, $group)
Expand Down Expand Up @@ -154,7 +155,7 @@ function fetch($id, $group)
}
$buffer = '';
while (!feof($fh)) {
$buffer .= fread($fh, 8192);
$buffer .= fread($fh, 8192);
}
$cachedata = $this->decode($buffer);

Expand Down Expand Up @@ -207,7 +208,7 @@ function save($id, $cachedata, $expires, $group, $userdata)
fclose($fh);

// I'm not sure if we need this
// i don't think we need this (chregu)
// i don't think we need this (chregu)
// touch($file);

return true;
Expand Down Expand Up @@ -269,28 +270,28 @@ function garbageCollection($maxlifetime)

$ok = $this->doGarbageCollection($maxlifetime, $this->cache_dir);

// check the space used by the cache entries
// check the space used by the cache entries
if ($this->total_size > $this->highwater) {

krsort($this->entries);
reset($this->entries);

while ($this->total_size > $this->lowwater && list($lastmod, $entry) = each($this->entries)) {
if (@unlink($entry['file'])) {
$this->total_size -= $entry['size'];
} else {
new CacheError("Can't delete {$entry['file']}. Check the permissions.");
}
}

}

$this->entries = array();
$this->total_size = 0;

return $ok;
} // end func garbageCollection

/**
* Does the recursive gc procedure, protected.
*
Expand Down Expand Up @@ -324,10 +325,10 @@ function doGarbageCollection($maxlifetime, $dir)
$expire = fgets($fh, 11);
fclose($fh);
$lastused = filemtime($file);

$this->entries[$lastused] = array('file' => $file, 'size' => filesize($file));
$this->total_size += filesize($file);

// remove if expired
if (( ($expire && $expire <= time()) || ($lastused <= (time() - $maxlifetime)) ) && !unlink($file)) {
new Cache_Error("Can't unlink cache file '$file', skipping. Check permissions and path.", __FILE__, __LINE__);
Expand Down Expand Up @@ -409,6 +410,6 @@ function deleteDir($dir)

return $num_removed;
} // end func deleteDir

} // end class file
?>
10 changes: 6 additions & 4 deletions Container/mdb.php
Expand Up @@ -88,15 +88,17 @@ class Cache_Container_mdb extends Cache_Container
*/
function Cache_Container_mdb($options)
{
$this->setAllowedOptions(array('dsn', 'cache_table'));
$this->setOptions($options);
$this->db = &MDB::Connect($options);
if (MDB::isError($this->db)) {
return new Cache_Error('MDB::connect failed: '
. $this->db->getMessage(), __FILE__, __LINE__);
return new Cache_Error(
'MDB::connect failed: '
. $this->db->getMessage(), __FILE__, __LINE__
);
} else {
$this->db->setFetchMode(MDB_FETCHMODE_ASSOC);
}
$this->setOptions($options, array_merge($this->allowed_options,
array('dsn', 'cache_table')));
}

/**
Expand Down

0 comments on commit 24f53b4

Please sign in to comment.