用于移动设备的 Magento 自定义模块 API 发送响应时间过长
Magento Custom module for Mobile API taking too much time to send response
我已经使用以下代码为移动 API 创建了自定义模块:
AppController.php
<?php
class <NameSpace>_<Module>_AppController extends Mage_Core_Controller_Front_Action {
public function loginAction() {
$postData = json_decode($_POST['login_details'], true);
if (($postData['email'] != '') && ($postData['password'] != '')) {
$email = $postData['email'];
$password = $postData['password'];
Mage::getSingleton("core/session", array("name" => "frontend"));
$customer = Mage::getModel("customer/customer");
$customer->website_id = Mage::app()->getWebsite()->getId();
$customer->setStore(Mage::app()->getStore());
try {
$customer->loadByEmail($email);
$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
$session->login($email, $password);
$response['success'] = '1';
$response['message'] = 'Login Successfull.';
$response['status_code'] = '200';
$response['data'] = $customer->getData();
} catch (Exception $e) {
$response['success'] = '0';
$response['message'] = $e->getMessage();
$response['status_code'] = $e->getCode();
}
} else {
$response['success'] = '0';
$response['message'] = 'Login Fail : Please Enter Email Id and Password.';
$response['status_code'] = '400';
}
echo json_encode($response);
}
}
Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<[NameSpace]_[Module]>
<version>0.1.0</version>
</[NameSpace]_[Module]>
</modules>
<frontend>
<routers>
<[module]>
<use>standard</use>
<args>
<module>[NameSpace]_[Module]</module>
<frontName>mobileapi</frontName>
</args>
</[module]>
</routers>
</frontend>
</config>
当我通过 Curl 调用登录 API 时,它花费了太多时间来显示响应:
<?php
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://[website-url]/mobileapi/app/login",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'email' => 'test@mail.com',
'password' => '123456',
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
echo "<pre>";
print_r($result);
curl_close($ch);
echo "<br/>CLOSE";
exit();
CURL 执行成功,但每次执行都花费太多时间。
谁能指导我减少自定义模块中 API 的执行时间?
我试过了,它对我来说很有效API响应:
public function loginAction() {
$postData = json_decode($_POST['login_details'], true);
$flag = 0;
$cacheId = 'login_' . $postData['email'].$postData['password'];
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
$data = unserialize($data);
if($data['success']==1){
$flag = 1;
$response=$data['response'];
}
}
if ($flag == 0) {
if (($postData['email'] != '') && ($postData['password'] != '')) {
$email = $postData['email'];
$password = $postData['password'];
Mage::getSingleton("core/session", array("name" => "frontend"));
$customer = Mage::getModel("customer/customer");
$customer->website_id = Mage::app()->getWebsite()->getId();
$customer->setStore(Mage::app()->getStore());
try {
$customer->loadByEmail($email);
$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
$session->login($email, $password);
$response['success'] = '1';
$response['message'] = 'Login Successfull.';
$response['status_code'] = '200';
$response['data'] = $customer->getData();
$data['success'] = 1;
$data['response']=$response;
Mage::app()->getCache()->save(serialize($data), $cacheId);
} catch (Exception $e) {
$response['success'] = '0';
$response['message'] = $e->getMessage();
$response['status_code'] = $e->getCode();
}
} else {
$response['success'] = '0';
$response['message'] = 'Login Fail : Please Enter Email Id and Password.';
$response['status_code'] = '400';
}
}
echo json_encode($response);
exit;
}
我已经使用以下代码为移动 API 创建了自定义模块:
AppController.php
<?php
class <NameSpace>_<Module>_AppController extends Mage_Core_Controller_Front_Action {
public function loginAction() {
$postData = json_decode($_POST['login_details'], true);
if (($postData['email'] != '') && ($postData['password'] != '')) {
$email = $postData['email'];
$password = $postData['password'];
Mage::getSingleton("core/session", array("name" => "frontend"));
$customer = Mage::getModel("customer/customer");
$customer->website_id = Mage::app()->getWebsite()->getId();
$customer->setStore(Mage::app()->getStore());
try {
$customer->loadByEmail($email);
$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
$session->login($email, $password);
$response['success'] = '1';
$response['message'] = 'Login Successfull.';
$response['status_code'] = '200';
$response['data'] = $customer->getData();
} catch (Exception $e) {
$response['success'] = '0';
$response['message'] = $e->getMessage();
$response['status_code'] = $e->getCode();
}
} else {
$response['success'] = '0';
$response['message'] = 'Login Fail : Please Enter Email Id and Password.';
$response['status_code'] = '400';
}
echo json_encode($response);
}
}
Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<[NameSpace]_[Module]>
<version>0.1.0</version>
</[NameSpace]_[Module]>
</modules>
<frontend>
<routers>
<[module]>
<use>standard</use>
<args>
<module>[NameSpace]_[Module]</module>
<frontName>mobileapi</frontName>
</args>
</[module]>
</routers>
</frontend>
</config>
当我通过 Curl 调用登录 API 时,它花费了太多时间来显示响应:
<?php
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://[website-url]/mobileapi/app/login",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'email' => 'test@mail.com',
'password' => '123456',
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
echo "<pre>";
print_r($result);
curl_close($ch);
echo "<br/>CLOSE";
exit();
CURL 执行成功,但每次执行都花费太多时间。 谁能指导我减少自定义模块中 API 的执行时间?
我试过了,它对我来说很有效API响应:
public function loginAction() {
$postData = json_decode($_POST['login_details'], true);
$flag = 0;
$cacheId = 'login_' . $postData['email'].$postData['password'];
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
$data = unserialize($data);
if($data['success']==1){
$flag = 1;
$response=$data['response'];
}
}
if ($flag == 0) {
if (($postData['email'] != '') && ($postData['password'] != '')) {
$email = $postData['email'];
$password = $postData['password'];
Mage::getSingleton("core/session", array("name" => "frontend"));
$customer = Mage::getModel("customer/customer");
$customer->website_id = Mage::app()->getWebsite()->getId();
$customer->setStore(Mage::app()->getStore());
try {
$customer->loadByEmail($email);
$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
$session->login($email, $password);
$response['success'] = '1';
$response['message'] = 'Login Successfull.';
$response['status_code'] = '200';
$response['data'] = $customer->getData();
$data['success'] = 1;
$data['response']=$response;
Mage::app()->getCache()->save(serialize($data), $cacheId);
} catch (Exception $e) {
$response['success'] = '0';
$response['message'] = $e->getMessage();
$response['status_code'] = $e->getCode();
}
} else {
$response['success'] = '0';
$response['message'] = 'Login Fail : Please Enter Email Id and Password.';
$response['status_code'] = '400';
}
}
echo json_encode($response);
exit;
}