Skip to content

Instantly share code, notes, and snippets.

@kalbac
Created November 14, 2022 13:52
Show Gist options
  • Select an option

  • Save kalbac/5a0ee19dcee250594d40469b01fd9147 to your computer and use it in GitHub Desktop.

Select an option

Save kalbac/5a0ee19dcee250594d40469b01fd9147 to your computer and use it in GitHub Desktop.
Кеширование HTTP запросов
<?php
class Request extends Woodev_API_JSON_Request {
use Woodev_Cacheable_Request_Trait;
public function __construct() {
$this->cache_lifetime = 600; // override the deafult cache lifetime in seconds
}
// ...
}
class API extends Woodev_Cacheable_API_Base {
public function get_external_data( $refresh = false ) {
$request = $this->get_new_request();
if( $refresh ) {
$request->set_force_refresh( true );
}
return $this->perform_request( $request );
}
protected function get_new_request( $request_type = '' ) {
return new Request();
}
}
$api = new API();
$api->get_external_data(); // this should be a fresh request, the response will be cached
$api->get_external_data(); // this should use the cached response
$api->get_external_data( true ); // this should be a fresh request again, the response will be cached
// override cache timeout on runtime:
$request->set_cache_lifetime( 60 ); // note - this won't invalidate a previous cache with a longer lifetime - should it?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment