我有一个脚本,通过RESTful Web服务将订单数据发送到第三方系统.该系统要求每个请求都发送一个唯一的ID,该请求将从下一个请求中自动增加.
我已经通过在Magento core_config_data表中添加一个变量来实现这一点,并且作为我的代码的一部分,调用下面的函数来获取ID的下一个值,并为下一个请求递增它.
class MyProject
{
public function getNextApiId() {
// Get the next ID.
$id = Mage::getStoreConfig('myproject/next_api_id');
// Increment the stored value for next time.
$nextId = $id + 1; // change $id++ by $id + 1 otherwise the result of $nextId = $id - 1;
Mage::getModel('core/config')->saveConfig('myproject/next_api_id',$nextId);
// Refresh the config.
Mage::getConfig()->cleanCache();
Mage::getConfig()->reinit();
// Return the ID.
return $id;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我用我的脚本发送一个请求,这很好 - 值递增,下一个ID用于下一次执行脚本.
但是,如果我在同一个脚本执行中的循环中处理多个请求,则该值似乎被缓存.下面的代码应该说明一般流程,尽管我为了简洁起见减少了它:
function sendRequest($item) {
$apiId = $MyProject->getNextApiId();
// Build and send request body
}
foreach($items as $item) {
sendRequest($item);
}
Run Code Online (Sandbox Code Playgroud)
这将导致初始ID号用于所有人$items.
在cleanCache()和reinit()尝试刷新配置高速缓存似乎并没有在所有的工作.关于如何阻止缓存价值的任何想法?
必须以不同的方式清理缓存,您必须重置存储的缓存并再次启动它导致循环.如果你没有循环,它也会被清理,但它需要商店的第二个url请求,它将启动缓存.
试试这个:
function getNextApiId() {
// Get the next ID.
$id = Mage::getStoreConfig('myproject/next_api_id');
// Increment the stored value for next time.
$nextId = $id + 1;
Mage::getConfig()->saveConfig('myproject/next_api_id',$nextId);
// Refresh the config.
Mage::app()->getStore()->resetConfig();
// Return the ID.
return $id;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3736 次 |
| 最近记录: |