Yii:其他操作的数据缓存

时间:2012-12-05 02:58:42

标签: php caching yii twitter

我有一个应用程序,通过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,但如果我尝试从上一个操作获取它,则返回值。为什么在调用另一个动作时清除缓存?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如果没有错误的缓存时间,如果没有引用'now' 我认为应该是:

$cache->set($key, $data, time() + (60 * 30));