Making WordPress.org

Changeset 6633


Ignore:
Timestamp:
02/14/2018 01:00:05 AM (9 years ago)
Author:
iandunn
Message:

Currency_XRT_Client: Cache responses from the Fixer API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/utilities/class-currency-xrt-client.php

    r6596 r6633  
    7979        public function get_rates( $date ) {
    8080                $rates = array();
     81                $cache_key = 'wc_currency_rates_' . strtotime( $date );
    8182
    8283                try {
     
    8889                }
    8990
    90                 $cached_rates = $this->get_cached_rates( $date );
     91                $cached_rates = get_transient( $cache_key );
    9192
    9293                if ( false !== $cached_rates ) {
     
    107108                $rates = array_map( 'floatval', $rates );
    108109
    109                 $this->cache_rates( $date, $rates );
     110                set_transient( $cache_key, $rates, MONTH_IN_SECONDS );
    110111
    111112                return $rates;
     
    212213                return $data;
    213214        }
    214 
    215         /**
    216          * Check for cached currency exchange rates for a particular date and return them if available.
    217          *
    218          * @todo Add object and/or database caching.
    219          *
    220          * @param \DateTime $date The date to retrieve rates for.
    221          *
    222          * @return array|bool
    223          */
    224         protected function get_cached_rates( \DateTime $date ) {
    225                 if ( isset( $this->cache[ $date->format( 'Y-m-d' ) ] ) ) {
    226                         return $this->cache[ $date->format( 'Y-m-d' ) ];
    227                 }
    228 
    229                 return false;
    230         }
    231 
    232         /**
    233          * Cache the currency exchange rates for a particular date.
    234          *
    235          * @todo Add object and/or database caching.
    236          *
    237          * @param \DateTime $date The date of the rates to be cached.
    238          * @param array $rates    The rates to be cached.
    239          *
    240          * @return void
    241          */
    242         protected function cache_rates( \DateTime $date, $rates ) {
    243                 $this->cache[ $date->format( 'Y-m-d' ) ] = $rates;
    244         }
    245215}
Note: See TracChangeset for help on using the changeset viewer.