Нет описания

Cache.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Utils_Cache
  10. */
  11. class PUM_Utils_Cache {
  12. /**
  13. * @var string
  14. */
  15. static $prefix = 'pum';
  16. /**
  17. * @return bool
  18. */
  19. public static function enabled() {
  20. return (bool) ! pum_get_option( 'disable_cache', false );
  21. }
  22. /**
  23. * Returns the general
  24. *
  25. * @param string $string
  26. *
  27. * @return string
  28. */
  29. public static function prefix_( $string = '' ) {
  30. return empty( $string ) ? self::$prefix : self::$prefix . '_' . $string;
  31. }
  32. /**
  33. * @param $key
  34. * @param string $group
  35. *
  36. * @return mixed
  37. */
  38. public static function get_timeout( $key, $group = '' ) {
  39. return apply_filters( 'pum_cache_timeout', pum_cache_timeout( $group ), $key, $group );
  40. }
  41. /**
  42. * @param $key
  43. * @param $data
  44. * @param string $group
  45. *
  46. * @return bool
  47. */
  48. public static function add( $key, $data, $group = '' ) {
  49. if ( ! self::enabled() ) {
  50. return true;
  51. }
  52. return wp_cache_add( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
  53. }
  54. /**
  55. * @param $key
  56. * @param $data
  57. * @param string $group
  58. *
  59. * @return bool
  60. */
  61. public static function replace( $key, $data, $group = '' ) {
  62. if ( ! self::enabled() ) {
  63. return true;
  64. }
  65. return wp_cache_replace( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
  66. }
  67. /**
  68. * @param $key
  69. * @param $data
  70. * @param string $group
  71. *
  72. * @return bool
  73. */
  74. public static function set( $key, $data, $group = '' ) {
  75. if ( ! self::enabled() ) {
  76. return true;
  77. }
  78. return wp_cache_set( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
  79. }
  80. /**
  81. * @param $key
  82. * @param string $group
  83. * @param bool $force
  84. * @param null $found
  85. *
  86. * @return bool|mixed
  87. */
  88. public static function get( $key, $group = '', $force = false, &$found = null ) {
  89. if ( ! self::enabled() ) {
  90. return false;
  91. }
  92. return wp_cache_get( $key, self::prefix_( $group ), $force, $found );
  93. }
  94. /**
  95. * @param $key
  96. * @param string $group
  97. *
  98. * @return bool
  99. */
  100. public static function delete( $key, $group = '' ) {
  101. if ( ! self::enabled() ) {
  102. return true;
  103. }
  104. return wp_cache_delete( $key, self::prefix_( $group ) );
  105. }
  106. /**
  107. * @param string $group
  108. *
  109. * @return bool
  110. */
  111. public static function delete_group( $group = '' ) {
  112. if ( ! self::enabled() ) {
  113. return true;
  114. }
  115. if ( ! function_exists( 'wp_cache_delete_group' ) ) {
  116. return false;
  117. }
  118. return wp_cache_delete_group( self::prefix_( $group ) );
  119. }
  120. /**
  121. * @param $key
  122. * @param int $offset
  123. * @param string $group
  124. *
  125. * @return bool|false|int
  126. */
  127. public static function incr( $key, $offset = 1, $group = '' ) {
  128. if ( ! self::enabled() ) {
  129. return true;
  130. }
  131. return wp_cache_incr( $key, $offset, self::prefix_( $group ) );
  132. }
  133. /**
  134. * @param $key
  135. * @param int $offset
  136. * @param string $group
  137. *
  138. * @return bool|false|int
  139. */
  140. public static function decr( $key, $offset = 1, $group = '' ) {
  141. if ( ! self::enabled() ) {
  142. return true;
  143. }
  144. return wp_cache_decr( $key, $offset, self::prefix_( $group ) );
  145. }
  146. }