REST Controller: Built-in caching. #123

Closed
opened 2018-05-15 01:24:02 +00:00 by jamie · 5 comments
jamie commented 2018-05-15 01:24:02 +00:00 (Migrated from git.hazaar.io)

This should be quite simple to implement. Basically, I will generate a key based on the endpoint, arguments and query string and cache the result. Then next time we get the same query we can just return it immediately. This should speed up and cut down on some processing, especially when external network/database requests are involved.

There will be two operating modes.

  1. Cache by endpoint
  2. Cache everything

Caching by endpoint

This will involve a new phpdoc directive called @cache that takes a boolean parameter indicating if caching is enabled or not. If the default is to "cache everything" then setting this true will do nothing. Setting it to false will disable caching on this particular endpoint.

Example

/**
 * Find a list of available parent products for a participant with a specific product type
 *
 * @route('/participantProducts/<string:participantRole>/<string:participantAPIRCode>', method=["GET"])
 *
 * @cache true
 *
 * @param mixed $parentType The parent product type to return
 * @param mixed $apirCode   The participant to search for products under
 */

Cache Everything

This will involve an initialiser function called enableEndpointCaching(boolean). Setting this to true will enable caching on all endpoints by default. To disable caching for a single endpoint, use the above @cache false directive.

class ApiController extends \Hazaar\Controller\REST {

    public function init(){

        $this->enableEndpointCaching(true);

    }

}
This should be quite simple to implement. Basically, I will generate a key based on the endpoint, arguments and query string and cache the result. Then next time we get the same query we can just return it immediately. This should speed up and cut down on some processing, especially when external network/database requests are involved. There will be two operating modes. 1. Cache by endpoint 1. Cache everything # Caching by endpoint This will involve a new phpdoc directive called ```@cache``` that takes a boolean parameter indicating if caching is enabled or not. If the default is to "cache everything" then setting this *true* will do nothing. Setting it to *false* will disable caching on this particular endpoint. ## Example ```php /** * Find a list of available parent products for a participant with a specific product type * * @route('/participantProducts/<string:participantRole>/<string:participantAPIRCode>', method=["GET"]) * * @cache true * * @param mixed $parentType The parent product type to return * @param mixed $apirCode The participant to search for products under */ ``` # Cache Everything This will involve an initialiser function called *enableEndpointCaching(boolean)*. Setting this to *true* will enable caching on all endpoints by default. To disable caching for a single endpoint, use the above ```@cache false``` directive. ```php class ApiController extends \Hazaar\Controller\REST { public function init(){ $this->enableEndpointCaching(true); } } ```
jamie commented 2018-05-15 01:25:00 +00:00 (Migrated from git.hazaar.io)
created branch [`123-rest-controller-built-in-caching`](https://git.hazaarlabs.com/hazaar/hazaar-mvc/compare/master...123-rest-controller-built-in-caching)
jamie commented 2018-05-15 01:26:42 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !36

mentioned in merge request !36
jamie commented 2018-05-15 01:48:39 +00:00 (Migrated from git.hazaar.io)

closed via merge request !36

closed via merge request !36
jamie commented 2018-05-15 01:48:41 +00:00 (Migrated from git.hazaar.io)

mentioned in commit b5c116e0c7ee97026a70a046346bfa3e0fd57260

mentioned in commit b5c116e0c7ee97026a70a046346bfa3e0fd57260
jamie commented 2018-06-29 02:24:28 +00:00 (Migrated from git.hazaar.io)

changed milestone to %4

changed milestone to %4
jamie self-assigned this 2025-09-04 01:15:47 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#123
No description provided.