在CodeIgniter中缓存货币换算率的最佳方法

问题描述

|| 我的购物应用程序需要进行货币换算,我的计划是使用crontab每小时获取一次最新汇率。我的问题是在CI应用程序中存储和使用它的最佳方法是什么。我是不是该: -每次运行时将其写入数据库,然后从数据库evrytime中获取速率,我需要进行一次转换 -进行相同操作,但使用文本文件 -将其写入index.php中的常量 -某种核心或挂钩文件 什么是最有效的方法     

解决方法

        快速而肮脏的方法是使用上述货币编写一个PHP文件。您可以使用
var_export()
安全保存它们。 配置:
$file = \'/path/to/currencies.php\';
Cronjob:
// get data from API (note: you should use proper functions via CURL)
$currencies = file_get_contents(\'http://some.api.com/currencies\');
// decode data
$currencies = json_decode( currencies );
// export data into PHP format
$currencies = var_export( currencies,true );
// save to file
file_put_contents($file,\"<?php\\r\\n $currencies=\".$currencies.\"; \\r\\n?>\");
货币页面:
// ensure you get a default something
$currencies = array();
// load data file (note,we use include so we can load it whenever we want)
@include $file;
// output something from array
echo $currencies[\'EUR\'];
请注意,我们甚至都没有连接到数据库,这非常快。也没有解析/转换。使用数据库概念,您至少必须遍历数据库行。使用普通的配置媒体(例如ini,json或xml文件)在PHP加载后需要某种形式的解析。而使用这种方法,它是PHP加载过程的一部分;您只包含一个小的PHP文件。     ,        我认为最常见的方式(也很有效)是在数据库中有一个Currency表。 每种货币和汇率都有其自己的行。 根据所选的语言环境,在每个页面请求中从数据库中获取适当的货币信息。 在执行货币转换(基本货币的简单乘法)的函数中包装价格显示。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...