PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/core/gw_includes/class.cached_units.php

http://glossword.googlecode.com/
PHP | 207 lines | 160 code | 2 blank | 45 comment | 15 complexity | 82cccb971cfe4b83094ef3776bac5f51 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Dmitry N. Shilnikov, 2006-2010
  5. * @license Commercial
  6. */
  7. /**
  8. * Class to store PHP-variables in a cache.
  9. *
  10. * All cached units are stored in database. We can't use a file system because of
  11. * problems with clearing expired units.
  12. *
  13. * Changes:
  14. * 19 Jan 2010: make_bigint16() renamed to make_bigint().
  15. * 2 Sep 2009: Longer numbers for make_bigint16() (18 characters).
  16. * 18 Aug 2009: Changed algorithm for make_bigint16(). Numbers are always started with 1.
  17. */
  18. class site_cache
  19. {
  20. /* Cache life time in seconds */
  21. public $lifetime_sec = 60;
  22. public $id_unit = 0;
  23. public $arStatus = array();
  24. public $db_tablename = 'cached_units';
  25. public $unit_group = '';
  26. public $is_enable = 1;
  27. public $is_expired = 1;
  28. public $is_exist = 0;
  29. public $diff_sec = 0;
  30. public $oDb;
  31. public $value;
  32. public $str_seconds = 'sec.';
  33. public $str_minutes = 'min.';
  34. public $str_hours = 'hrs';
  35. public $str_days = 'days';
  36. private $datetime;
  37. /* */
  38. public function __construct(&$oDb, $lifetime_sec)
  39. {
  40. $this->datetime = @date( "Y-m-d H:i:s", mktime() );
  41. $this->oDb = $oDb;
  42. $this->lifetime_sec = $lifetime_sec;
  43. }
  44. /* */
  45. public function text_sec_to_str($sec)
  46. {
  47. $str['days'] = intval( ($sec / 3600) / 24 );
  48. $str['hours'] = intval( ($sec - (($str['days'] * 24) * 3600)) / 3600 );
  49. $str['minutes'] = intval(($sec - (($str['days'] * 24 * 3600) + ($str['hours'] * 3600))) / 60);
  50. $str['seconds'] = $sec - (($str['days'] * 24 * 3600) + ($str['hours'] * 3600) + ($str['minutes'] * 60));
  51. $str['days'] .= ' '.$this->str_days;
  52. $str['hours'] .= ' '.$this->str_hours;
  53. $str['minutes'] .= ' '.$this->str_minutes;
  54. $str['seconds'] .= ' '.$this->str_seconds;
  55. return implode(' ', $str);
  56. }
  57. /**
  58. * Loads cached object.
  59. *
  60. * @param string Unit name.
  61. * @param string Unit group name. Default is ''.
  62. * @param int Lifetime in seconds. Default is 0.
  63. * @return TRUE if success, FALSE otherwise.
  64. */
  65. public function in_cache( $id_unit, $unit_group = '', $lifetime_sec = 0 )
  66. {
  67. if ( !$this->is_enable ){ return false; }
  68. /* Generate unique numbers */
  69. $this->id_unit = $this->make_bigint( $id_unit );
  70. $this->unit_group = $this->make_bigint( $unit_group );
  71. /* An individual lifetime for each cached object */
  72. $this->lifetime_sec = $lifetime_sec;
  73. /* Check for cached object in database */
  74. $this->oDb->select( '*' );
  75. $this->oDb->from( $this->db_tablename );
  76. $this->oDb->where( array( 'id_unit' => $this->id_unit, 'unit_group' => $this->unit_group ) );
  77. $this->oDb->limit( 1 );
  78. $ar_sql = $this->oDb->get()->result_array();
  79. if ( empty($ar_sql) )
  80. {
  81. /* No results */
  82. $this->is_exist = 0;
  83. $this->is_expired = 1;
  84. return false;
  85. }
  86. $this->is_exist = 1;
  87. /* Check if the cache unit has been expired */
  88. if ( $lifetime_sec )
  89. {
  90. /* Set `expired` by default */
  91. $this->is_expired = 1;
  92. /* Calculate the difference */
  93. $this->diff_sec = mktime() - strtotime( $ar_sql[0]['cdate'] );
  94. if ( $this->diff_sec <= $lifetime_sec )
  95. {
  96. /* Set `not expired` */
  97. $this->is_expired = 0;
  98. }
  99. if ( $this->is_expired )
  100. {
  101. return false;
  102. }
  103. }
  104. /* Load cache unit contents */
  105. $this->value = $ar_sql[0]['unit_value'];
  106. return true;
  107. }
  108. /* */
  109. static public function make_bigint( $s )
  110. {
  111. $h = hash( 'md5', $s );
  112. $n = '1';
  113. for ( $i = 0; $i < 32; $i++ )
  114. {
  115. $n .= hexdec( substr( $h, $i, 2 ) );
  116. if ( strlen( $n ) > 18 )
  117. {
  118. $n = substr( $n, 0, 18 );
  119. break;
  120. }
  121. $i++;
  122. }
  123. return strval( $n );
  124. }
  125. /* */
  126. public function remove_by_group( $s )
  127. {
  128. $this->remove( 0, $s );
  129. }
  130. public function remove_by_unit( $s )
  131. {
  132. $this->remove( $s, 0 );
  133. }
  134. private function remove( $id_unit_ref = 0, $unit_group = 0 )
  135. {
  136. $ar = array();
  137. if ( $id_unit_ref )
  138. {
  139. $ar['id_unit'] = $this->make_bigint( $id_unit_ref );
  140. }
  141. if ( $unit_group )
  142. {
  143. $ar['unit_group'] = $this->make_bigint( $unit_group );
  144. }
  145. $this->oDb->delete( $this->db_tablename, $ar );
  146. }
  147. /* */
  148. public function load()
  149. {
  150. $this->arStatus[] = 'cache load: ' . $this->id_unit. ' (expired in ' . $this->text_sec_to_str( $this->lifetime_sec-$this->diff_sec ) .')';
  151. return unserialize($this->value);
  152. }
  153. /* */
  154. public function save($value)
  155. {
  156. if ( !$this->is_enable ){ return $value; }
  157. /* $this->id_unit should exist */
  158. if ( $this->is_expired && !$this->is_exist )
  159. {
  160. $this->_add_unit( $this->id_unit, $value );
  161. }
  162. elseif ( $this->is_expired && $this->is_exist )
  163. {
  164. $this->_update_unit( $this->id_unit, $value );
  165. }
  166. return $value;
  167. }
  168. /* */
  169. public function events()
  170. {
  171. return $this->arStatus;
  172. }
  173. /* */
  174. public function debug()
  175. {
  176. print '<pre class="debug">';
  177. print_r( $this->arStatus );
  178. print '</pre>';
  179. }
  180. /* */
  181. private function _add_unit($id_unit, $value)
  182. {
  183. if ( $this->oDb->insert( $this->db_tablename, array(
  184. 'id_unit' => $this->id_unit,
  185. 'cdate' => $this->datetime,
  186. 'unit_group' => $this->unit_group,
  187. 'unit_value' => serialize( $value )
  188. ) ) )
  189. {
  190. $this->arStatus[] = 'cache save: ' . $this->id_unit. ' (expired in ' . $this->text_sec_to_str( $this->lifetime_sec ) .')';
  191. }
  192. }
  193. /* */
  194. private function _update_unit($id_unit, $value)
  195. {
  196. if ($this->oDb->update( $this->db_tablename, array(
  197. 'cdate' => $this->datetime,
  198. 'unit_value' => serialize( $value )
  199. ), array( 'id_unit' => $this->id_unit ) ) )
  200. {
  201. $this->arStatus[] = 'cache save: ' . $this->id_unit. ' (expired in ' . $this->text_sec_to_str( $this->lifetime_sec ) .')';
  202. }
  203. }
  204. }
  205. ?>