//获取奖励返佣key值
if (CacheHelper::get(CacheKey::rewardRedKey($params))) {
throw new \Exception('此时有任务正在执行');
}
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
*/
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;
}
return $val;
}
/**
* @param string $key
* @param mixed $value
* @param int|null $expire
* @return mixed
*/
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;
}
}