Reputation: 2361
I have an app which links twitter to it with the API, in this action I have a creation of a request key which is done here:
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;
}
The action that calls this method is this:
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();
}
But upon calling the next action, if I access the cache using $cache->get($key) it returns to false, even though it set the cache's storage time of about 1800 seconds.
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();
}
Upon displaying the rendered key from the cache it returns to false but if I try to get it from the previous action there is a returned value. Why is the cache cleared upon calling another action? Any help will be greatly appreciated!
Upvotes: 1
Views: 577
Reputation: 2489
If im not mistaken cache time if not referenced from 'now' I think it should be :
$cache->set($key, $data, time() + (60 * 30));
Upvotes: 1