SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Drivers/Cache/Xcache.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers\Drivers\Cache;
  17.  
  18. use Kohana;
  19. use Kohana_Exception;
  20.  
  21. use Sprout\Helpers\Drivers\CacheDriver;
  22.  
  23.  
  24. /**
  25.  * Xcache Cache driver.
  26.  */
  27. class Xcache implements CacheDriver
  28. {
  29.  
  30. public function __construct()
  31. {
  32. if ( ! extension_loaded('xcache'))
  33. throw new Kohana_Exception('cache.extension_not_loaded', 'xcache');
  34. }
  35.  
  36. public function get($id)
  37. {
  38. if (xcache_isset($id))
  39. return xcache_get($id);
  40.  
  41. return NULL;
  42. }
  43.  
  44. public function set($id, $data, array $tags = NULL, $lifetime)
  45. {
  46. if ( ! empty($tags))
  47. {
  48. Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
  49. }
  50.  
  51. return xcache_set($id, $data, $lifetime);
  52. }
  53.  
  54. public function find($tag)
  55. {
  56. Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
  57. return FALSE;
  58. }
  59.  
  60. public function delete($id, $tag = FALSE)
  61. {
  62. if ($tag !== FALSE)
  63. {
  64. Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
  65. return TRUE;
  66. }
  67. elseif ($id !== TRUE)
  68. {
  69. if (xcache_isset($id))
  70. return xcache_unset($id);
  71.  
  72. return FALSE;
  73. }
  74. else
  75. {
  76. // Do the login
  77. $this->auth();
  78. $result = TRUE;
  79. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  80. {
  81. if (xcache_clear_cache(XC_TYPE_VAR, $i) !== NULL)
  82. {
  83. $result = FALSE;
  84. break;
  85. }
  86. }
  87.  
  88. // Undo the login
  89. $this->auth(TRUE);
  90. return $result;
  91. }
  92.  
  93. return TRUE;
  94. }
  95.  
  96. public function deleteExpired()
  97. {
  98. return TRUE;
  99. }
  100.  
  101. private function auth($reverse = FALSE)
  102. {
  103. static $backup = array();
  104.  
  105. $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW');
  106.  
  107. foreach ($keys as $key)
  108. {
  109. if ($reverse)
  110. {
  111. if (isset($backup[$key]))
  112. {
  113. $_SERVER[$key] = $backup[$key];
  114. unset($backup[$key]);
  115. }
  116. else
  117. {
  118. unset($_SERVER[$key]);
  119. }
  120. }
  121. else
  122. {
  123. $value = getenv($key);
  124.  
  125. if ( ! empty($value))
  126. {
  127. $backup[$key] = $value;
  128. }
  129.  
  130. $_SERVER[$key] = Kohana::config('cache_xcache.'.$key);
  131. }
  132. }
  133. }
  134.  
  135. } // End Cache Xcache Driver
  136.