1

我有一个使用 API 将 twitter 链接到它的应用程序,在此操作中,我创建了一个请求密钥,在此处完成:

 public function createRequestKey()
 {
     $this->setScenario(self::SCENARIO_DEFAULT);
     if (!$this->validate())
         return false;

     $cache = \Yii::app()->cache;
     $key = __CLASS__ . '_' . $this->user->id . '_' . time() . '_' . rand(0, 100);
     $key = sha1($key);
     $data = array(
         'userId' => $this->user->id,      
     );
     $cache->set($key, $data, 60 * 30);
     $this->requestKey = $key;
     return true;
 }

调用此方法的操作是这样的:

 public function actionTwitterLinkUrl()
 {
    if (!$this->checkOAuthRequest())
        return;

    $request  = \Yii::app()->getRequest();
    $response = \Yii::app()->response->setContentTypeToJSON();

    $user = $this->getUserFromHeader();
    $this->matchUserToLastValidatedToken($user);

    $linker = new \Sakanade\Operations\APITwitterLinker($user);
    if ($linker->createRequestKey()) {
        $params = $this->createQueryStringParams($linker->requestKey);
        $response->set('type',UserProfile::profileTypeToString(UserProfile::PROFILETYPE_TWITTER))
                 ->set('url', $request->getBaseUrl(true) . '/users/' . urlencode($user->id)
                 . '/profiles/twitter/link?' . http_build_query($params));
    } else {
        $response->setStatusCode(\Harusame\Components\HttpResponse::STATUS_BAD_REQUEST)
                 ->addMessage(\Sakanade\Models\Model::flattenErrorMessages($linker));
    }

    $response->render();
 }

但是在调用下一个操作时,如果我使用 $cache->get($key) 访问缓存,它会返回 false,即使它将缓存的存储时间设置为大约 1800 秒。

public function actionTwitterRunLink()
{
    $request = \Yii::app()->request;
    $requestKey = $request->getQuery('key');
    if (!$this->checkOAuthRequest())
        return;

    $user = $this->getUserFromHeader();
    $this->matchUserToLastValidatedToken($user);

    $authUrl = $this->getAuthorizationUrl($user);
    $response = \Yii::app()->response->setContentTypeToJSON();
    $cache = \Yii::app()->cache;
    $response->set('key', $cache->get($requestKey))
             ->set('key2', $requestKey);
    $response->render();
}

从缓存中显示呈现的键后,它会返回 false,但是如果我尝试从上一个操作中获取它,则会有一个返回值。为什么在调用另一个操作时会清除缓存?任何帮助将不胜感激!

4

1 回答 1

1

如果我没有记错缓存时间,如果没有从“现在”引用,我认为应该是:

$cache->set($key, $data, time() + (60 * 30));
于 2013-01-10T16:21:21.650 回答