YII2 は Redis キャッシュアシスタントをどのように使用しますか?

//キー値を取得
if (CacheHelper::get(CacheKey::rewardRedKey($params))) { 
    throw new \Exception('A task is being running at this time'); 
} 
CacheHelper::set(CacheKey:: rewardRedKey($ params), $params, 3600 * 24); 
//独自のロジックを実行します. . . . . 
//正常に削除タスクのキャッシュを実行
CacheHelper::del(CacheKey::rewardRedKey($params)); CacheHelper の基本クラスは次のとおりです: 
<?php 
/** 
 *YII2 で Redis キャッシュ アシスタントを使用するには? 
 */ 
namespace common\helpers; 
use Yii; 
use Yii\caching\CacheInterface; 
/** 
 * Redis キャッシュ ヘルパー
 * Class Cache 
 * @package common\helper 
 */ 
class CacheHelper 
{ 
    /**




 
     * @param string $key
     * @return mixed 
     */ 
     * @return 混合
    public static function has(string $key) 
    { 
        return self::init()->exists($key); 
    } 

    /** 
     * @param string $key 
     * @param null $default 
     * @return mixed 
     */ 
    public static function get(string $key, $default = null) 
    { 
        $val = self::init()->get( $key); 
        if (empty($val)) { 
            $val = $default; 
        $val を返し
        ます。
    } 

    /** 
     * @param string $key 
     * @param mixed $value 
     * @param int|null $expire 
     */ 
    public static function set(string $key, $value, int $expire = null) 
    { 
        return self::init()->set($key, $value, $expire); 
    / 

    ** 
     * @param string $key 
     * @return mixed 
     */ 
    public static function del(string $key) 
    { 
        return self::init()->delete($key); 
    / 

    ** 
     * 如果不存在写入缓存
     * @param string $key 
     * @param mixed $callable 
     * @param int|null $expire 
     * @return mixed 
     */ 
    public static function getOrSet(string $key, $callable, int $expire = null) 
    {
        return self::init()->getOrSet($key, $callable, $expire); 
    } 

    /** 
     * @return CacheInterface 
     */ 
    private static function init() 
    { 
        return Yii::$app->cache; 
    } 
}

おすすめ

転載: blog.csdn.net/hechenhongbo/article/details/124913138