<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Redis使用 ### 使用Redis緩存 ~~~ Cache::store('redis')->set('name','value',3600); Cache::get('name'); ~~~ #### 其他用法 ~~~ Cache::store('redis')->set('name',time(),3600); Cache::remember('name',time(),0); Cache::rm('name'); Cache::clear('name'); Cache::inc('name',1); Cache::dec('name'); $name = Cache::get('name'); var_dump($name); ~~~ **Redis.php** > \thinkphp\library\think\cache\driver > ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- namespace think\cache\driver; use think\cache\Driver; /** * Redis緩存驅動,適合單機部署、有前端代理實現高可用的場景,性能最好 * 有需要在業務層實現讀寫分離、或者使用RedisCluster的需求,請使用Redisd驅動 * * 要求安裝phpredis擴展:https://github.com/nicolasff/phpredis * @author 塵緣 <130775@qq.com> */ class Redis extends Driver { protected $options = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', 'serialize' => true, ]; /** * 架構函數 * @access public * @param array $options 緩存參數 */ public function __construct($options = []) { if (!extension_loaded('redis')) { throw new \BadFunctionCallException('not support: redis'); } if (!empty($options)) { $this->options = array_merge($this->options, $options); } $this->handler = new \Redis; if ($this->options['persistent']) { $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']); } else { $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']); } if ('' != $this->options['password']) { $this->handler->auth($this->options['password']); } if (0 != $this->options['select']) { $this->handler->select($this->options['select']); } } /** * 判斷緩存 * @access public * @param string $name 緩存變量名 * @return bool */ public function has($name) { return $this->handler->exists($this->getCacheKey($name)); } /** * 讀取緩存 * @access public * @param string $name 緩存變量名 * @param mixed $default 默認值 * @return mixed */ public function get($name, $default = false) { $this->readTimes++; $value = $this->handler->get($this->getCacheKey($name)); if (is_null($value) || false === $value) { return $default; } return $this->unserialize($value); } /** * 寫入緩存 * @access public * @param string $name 緩存變量名 * @param mixed $value 存儲數據 * @param integer|\DateTime $expire 有效時間(秒) * @return boolean */ public function set($name, $value, $expire = null) { $this->writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } if ($this->tag && !$this->has($name)) { $first = true; } $key = $this->getCacheKey($name); $expire = $this->getExpireTime($expire); $value = $this->serialize($value); if ($expire) { $result = $this->handler->setex($key, $expire, $value); } else { $result = $this->handler->set($key, $value); } isset($first) && $this->setTagItem($key); return $result; } /** * 自增緩存(針對數值緩存) * @access public * @param string $name 緩存變量名 * @param int $step 步長 * @return false|int */ public function inc($name, $step = 1) { $this->writeTimes++; $key = $this->getCacheKey($name); return $this->handler->incrby($key, $step); } /** * 自減緩存(針對數值緩存) * @access public * @param string $name 緩存變量名 * @param int $step 步長 * @return false|int */ public function dec($name, $step = 1) { $this->writeTimes++; $key = $this->getCacheKey($name); return $this->handler->decrby($key, $step); } /** * 刪除緩存 * @access public * @param string $name 緩存變量名 * @return boolean */ public function rm($name) { $this->writeTimes++; return $this->handler->delete($this->getCacheKey($name)); } /** * 清除緩存 * @access public * @param string $tag 標簽名 * @return boolean */ public function clear($tag = null) { if ($tag) { // 指定標簽清除 $keys = $this->getTagItem($tag); foreach ($keys as $key) { $this->handler->delete($key); } $this->rm('tag_' . md5($tag)); return true; } $this->writeTimes++; return $this->handler->flushDB(); } /** * 如果不存在則寫入緩存 * @access public * @param string $name 緩存變量名 * @param mixed $value 存儲數據 * @param int $expire 有效時間 0為永久 * @return mixed */ public function remember($name, $value, $expire = null) { if (is_null($expire)) { $expire = $this->options['expire']; } // 沒有過期參數時,使用setnx if (!$expire) { $key = $this->getCacheKey($name); $val = $this->serialize($value); $res = $this->handler->setnx($key, $val); if ($res) { $this->writeTimes++; return $value; } else { return $this->get($name); } } if ($this->has($name)) { return $this->get($name); } else { $this->set($name, $value, $expire); } return $value; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看