亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否有更好的循環遍歷這些 PHP 代碼的方法,也許使用 foreach 循環?

是否有更好的循環遍歷這些 PHP 代碼的方法,也許使用 foreach 循環?

PHP
心有法竹 2022-12-23 16:19:04
我file_get_contents用來獲取遠程定價(每天更新),substr用來只保留我想要的部分(從輸出中剝離貨幣符號和其他數據,只保留數字)并file_put_contents用來將它存儲到我引用的緩存目錄中到以后。這就是我現在所擁有的:-<?php$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';// Small Plan - US$cachefile_SM_US = $cacheDirectory . 'SM_US.cache';if(file_exists($cachefile_SM_US)) {    if(time() - filemtime($cachefile_SM_US) > 1600) {        // too old , re-fetch       $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');        $substr_SM_US = substr($cache_SM_US,17,2);        file_put_contents($cachefile_SM_US, $substr_SM_US);        } else {            // cache is still fresh    }} else {    // no cache, create one    $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');    $substr_SM_US = substr($cache_SM_US,17,2);    file_put_contents($cachefile_SM_US, $substr_SM_US);}// Large Plan - US$cachefile_LG_US = $cacheDirectory . 'LG_US.cache';if(file_exists($cachefile_LG_US)) {    if(time() - filemtime($cachefile_LG_US) > 1600) {        // too old , re-fetch        $cache_LG_US = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=1');        $substr_LG_US = substr($cache_LG_US,17,2);        file_put_contents($cachefile_LG_US, $substr_LG_US);    } else {        // cache is still fresh    }} else {    // no cache, create one    $cache_LG_US = file_get_contents('https://remotedomain.com/?get=price&product=20&currency=1');    $substr_LG_US = substr($cache_LG_US,17,2);    file_put_contents($cachefile_LG_US, $substr_LG_US);}當只有兩種產品 (10和20) 和兩種貨幣 (1和2) 時,這種手動方式有效,因為我只需要這樣做 4 次就可以獲得我需要的所有定價。但是,我打算將產品數量顯著擴大到至少 12 種產品和 9 種貨幣,因此手動完成它們是不現實的。我相信這可以通過 PHP foreach 循環更有效地完成,但我嘗試了幾天但沒能成功,這可能是因為我對這個概念的理解較弱。
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

絕對地。看看這個例子:)


<?php declare(strict_types=1);


interface CacheNormalizer

{

    public function normalize(string $text): string;

}


interface PlanDomainToCache

{

    public function buildUrl(Plan $plan): string;

}


final class CachedRemoteSiteManager

{

    /** @var int Time To Live Cache */

    private $timeToLive;


    /** @var CacheNormalizer */

    private $cacheNormalizer;


    /** @var PlanDomainToCache */

    private $planDomainToCache;


    public function __construct(

        int $timeToLive,

        CacheNormalizer $cacheNormalizer,

        PlanDomainToCache $planDomainToCache

    ) {

        $this->timeToLive = $timeToLive;

        $this->cacheNormalizer = $cacheNormalizer;

        $this->planDomainToCache = $planDomainToCache;

    }


    public function updateIfNecessary(Plan $plan): void

    {

        if ($this->shouldCreateOrUpdateCache($plan)) {

            $this->createOrUpdateCache($plan);

        }

    }


    private function shouldCreateOrUpdateCache(Plan $plan): bool

    {

        return !file_exists($plan->cacheDirectory())

            || time() - filemtime($plan->cacheDirectory()) > $this->timeToLive;

    }


    private function createOrUpdateCache(Plan $plan): void

    {

        $urlToCache = $this->planDomainToCache->buildUrl($plan);

        $textToCache = file_get_contents($urlToCache);


        file_put_contents(

            $plan->cacheDirectory(),

            $this->cacheNormalizer->normalize($textToCache)

        );

    }

}


final class Plan

{

    /** @var string */

    private $cacheDirectory;


    /** @var int */

    private $product;


    /** @var int */

    private $currency;


    public function __construct(string $cacheDir, int $product, int $currency)

    {

        $this->cacheDirectory = $cacheDir;

        $this->product = $product;

        $this->currency = $currency;

    }


    public function cacheDirectory(): string

    {

        return $this->cacheDirectory;

    }


    public function product(): int

    {

        return $this->product;

    }


    public function currency(): int

    {

        return $this->currency;

    }

}


// Usage example:


$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

$productA = 10;

$productB = 20;

$USD = 1;

$EUR = 2;


/** @var Plan[] */

$plansToCache = [

    new Plan($cacheDirectory . 'SM_US.cache', $productA, $USD),

    new Plan($cacheDirectory . 'LG_US.cache', $productB, $USD),

    new Plan($cacheDirectory . 'SM_EU.cache', $productA, $EUR),

    new Plan($cacheDirectory . 'LG_EU.cache', $productB, $EUR),

];


$cacheManager = new CachedRemoteSiteManager(

    $cacheTtl = 1600,

    new class implements CacheNormalizer {

        public function normalize(string $text): string

        {

            return substr($text, 17, 2);

        }

    },

    new class implements PlanDomainToCache {

        public function buildUrl(Plan $plan): string

        {

            return sprintf(

                'https://remotedomain.com/?get=price&product=%d&currency=%d',

                $plan->product(),

                $plan->currency()

            );

        }

    }

);


foreach ($plansToCache as $plan) {

    $cacheManager->updateIfNecessary($plan);

}

正如您在底部看到的,在“使用示例”中,我提取了所有細節(幾乎所有細節),因此我們可以輕松定義:

  • 我們要如何規范化緩存數據(使用 CacheNormalizer)

  • 我們要如何構建要緩存的 URL(使用 PlanDomainToCache)。

更新:

如果您想了解如何從結束代碼中提取/解耦每個細節,即使對于“持久性”層也向上反轉依賴關系:https ://gist.github.com/Chemaclass/01d3f42685ff69f6897192202a32014d


查看完整回答
反對 回復 2022-12-23
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

如果我正確地解釋了代碼,您想要查找兩種貨幣的兩種產品。這可以在您定義產品和貨幣后使用嵌套的 foreach 循環來完成。


$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

$url = 'https://remotedomain.com/?get=price';


const MAX_CACHE_TIME = 1600;


// Optional

$output = [];


$productList = [

    [

        'id'   => 10,

        'name' => 'SM',

    ],

    [

        'id'   => 20,

        'name' => 'LG',

    ]

];


$currencies = [

    'US' => 1,

    'EU' => 2,

];


foreach ($productList as $product) {

    foreach ($currencies as $currencyName => $currencyId) {

        $cacheFile = $cacheDirectory . $product['name'] . '_' . $currencyName . '.cache';


        if (!file_exists($cacheFile) || filemtime($cacheFile) > MAX_CACHE_TIME) {

            // No cache or too old

            $data = file_get_contents($url . '&product=' . $product['id'] . '&currency=' . $currencyId);

            $relevantData = substr($data, 17, 2);

            file_put_contents($cacheFile, $relevantData);

            // Optional, put the data in an array

            $output[$product['id']][$currencyId] = $relevantData;

        } else {

            $output[$product['id']][$currencyId] = file_get_contents($cacheFile);

        }


    }

}

// Read outputwith $output[10]['US'] for example


查看完整回答
反對 回復 2022-12-23
  • 2 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號