Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <?php
  2. class CachedTourCMS {
  3.  
  4. // General settings
  5. protected $marketp_id = 0;
  6. protected $private_key = "";
  7. protected $result_type = "";
  8.  
  9. // Cache settings
  10. public $cache_dir = 'cache';
  11. public $cache_rules = array(
  12. "search_tours" => array("time" => 1800),
  13. "show_tour" => array("time" => 3600),
  14. "show_tour_datesanddeals" => array("time" => 900),
  15. "list_channels" => array("time" => 3600),
  16. "show_channel" => array("time" => 3600),
  17. "show_supplier" => array("time" => 3600)
  18. );
  19.  
  20. // TourCMS object
  21. protected $tourcms;
  22.  
  23. /**
  24. * __construct
  25. *
  26. * @author Paul Slugocki
  27. * @param $mp Marketplace ID
  28. * @param $k API Private Key
  29. * @param $res Result type, defaults to raw
  30. * @param $cache_rules
  31. */
  32. public function __construct($mp, $k, $res = "raw", $cache_rules = null) {
  33. $this->marketp_id = $mp;
  34. $this->private_key = $k;
  35. $this->result_type = $res;
  36.  
  37. if($cache_rules != null)
  38. $this->cache_rules = $cache_rules;
  39.  
  40. $this->tourcms = new TourCMS($mp, $k, $res);
  41. }
  42.  
  43. // Overload function
  44. public function __call($name, $arguments) {
  45.  
  46. // Request is cachable if we have settings for this method
  47. $cachable_request = array_key_exists($name, $this->cache_rules);
  48.  
  49. // If request is cachable, try loading
  50. if($cachable_request) {
  51.  
  52. $cached_content = $this->get_cache($name, $arguments);
  53.  
  54. if($cached_content != "") {
  55.  
  56. if($this->result_type == "simplexml")
  57. return simplexml_load_string($cached_content);
  58. else
  59. return $cached_content;
  60.  
  61. }
  62.  
  63. }
  64.  
  65. // No cache, outdated cache or uncacheable request
  66. // Call TourCMS object to get response
  67. $response = call_user_func_array(array($this->tourcms, $name), $arguments);
  68.  
  69. // Set cache
  70. if($cachable_request) {
  71.  
  72. $this->set_cache($name, $arguments, $response);
  73.  
  74. }
  75.  
  76. // Return
  77. return $response;
  78. }
  79.  
  80. // Get the cache for a given API query
  81. private function get_cache($name, $arguments) {
  82.  
  83. $filename = $this->get_filename($name, $arguments);
  84.  
  85. // Check the file exists
  86. if(file_exists($filename)) {
  87.  
  88. // Check it's not outdated
  89. $file_time = filemtime($filename);
  90. $cache_time = $this->cache_rules[$name]["time"];
  91.  
  92. $file_outdated = $file_time + $cache_time < time();
  93.  
  94. if(!$file_outdated) {
  95. return file_get_contents($filename);
  96. }
  97. }
  98.  
  99. // If we got this far, just return an empty cache
  100. return '';
  101. }
  102.  
  103. // Set the cache for a given API query
  104. private function set_cache($name, $arguments, $data) {
  105.  
  106. // If data is empty, return
  107. if($data == '')
  108. return;
  109.  
  110. $filename = $this->get_filename($name, $arguments);
  111.  
  112. // Create the method specific directory if it doesn't already exist
  113. if(!is_dir($this->cache_dir . DIRECTORY_SEPARATOR . $name))
  114. mkdir($this->cache_dir . DIRECTORY_SEPARATOR . $name);
  115.  
  116. // Make sure we have SimpleXML so we can check for errors
  117. if($this->result_type != "simplexml")
  118. $data = simplexml_load_string($data);
  119.  
  120. // As long as there's no errors, store the cache
  121. if($data->error == "OK") {
  122. $data = $data->asXml();
  123. file_put_contents($filename, $data);
  124. }
  125.  
  126. }
  127.  
  128. // Returns the full path to the cache file
  129. private function get_filename($name, $arguments) {
  130. return $this->cache_dir . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . $this->array_to_filename($arguments) . ".xml";
  131. }
  132.  
  133. // Convert an array into a filename
  134. private function array_to_filename($arr) {
  135. return base64_encode(serialize($arr));
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement