function testCachemanagerLocalfile()
 {
     $ctl =& Ethna_Controller::getInstance();
     $plugin =& $ctl->getPlugin();
     $cm = $plugin->getPlugin('Cachemanager', 'Localfile');
     // 文字列のキャッシュ
     $string_key = 'string_key';
     $string_value = "cache\ncontent";
     $cm->set($string_key, $string_value, mktime(0, 0, 0, 7, 1, 2000));
     $cache_string = $cm->get($string_key);
     $this->assertTrue($cm->isCached($string_key));
     $this->assertEqual(mktime(0, 0, 0, 7, 1, 2000), $cm->getLastModified($string_key));
     $this->assertTrue($string_value, $cache_string);
     // 整数のキャッシュ + namespace
     $int_key = 'int_key';
     $int_value = 777;
     $namespace = 'test';
     $cm->set($int_key, $int_value, mktime(0, 0, 0, 7, 1, 2000), $namespace);
     $cache_int = $cm->get($int_key, mktime(0, 0, 0, 7, 1, 2000), $namespace);
     $this->assertTrue($cm->isCached($int_key, mktime(0, 0, 0, 7, 1, 2000), $namespace));
     $this->assertTrue($int_value, $cache_int);
     // オブジェクトのキャッシュ
     $object_key = 'object_key';
     $object_value =& $cm;
     $cm->set($object_key, $object_value);
     $this->assertTrue($cm->isCached($object_key));
     // キャッシュされたインスタンス
     $cache_object = $cm->get($object_key);
     $this->assertTrue($string_value, $cache_object->get($string_key));
     // キャッシュのクリアをテスト
     $cm->clear($object_key);
     $this->assertFalse($cm->isCached($object_key));
     // キャッシュされていないのに呼び出そうとした場合
     $nocache_key = 'nocache_key';
     $cm->clear($nocache_key);
     $pear_error = $cm->get($nocache_key);
     $this->assertEqual(E_CACHE_NO_VALUE, $pear_error->getCode());
     $this->assertEqual('fopen failed', $pear_error->getMessage());
     // ファイルに読み込み権限がない場合
     Ethna_Util::chmod($cm->_getCacheFile(null, $string_key), 0222);
     $pear_error = $cm->get($string_key);
     $this->assertEqual(E_CACHE_NO_VALUE, $pear_error->getCode());
     $this->assertEqual('fopen failed', $pear_error->getMessage());
     Ethna_Util::chmod($cm->_getCacheFile(null, $string_key), 0666);
     // lifetime切れの場合
     $pear_error = $cm->get($string_key, 1);
     $this->assertEqual(E_CACHE_EXPIRED, $pear_error->getCode());
     $this->assertEqual('fopen failed', $pear_error->getMessage());
     // ディレクトリ名と同じファイルがあってディレクトリが作成できない場合
     $tmp_key = 'tmpkey';
     $tmp_dirname = $cm->_getCacheDir(null, $tmp_key);
     Ethna_Util::mkdir(dirname($tmp_dirname), 0777);
     $tmp_file = fopen($tmp_dirname, 'w');
     fclose($tmp_file);
     $pear_error = $cm->set($tmp_key, $string_value);
     $this->assertEqual(E_USER_WARNING, $pear_error->getCode());
     $this->assertEqual("mkdir({$tmp_dirname}) failed", $pear_error->getMessage());
     $this->rm($cm->backend->getTmpdir());
 }
Example #2
0
 /**
  *  mkdir -p
  *
  *  @access public
  *  @param  string  $dir    作成するディレクトリ
  *  @param  int     $mode   パーミッション
  *  @return bool    true:成功 false:失敗
  *  @static
  */
 public static function mkdir($dir, $mode)
 {
     if (file_exists($dir)) {
         return is_dir($dir);
     }
     $parent = dirname($dir);
     if ($dir === $parent) {
         return true;
     }
     if (is_dir($parent) === false) {
         if (Ethna_Util::mkdir($parent, $mode) === false) {
             return false;
         }
     }
     return mkdir($dir, $mode) && Ethna_Util::chmod($dir, $mode);
 }
Example #3
0
 /**
  *  キャッシュに値を設定する
  *
  *  @access public
  *  @param  string  $key        キャッシュキー
  *  @param  mixed   $value      キャッシュ値
  *  @param  int     $timestamp  キャッシュ最終更新時刻(unixtime)
  *  @param  string  $namespace  キャッシュネームスペース
  */
 public function set($key, $value, $timestamp = null, $namespace = null)
 {
     $namespace = $this->getNamespace($namespace);
     $dir = $this->_getCacheDir($namespace, $key);
     // キャッシュディレクトリチェック
     $r = Ethna_Util::mkdir($dir, 0777);
     if ($r == false && is_dir($dir) == false) {
         return Ethna::raiseError('mkdir(%s) failed', E_USER_WARNING, $dir);
     }
     $cache_file = $this->_getCacheFile($namespace, $key);
     $fp = fopen($cache_file, "a+");
     if ($fp == false) {
         return Ethna::raiseError('fopen failed', E_CACHE_GENERAL);
     }
     // ロック
     $timeout = 3;
     while ($timeout > 0) {
         $r = flock($fp, LOCK_EX | LOCK_NB);
         if ($r) {
             break;
         }
         $timeout--;
         sleep(1);
     }
     if ($timeout <= 0) {
         fclose($fp);
         return Ethna::raiseError('fopen failed', E_CACHE_GENERAL);
     }
     rewind($fp);
     ftruncate($fp, 0);
     fwrite($fp, serialize($value));
     fclose($fp);
     Ethna_Util::chmod($cache_file, 0666);
     if (is_null($timestamp)) {
         // this could suppress warning
         touch($cache_file);
     } else {
         touch($cache_file, $timestamp);
     }
     return 0;
 }
Example #4
0
 function testCachemanagerLocalfileObject()
 {
     // FIXME: mark as skip (if the tester support)
     if (version_compare(PHP_VERSION, '5.3.2', '<')) {
         return;
     }
     $string_key = 'string_key';
     $string_value = "cache\ncontent";
     // オブジェクトのキャッシュ
     $object_key = 'object_key';
     $object_value = $this->cm;
     $this->cm->set($object_key, $object_value);
     $this->assertTrue($this->cm->isCached($object_key));
     // キャッシュされたインスタンス
     $cache_object = $this->cm->get($object_key);
     $this->assertTrue((bool) $string_value);
     $this->assertTrue((bool) $cache_object->get($string_key));
     // キャッシュのクリアをテスト
     $this->cm->clear($object_key);
     $this->assertFalse($this->cm->isCached($object_key));
     // キャッシュされていないのに呼び出そうとした場合
     $nocache_key = 'nocache_key';
     $this->cm->clear($nocache_key);
     $pear_error = $this->cm->get($nocache_key);
     $this->assertEquals(E_CACHE_NO_VALUE, $pear_error->getCode());
     // ファイルに読み込み権限がない場合
     // PHP 4, PHP5 ともに、Windows上ではmodeをどのように設定しても
     // read権限が残るためskip.(PHP 4.4.8, 5.2.6 on Windows XP)
     if (!ETHNA_OS_WINDOWS) {
         $ref = new ReflectionMethod($this->cm, '_getCacheFile');
         $ref->setAccessible(true);
         Ethna_Util::chmod($ref->invoke($this->cm, $this->cm->getNamespace(), $string_key), 0222);
         $pear_error = $this->cm->get($string_key);
         $this->assertEquals(E_CACHE_NO_VALUE, $pear_error->getCode());
         Ethna_Util::chmod($ref->invoke($this->cm, $this->cm->getNamespace(), $string_key), 0666);
     }
     // lifetime切れの場合
     $pear_error = $this->cm->get($string_key, 1);
     $this->assertEquals(E_CACHE_EXPIRED, $pear_error->getCode());
     // ディレクトリ名と同じファイルがあってディレクトリが作成できない場合
     $ref = new ReflectionMethod($this->cm, '_getCacheDir');
     $ref->setAccessible(true);
     $tmp_key = 'tmpkey';
     $tmp_dirname = $ref->invoke($this->cm, $this->cm->getNamespace(), $tmp_key);
     Ethna_Util::mkdir(dirname($tmp_dirname), 0777);
     $tmp_file = fopen($tmp_dirname, 'w');
     fclose($tmp_file);
     $pear_error = $this->cm->set($tmp_key, $string_value);
     $this->assertEquals(E_USER_WARNING, $pear_error->getCode());
     $this->assertEquals("mkdir({$tmp_dirname}) failed", $pear_error->getMessage());
     $this->rm($this->getNonpublicProperty($this->cm, 'backend')->getTmpdir());
 }
 function testCachemanagerLocalfileObject()
 {
     $string_key = 'string_key';
     $string_value = "cache\ncontent";
     // オブジェクトのキャッシュ
     $object_key = 'object_key';
     $object_value = $this->cm;
     $this->cm->set($object_key, $object_value);
     $this->assertTrue($this->cm->isCached($object_key));
     // キャッシュされたインスタンス
     $cache_object = $this->cm->get($object_key);
     $this->assertTrue($string_value, $cache_object->get($string_key));
     // キャッシュのクリアをテスト
     $this->cm->clear($object_key);
     $this->assertFalse($this->cm->isCached($object_key));
     // キャッシュされていないのに呼び出そうとした場合
     $nocache_key = 'nocache_key';
     $this->cm->clear($nocache_key);
     $pear_error = $this->cm->get($nocache_key);
     $this->assertEqual(E_CACHE_NO_VALUE, $pear_error->getCode());
     $this->assertEqual('fopen failed', $pear_error->getMessage());
     // ファイルに読み込み権限がない場合
     // PHP 4, PHP5 ともに、Windows上ではmodeをどのように設定しても
     // read権限が残るためskip.(PHP 4.4.8, 5.2.6 on Windows XP)
     if (!ETHNA_OS_WINDOWS) {
         Ethna_Util::chmod($this->cm->_getCacheFile($this->cm->getNamespace(), $string_key), 0222);
         $pear_error = $this->cm->get($string_key);
         $this->assertEqual(E_CACHE_NO_VALUE, $pear_error->getCode());
         $this->assertEqual('fopen failed', $pear_error->getMessage());
         Ethna_Util::chmod($this->cm->_getCacheFile($this->cm->getNamespace(), $string_key), 0666);
     }
     // lifetime切れの場合
     $pear_error = $this->cm->get($string_key, 1);
     $this->assertEqual(E_CACHE_EXPIRED, $pear_error->getCode());
     $this->assertEqual('fopen failed', $pear_error->getMessage());
     // ディレクトリ名と同じファイルがあってディレクトリが作成できない場合
     $tmp_key = 'tmpkey';
     $tmp_dirname = $this->cm->_getCacheDir($this->cm->getNamespace(), $tmp_key);
     Ethna_Util::mkdir(dirname($tmp_dirname), 0777);
     $tmp_file = fopen($tmp_dirname, 'w');
     fclose($tmp_file);
     $pear_error = $this->cm->set($tmp_key, $string_value);
     $this->assertEqual(E_USER_WARNING, $pear_error->getCode());
     $this->assertEqual("mkdir({$tmp_dirname}) failed", $pear_error->getMessage());
     $this->rm($this->cm->backend->getTmpdir());
 }