欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          分享TP6框架中Redis操作服務(wù)類的記錄

          下面由thinkphp框架教程欄目給大家分享ThinkPHP6中Redis操作服務(wù)類的記錄,希望對(duì)需要的朋友有所幫助!

          1. 定義服務(wù)類

          <?php  declare (strict_types=1);  namespace appapiservicecommon;  use thinkfacadeCache;  /**  * 緩存服務(wù)  * Class RedisService  *  * @package appapiservicecommon  */ class RedisService {     private $expire;     private $expire_at;      /**      * 獲取redis句柄      *      * @return object|null      */     public function client(): ?object     {         return Cache::store('redis')->handler();     }      /**      * 處理緩存key(添加前綴...)      *      * @param string $key  key      *      * @return string      */     private function cacheKey(string $key): string     {         return Cache::getCacheKey($key);     }      /**      * 緩存程序運(yùn)行結(jié)果      *      * @param          $key      * @param callable $callback      * @param int      $expire      *      * @return mixed      */     public function cache($key, callable $callback, int $expire = 3600)     {         $cache = $this->client()->get($key);         if (! $cache || ! unserialize($cache)) {             $data = $callback();             $this->client()->set($key, $cache = serialize($data), $expire);         }          return unserialize($cache);     }      /**      * 程序運(yùn)行鎖      * @param          $key      * @param callable $callback      * @param int      $timeout      *      * @return array      */     public function lock($key, callable $callback, int $timeout = 10): array     {         $lock = $this->client()->get($key);         if ($lock) return ['code' => 0, 'data' => null];         $this->client()->setex($key, $timeout, 1);         $data = $callback();         $this->client()->del($key);          return ['code' => 1, 'data' => $data];     }      /**      * 設(shè)置有效時(shí)間      *      * @param $ttl      *      * @return $this|false      * @throws Exception      */     public function setExpire($ttl)     {         if ($this->expire_at) throw new Exception('setExpire and setExpireAt can not set both');         $this->expire = $ttl;          return $this;     }      /**      * 設(shè)置到期時(shí)間      *      * @param $timestamp      *      * @return $this|false      * @throws Exception      */     public function setExpireAt($timestamp)     {         if ($this->expire > 0) throw new Exception('setExpire and setExpireAt can not set both');         $this->expire_at = $timestamp;          return $this;     }      /**      * 調(diào)用原生redis方法      *      * @return mixed      */     public function __call($name, $arguments)     {         $cache_key = $this->cacheKey($arguments[0]);          $result = $this->client()->{$name}(...$arguments);          // 設(shè)置過(guò)期時(shí)間         $this->expire && $this->client()->expire($cache_key, $this->expire);         $this->expire_at && $this->client()->expireAt($cache_key, $this->expire_at);          return $result;     } }

          2. 定義門(mén)面Facade

          <?php   namespace appapifacade;   use appapiservicecommonRedisService; use thinkFacade;  /**  * Class Redis  *  * @package appapifacade  *  * @method static Redis client()  * @method static Redis setExpire($ttl)  * @method static Redis setExpireAt($timestamp)  * @method static mixed cache($key, callable $callback, int $expire = 3600)  * @method static array lock($key, callable $callback, int $timeout = 10)  */ class Redis extends Facade {     protected static function getFacadeClass()     {         return RedisService::class;     } }

          3. 如何使用

          3.1 程序鎖

          public function test()     {         $a = 1;         $b = 2;         $result = Redis::lock('lock:demo', function () use ($a, $b) {             return $a + $b;         }, 5);         if ($result['code'] == 0) return '操作頻繁,請(qǐng)稍后再試';         return $result['data']; // 成功返回?cái)?shù)據(jù) 3     }

          3.2 方法數(shù)據(jù)緩存

          public function test() {     $a = 1;     $b = 2;     $result = Redis::cache('cache:demo', function () use ($a, $b) {         return $a + $b;     }, 5);      return $result; // 成功返回?cái)?shù)據(jù) 3,有效時(shí)長(zhǎng)5秒 }

          3.3 簡(jiǎn)化過(guò)期時(shí)間設(shè)置

          // 24小時(shí)到期 Redis::setExpire(86400)->hSet('expire:demo', 'hash-key', 'hash-value'); // 2021-08-24 23:59:59到期 Redis::setExpireAt(strtotime('2021-08-24 23:59:59'))->hSet('expireAt:demo', 'hash-key', 'hash-value');

          3.4 普通調(diào)用

          // 普通調(diào)用,直接跟redis方法名 Redis::set('set:demo', 132456); // idea支持代碼提示調(diào)用 Redis::client()->set('set:demo', 132456);

          推薦:《最新的10個(gè)thinkphp視頻教程》

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)