/**
  * Delete cache from shared memory
  *
  * @param  string $sKey - file name
  * @return result of the operation
  */
 function delData($sKey)
 {
     eaccelerator_lock($sKey);
     eaccelerator_rm($sKey);
     eaccelerator_unlock($sKey);
     return true;
 }
Beispiel #2
0
 /**
  * Write an item to the cache.
  *
  * @param string The name of the cache
  * @param mixed The data to write to the cache item
  * @return boolean True on success, false on failure
  */
 function put($name, $contents)
 {
     eaccelerator_lock($this->unique_id . "_" . $name);
     $status = eaccelerator_put($this->unique_id . "_" . $name, serialize($contents));
     eaccelerator_unlock($this->unique_id . "_" . $name);
     return $status;
 }
 /**
  * Put data into remote cache store
  *
  * @param	string		Cache unique key
  * @param	string		Cache value to add
  * @param	integer		[Optional] Time to live
  * @return	@e boolean
  */
 public function putInCache($key, $value, $ttl = 0)
 {
     eaccelerator_lock(md5($this->identifier . $key));
     $check = eaccelerator_put(md5($this->identifier . $key), $value, intval($ttl));
     eaccelerator_unlock(md5($this->identifier . $key));
     return $check;
 }
 function put($name, $data, $ttl = 604800)
 {
     $ttl = (int) $ttl ? (int) $ttl : 604800;
     if (eaccelerator_lock($this->key . $name)) {
         eaccelerator_put($this->key . $name, serialize($data), $ttl);
         eaccelerator_unlock($this->key . $name);
     }
     $this->vars[$name] = $data;
 }
Beispiel #5
0
 /**
  * Enter description here ...
  */
 public function unlock()
 {
     if (!$this->eAccelerator) {
         if ($this->fp !== false) {
             flock($this->fp, LOCK_UN);
             clearstatcache();
         }
         fclose($this->fp);
     } else {
         return eaccelerator_unlock($this->name);
     }
 }
 /**
  * write template to cache
  *
  * @access   public
  * @param	string		cache key
  * @param	array		templates to store
  * @return   boolean		true on success
  */
 function write($key, $templates)
 {
     if (!function_exists('eaccelerator_lock')) {
         return false;
     }
     eaccelerator_lock($key);
     if ($this->getParam('lifetime') == 'auto') {
         eaccelerator_put($key, serialize($templates));
     } else {
         eaccelerator_put($key, serialize($templates), $this->getParam('lifetime') * 60);
     }
     eaccelerator_unlock($key);
     return true;
 }
 public function set($key, $value = "", $ttl = 300, $mode = 0)
 {
     if (empty($value)) {
         return eaccelerator_rm($key);
     } else {
         eaccelerator_lock($key);
         switch ($mode) {
             case 1:
                 return eaccelerator_cache_output($key, $value, $ttl);
                 break;
             case 2:
                 return eaccelerator_cache_result($key, $value, $ttl);
                 break;
             default:
                 return eaccelerator_put($key, $value, $ttl);
         }
         eaccelerator_unlock($key);
     }
 }
 public function free($key)
 {
     return eaccelerator_unlock($key);
 }
Beispiel #9
0
 function unlock($key)
 {
     return eaccelerator_unlock($key);
 }
 function unlock($key)
 {
     eaccelerator_unlock($key);
     return true;
 }
  /**
   * Smarty Cache Handler<br>
   * utilizing eAccelerator extension (http://eaccelerator.net/HomeUk)<br>
   *
   * Name:     smarty_cache_eaccelerator<br>
   * Type:     Cache Handler<br>
   * Purpose:  Replacement for the file based cache handling of Smarty. smarty_cache_eaccelerator() is
   *           using Turck eaccelerator extension to minimize disk usage.
   * File:     cache.eaccelerator.php<br>
   * Date:     Dec 2, 2003<br>
   *
   * Usage Example<br>
   * <pre>
   * $smarty = new Smarty;
   * $smarty->cache_handler_func = 'smarty_cache_eaccelerator';
   * $smarty->caching = true;
   * $smarty->display('index.tpl');
   * </pre>
   *
   * @author   André Rabold
   * @version  RC-1
   *
   * @param    string   $action         Cache operation to perform ( read | write | clear )
   * @param    mixed    $smarty         Reference to an instance of Smarty
   * @param    string   $cache_content  Reference to cached contents
   * @param    string   $tpl_file       Template file name
   * @param    string   $cache_id       Cache identifier
   * @param    string   $compile_id     Compile identifier
   * @param    integer  $exp_time       Expiration time
   * @return   boolean                  TRUE on success, FALSE otherwise
   *
   * @link     http://eaccelerator.net/HomeUk
   *           (eaccelerator homepage)
   * @link     http://smarty.php.net/manual/en/section.template.cache.handler.func.php
   *           (Smarty online manual)
   */
  function smarty_cache_eaccelerator($action, &$smarty, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null)
  {
    if(!function_exists("eaccelerator")) {
      $smarty->trigger_error("cache_handler: PHP Extension \"eaccelerator\" (http://eaccelerator.net/HomeUk) not installed.");
      return false;
    }

    // Create unique cache id:
    // We are using smarty's internal functions here to be as compatible as possible.
    $_auto_id    = $smarty->_get_auto_id($cache_id, $compile_id);
    $_cache_file = substr($smarty->_get_auto_filename(".", $tpl_file, $_auto_id),2);
    $eaccelerator_id  = "smarty_eaccelerator|".$_cache_file;

    // The index contains all stored cache ids in a hierarchy and can be iterated later
    $eaccelerator_index_id = "smarty_eaccelerator_index";

    switch ($action) {

      case 'read':
        // read cache from shared memory
        $cache_content = eaccelerator_get($eaccelerator_id);
        if (!is_null($cache_content) && _eaccelerator_hasexpired($cache_content)) {
          // Cache has been expired so we clear it now by calling ourself with another parameter :)
          $cache_content = null;
          smarty_cache_eaccelerator('clear', $smarty, $cache_content, $tpl_file, $cache_id, $compile_id);
        }

        $return = true;
        break;

      case 'write':
        // save cache to shared memory
        $current_time = time();
        if (is_null($exp_time) || $exp_time < $current_time)
          $ttl = 0;
        else
          $ttl = $exp_time - time();

        // First run garbage collection
        eaccelerator_gc();

        // Put content into cache
        eaccelerator_lock($eaccelerator_id);
        eaccelerator_put($eaccelerator_id, $cache_content, $ttl);

        // Create an index association
        eaccelerator_lock($eaccelerator_index_id);
        $eaccelerator_index = eaccelerator_get($eaccelerator_index_id);
        if (!is_array($eaccelerator_index))
          $eaccelerator_index = array();
        $indexes = explode(DIRECTORY_SEPARATOR, $_cache_file);
        $_pointer =& $eaccelerator_index;
        foreach ($indexes as $index) {
          if (!isset($_pointer[$index]))
            $_pointer[$index] = array();
          $_pointer =& $_pointer[$index];
        }
        $_pointer = $eaccelerator_id;
        eaccelerator_put($eaccelerator_index_id, $eaccelerator_index, 0);
        eaccelerator_unlock($eaccelerator_index_id);

        eaccelerator_unlock($eaccelerator_id);
        break;

      case 'clear':
        // clear cache info
        eaccelerator_lock($eaccelerator_index_id);
        $eaccelerator_index = eaccelerator_get($eaccelerator_index_id);
        if (is_array($eaccelerator_index)) {
          if (empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
            // clear all cache
            eaccelerator_lock($eaccelerator_id);
            _eaccelerator_clear_cache($eaccelerator_index);
            eaccelerator_unlock($eaccelerator_id);
            $eaccelerator_index = array();
          }
          else {
            // clear single file or cache group
            $indexes = explode(DIRECTORY_SEPARATOR, $_cache_file);
            if (is_null($tpl_file))
              array_pop($indexes);

            $_pointer =& $eaccelerator_index;
            $_failed = false;
            foreach ($indexes as $index) {
              if (!isset($_pointer[$index])) {
                $_failed = true;
                break;
              }
              $_pointer =& $_pointer[$index];
            }

            if (!$_failed) {
              if (is_array($_pointer)) {
                // Clear cache group
                _eaccelerator_clear_cache($_pointer);
              }
              else {
                // Clear single file
                eaccelerator_lock($_pointer);
                eaccelerator_rm($_pointer);
                eaccelerator_unlock($_pointer);
              }
              $_pointer = null;
            }
          }
        }
        eaccelerator_put($eaccelerator_index_id, $eaccelerator_index, 0);
        eaccelerator_unlock($eaccelerator_index_id);

        $return = true;
        break;

      default:
        // error, unknown action
        $smarty->trigger_error("cache_handler: unknown action \"$action\"");
        $return = false;
        break;
    }
    return $return;
  }
Beispiel #12
0
 /**
  * Unlock cached item
  *
  * @param	string	$id		The cache data id
  * @param	string	$group	The cache data group
  * @return	boolean	True on success, false otherwise.
  * @since	1.6
  */
 public function unlock($id, $group = null)
 {
     $cache_id = $this->_getCacheId($id, $group);
     return eaccelerator_unlock($cache_id);
 }
Beispiel #13
0
 /**
  * unlock()
  *   lock the cache from other writes.
  *
  * @param none
  * @return bool
  *   Returns TRUE on success, FALSE on failure
  */
 function unlock()
 {
     return eaccelerator_unlock($this->lock);
 }
Beispiel #14
0
 function cacheset($name, $value)
 {
     if (!$this->cache_type) {
         return;
     }
     if (!$this->thestorage) {
         $this->cacheget($name);
     }
     if ($this->thestorage[$name] == $value) {
         return;
     }
     $this->thestorage[$name] = $value;
     $value = serialize($this->thestorage);
     switch ($this->cache_type) {
         case 1:
             if ($this->connect()) {
                 $this->mchandle->set(VBSEO_CACHE_VAR, $value, 0, VBSEO_MEMCACHE_TTL);
             }
             break;
         case 2:
             apc_delete(VBSEO_CACHE_VAR);
             apc_store(VBSEO_CACHE_VAR, $value, VBSEO_MEMCACHE_TTL);
             break;
         case 3:
             xcache_set(VBSEO_CACHE_VAR, $value, VBSEO_MEMCACHE_TTL);
             break;
         case 4:
             if (eaccelerator_lock(VBSEO_CACHE_VAR)) {
                 eaccelerator_rm(VBSEO_CACHE_VAR);
                 eaccelerator_put(VBSEO_CACHE_VAR, $value);
                 eaccelerator_unlock(VBSEO_CACHE_VAR);
             }
             break;
     }
 }
Beispiel #15
0
 /**
  * subject.txtをダウンロードする
  *
  * @access  public
  * @return  array|null|false  subject.txtの配列データ(eaccelerator, apc用)、またはnullを返す。
  *                            失敗した場合はfalseを返す。
  */
 function downloadSubject()
 {
     global $_conf;
     static $spentDlTime_ = 0;
     // DL所要合計時間
     $perm = isset($_conf['dl_perm']) ? $_conf['dl_perm'] : 0606;
     $modified = false;
     if ($this->storage == 'file') {
         FileCtl::mkdirFor($this->subject_file);
         // 板ディレクトリが無ければ作る
         if (file_exists($this->subject_file)) {
             // ファイルキャッシュがあれば、DL制限時間をかける
             if (UA::isK()) {
                 $dlSubjectTotalLimitTime = $_conf['dlSubjectTotalLimitTimeM'];
             } else {
                 $dlSubjectTotalLimitTime = $_conf['dlSubjectTotalLimitTime'];
             }
             if ($dlSubjectTotalLimitTime and $spentDlTime_ > $dlSubjectTotalLimitTime) {
                 return null;
             }
             // 条件によって、キャッシュを適用する
             // subject.php でrefresh指定がある時は、キャッシュを適用しない
             if (!(basename($_SERVER['SCRIPT_NAME']) == $_conf['subject_php'] && !empty($_REQUEST['refresh']))) {
                 // キャッシュ適用指定時は、その場で抜ける
                 if (!empty($_GET['norefresh']) || isset($_REQUEST['word'])) {
                     return null;
                     // 並列ダウンロード済の場合も抜ける
                 } elseif (!empty($GLOBALS['expack.subject.multi-threaded-download.done'])) {
                     return null;
                     // 新規スレ立て時以外で、キャッシュが新鮮な場合も抜ける
                 } elseif (empty($_POST['newthread']) and $this->isSubjectTxtFresh()) {
                     return null;
                 }
             }
             $modified = gmdate("D, d M Y H:i:s", filemtime($this->subject_file)) . " GMT";
         }
     }
     $dlStartTime = $this->microtimeFloat();
     // DL
     require_once 'HTTP/Request.php';
     $params = array();
     $params['timeout'] = $_conf['fsockopen_time_limit'];
     if ($_conf['proxy_use']) {
         $params['proxy_host'] = $_conf['proxy_host'];
         $params['proxy_port'] = $_conf['proxy_port'];
     }
     $req = new HTTP_Request($this->subject_url, $params);
     $modified && $req->addHeader('If-Modified-Since', $modified);
     $req->addHeader('User-Agent', sprintf('Monazilla/1.00 (%s/%s)', $_conf['p2uaname'], $_conf['p2version']));
     $response = $req->sendRequest();
     $error_msg = null;
     if (PEAR::isError($response)) {
         $error_msg = $response->getMessage();
     } else {
         $code = $req->getResponseCode();
         if ($code == 302) {
             // ホストの移転を追跡
             require_once P2_LIB_DIR . '/BbsMap.php';
             $new_host = BbsMap::getCurrentHost($this->host, $this->bbs);
             if ($new_host != $this->host) {
                 $aNewSubjectTxt = new SubjectTxt($new_host, $this->bbs);
                 return $aNewSubjectTxt->downloadSubject();
             }
         }
         if (!($code == 200 || $code == 206 || $code == 304)) {
             //var_dump($req->getResponseHeader());
             $error_msg = $code;
         }
     }
     if (!is_null($error_msg) && strlen($error_msg) > 0) {
         $attrs = array();
         if ($_conf['ext_win_target']) {
             $attrs['target'] = $_conf['ext_win_target'];
         }
         $atag = P2View::tagA(P2Util::throughIme($this->subject_url), hs($this->subject_url), $attrs);
         $msg_ht = sprintf('<div>Error: %s<br>p2 info - %s に接続できませんでした。</div>', hs($error_msg), $atag);
         P2Util::pushInfoHtml($msg_ht);
         $body = '';
     } else {
         $body = $req->getResponseBody();
     }
     $dlEndTime = $this->microtimeFloat();
     $dlTime = $dlEndTime - $dlStartTime;
     $spentDlTime_ += $dlTime;
     // DL成功して かつ 更新されていたら
     if ($body && $code != '304') {
         // したらば or be.2ch.net ならEUCをSJISに変換
         if (P2Util::isHostJbbsShitaraba($this->host) || P2Util::isHostBe2chNet($this->host)) {
             $body = mb_convert_encoding($body, 'SJIS-win', 'eucJP-win');
         }
         // eaccelerator or apcに保存する場合
         if ($this->storage == 'eaccelerator' || $this->storage == 'apc') {
             $cache_key = "{$this->host}/{$this->bbs}";
             $cont = rtrim($body);
             $lines = explode("\n", $cont);
             if ($this->storage == 'eaccelerator') {
                 eaccelerator_lock($cache_key);
                 eaccelerator_put($cache_key, $lines, $_conf['sb_dl_interval']);
                 eaccelerator_unlock($cache_key);
             } else {
                 apc_store($cache_key, $lines, $_conf['sb_dl_interval']);
             }
             return $lines;
             // ファイルに保存する場合
         } else {
             if (false === FileCtl::filePutRename($this->subject_file, $body)) {
                 // 保存に失敗はしても、既存のキャッシュが読み込めるならよしとしておく
                 if (is_readable($this->subject_file)) {
                     return null;
                 } else {
                     die("Error: cannot write file");
                     return false;
                 }
             }
             chmod($this->subject_file, $perm);
         }
     } else {
         // touchすることで更新インターバルが効くので、しばらく再チェックされなくなる
         // (変更がないのに修正時間を更新するのは、少し気が進まないが、ここでは特に問題ないだろう)
         if ($this->storage == 'file') {
             touch($this->subject_file);
         }
     }
     return null;
 }