Making WordPress.org

Changeset 4990


Ignore:
Timestamp:
02/23/2017 05:48:33 AM (10 years ago)
Author:
tellyworth
Message:

Plugin directory: cache search API call responses for 5 minutes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/libs/site-search/jetpack-search.php

    r4752 r4990  
    7171        public static $analyzed_langs = array( 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'eu', 'fa', 'fi', 'fr', 'he', 'hi', 'hu', 'hy', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' );
    7272
     73        const CACHE_GROUP = 'jetpack-search';
     74        const CACHE_EXPIRY = 300;
     75
    7376        protected function __construct() {
    7477                /* Don't do anything, needs to be initialized via instance() method */
     
    173176        public function search( $es_args ) {
    174177                $service_url = 'http://public-api.wordpress.com/rest/v1/sites/' . $this->jetpack_blog_id . '/search';
     178                $json_es_args = json_encode( $es_args );
     179                $cache_key = md5( $json_es_args );
     180               
     181                $cached_response = wp_cache_get( $cache_key, self::CACHE_GROUP );
     182                if ( $cached_response !== false )
     183                        return $cached_response;
    175184
    176185                $request = wp_remote_post( $service_url, array(
     
    186195                        return $request;
    187196
    188                 return json_decode( wp_remote_retrieve_body( $request ), true );
     197                $response = json_decode( wp_remote_retrieve_body( $request ), true );
     198
     199                if ( !isset( $response['error'] ) )
     200                        wp_cache_set( $cache_key, $response, self::CACHE_GROUP, self::CACHE_EXPIRY );
     201
     202                return $response;
    189203        }
    190204
Note: See TracChangeset for help on using the changeset viewer.