/**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 写入缓存
  * @access public
  * @param string $name 缓存变量名
  * @param mixed $value  存储数据
  * @param integer $expire  有效时间(秒)
  * @return boolen
  */
 public function set($name, $value, $expire = null)
 {
     \think\Cache::$writeTimes++;
     if (is_null($expire)) {
         $expire = $this->options['expire'];
     }
     $name = $this->options['prefix'] . $name;
     eaccelerator_lock($name);
     if (eaccelerator_put($name, $value, $expire)) {
         if ($this->options['length'] > 0) {
             // 记录缓存队列
             $queue = eaccelerator_get('__info__');
             if (!$queue) {
                 $queue = [];
             }
             if (false === array_search($name, $queue)) {
                 array_push($queue, $name);
             }
             if (count($queue) > $this->options['length']) {
                 // 出列
                 $key = array_shift($queue);
                 // 删除缓存
                 eaccelerator_rm($key);
             }
             eaccelerator_put('__info__', $queue);
         }
         return true;
     }
     return false;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
 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;
 }
Exemplo n.º 6
0
 public function set($key, $value, $type = '', $ttl = SESSION_EXPIRE)
 {
     $this->type = $type;
     $name = $this->_key($key);
     eaccelerator_lock($name);
     if (eaccelerator_put($name, $value, $ttl)) {
         return true;
     }
     return false;
 }
Exemplo n.º 7
0
 public function set($key, $value, $type = "", $ttl = SESSION_EXPIRE)
 {
     $this->type = $type;
     $name = $this->_key($key);
     eaccelerator_lock($name);
     if (eaccelerator_put($name, $value, $ttl)) {
         return TRUE;
     }
     return FALSE;
 }
Exemplo n.º 8
0
 /**
 +----------------------------------------------------------
 * 写入缓存
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 * @param string $name 缓存变量名
 * @param mixed $value  存储数据
 +----------------------------------------------------------
 * @return boolen
 +----------------------------------------------------------
 */
 public function set($name, $value, $ttl = null)
 {
     N('cache_write', 1);
     if (isset($ttl) && is_int($ttl)) {
         $expire = $ttl;
     } else {
         $expire = $this->expire;
     }
     eaccelerator_lock($name);
     return eaccelerator_put($name, $value, $expire);
 }
Exemplo n.º 9
0
 /**
  * Enter description here ...
  */
 public function lock()
 {
     if (!$this->eAccelerator) {
         $this->fp = fopen($this->path, 'w+');
         if ($this->fp === false) {
             return false;
         }
         return flock($this->fp, LOCK_EX);
     } else {
         return eaccelerator_lock($this->name);
     }
 }
Exemplo n.º 10
0
 /**
  * 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;
 }
Exemplo n.º 11
0
 /**
  * 加锁
  * Enter description here ...
  */
 public function lock()
 {
     //如果无法开启ea内存锁,则开启文件锁
     if (!$this->eAccelerator) {
         //配置目录权限可写
         $this->fp = fopen($this->path, 'w+');
         if ($this->fp === false) {
             return false;
         }
         return flock($this->fp, LOCK_EX);
     } else {
         return eaccelerator_lock($this->name);
     }
 }
Exemplo n.º 12
0
 /**
 +----------------------------------------------------------
 * 写入缓存
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 * @param string $name 缓存变量名
 * @param mixed $value  存储数据
 * @param integer $expire  有效时间(秒)
 +----------------------------------------------------------
 * @return boolen
 +----------------------------------------------------------
 */
 public function set($name, $value, $expire = null)
 {
     N('cache_write', 1);
     if (is_null($expire)) {
         $expire = $this->options['expire'];
     }
     eaccelerator_lock($name);
     if (eaccelerator_put($name, $value, $expire)) {
         if ($this->options['length'] > 0) {
             // 记录缓存队列
             $this->queue($name);
         }
         return true;
     }
     return false;
 }
Exemplo n.º 13
0
 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);
     }
 }
Exemplo n.º 14
0
  /**
   * 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;
  }
Exemplo n.º 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;
 }
Exemplo n.º 16
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;
     }
 }
Exemplo n.º 17
0
 /**
  * 开始加锁
  *
  * @return bool 加锁成功返回true,失败返回false
  */
 public function lock()
 {
     if (!$this->eAccelerator) {
         if ($this->fp === false) {
             return false;
         }
         $result = false;
         $result = flock($this->fp, LOCK_EX);
         //防止死鎖
         /*
         if (file_exists($this->path) && @time()-fileatime($this->path) > $this->expire) {
             $this->unlock();
             $this->endLock ();   
             $this->startLock();
             $result = flock ( $this->fp, LOCK_EX );
         }
         */
         return $result;
     } else {
         return eaccelerator_lock($this->name);
     }
 }
 function set($key, $val, $ttl = 600)
 {
     eaccelerator_lock($this->pre . $key);
     return eaccelerator_put($this->pre . $key, $val, $ttl);
 }
Exemplo n.º 19
0
 function lock($key, $waitTimeout = 0)
 {
     return eaccelerator_lock($key);
 }
Exemplo n.º 20
0
 public function set($key, $value, $expire)
 {
     eaccelerator_lock($key);
     return eaccelerator_put($key, $value, $expire);
 }
Exemplo n.º 21
0
 /**
  * lock()
  *   lock the cache from other writes.
  *
  * @param none
  * @return string
  *   Returns TRUE on success, FALSE on failure
  */
 function lock()
 {
     return eaccelerator_lock($this->lock);
 }
Exemplo n.º 22
0
 function lock($key, $waitTimeout = 0)
 {
     eaccelerator_lock($key);
     return true;
 }
Exemplo n.º 23
0
 /**
  * set value of variable in shared mem
  *
  * @param string $name  name of the variable
  * @param string $value value of the variable
  * @param int $ttl (optional) time to life of the variable
  *
  * @return bool true on success
  * @access public
  */
 function set($name, $value, $ttl = 0)
 {
     eaccelerator_lock($name);
     return eaccelerator_put($name, $value, $ttl);
 }
Exemplo n.º 24
0
 public function lock($id, $group, $locktime)
 {
     $returning = new stdClass();
     $returning->locklooped = false;
     $looptime = $locktime * 10;
     $cache_id = $this->_getCacheId($id, $group);
     $data_lock = eaccelerator_lock($cache_id);
     if ($data_lock === false) {
         $lock_counter = 0;
         while ($data_lock === false) {
             if ($lock_counter > $looptime) {
                 $returning->locked = false;
                 $returning->locklooped = true;
                 break;
             }
             usleep(100);
             $data_lock = eaccelerator_lock($cache_id);
             $lock_counter++;
         }
     }
     $returning->locked = $data_lock;
     return $returning;
 }
 public function get($key)
 {
     return eaccelerator_lock($key);
 }
Exemplo n.º 26
0
 /**
  * Lock cached item
  *
  * @param	string	$id		The cache data id
  * @param	string	$group	The cache data group
  * @param	integer	$locktime Cached item max lock time
  * @return	boolean	True on success, false otherwise.
  * @since	1.6
  */
 public function lock($id, $group, $locktime)
 {
     $returning = new stdClass();
     $returning->locklooped = false;
     $looptime = $locktime * 10;
     $cache_id = $this->_getCacheId($id, $group);
     $data_lock = eaccelerator_lock($cache_id);
     if ($data_lock === false) {
         $lock_counter = 0;
         // loop until you find that the lock has been released.  that implies that data get from other thread has finished
         while ($data_lock === false) {
             if ($lock_counter > $looptime) {
                 $returning->locked = false;
                 $returning->locklooped = true;
                 break;
             }
             usleep(100);
             $data_lock = eaccelerator_lock($cache_id);
             $lock_counter++;
         }
     }
     $returning->locked = $data_lock;
     return $returning;
 }