Apcu.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Apcu;
  11. /**
  12. * Apcu for Zend Server Data Cache.
  13. *
  14. * @author Kate Gray <opensource@codebykate.com>
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @internal
  18. */
  19. final class Apcu
  20. {
  21. public static function apcu_add($key, $var = null, $ttl = 0)
  22. {
  23. if (!\is_array($key)) {
  24. return apc_add($key, $var, $ttl);
  25. }
  26. $errors = array();
  27. foreach ($key as $k => $v) {
  28. if (!apc_add($k, $v, $ttl)) {
  29. $errors[$k] = -1;
  30. }
  31. }
  32. return $errors;
  33. }
  34. public static function apcu_store($key, $var = null, $ttl = 0)
  35. {
  36. if (!\is_array($key)) {
  37. return apc_store($key, $var, $ttl);
  38. }
  39. $errors = array();
  40. foreach ($key as $k => $v) {
  41. if (!apc_store($k, $v, $ttl)) {
  42. $errors[$k] = -1;
  43. }
  44. }
  45. return $errors;
  46. }
  47. public static function apcu_exists($keys)
  48. {
  49. if (!\is_array($keys)) {
  50. return apc_exists($keys);
  51. }
  52. $existing = array();
  53. foreach ($keys as $k) {
  54. if (apc_exists($k)) {
  55. $existing[$k] = true;
  56. }
  57. }
  58. return $existing;
  59. }
  60. public static function apcu_fetch($key, &$success = null)
  61. {
  62. if (!\is_array($key)) {
  63. return apc_fetch($key, $success);
  64. }
  65. $succeeded = true;
  66. $values = array();
  67. foreach ($key as $k) {
  68. $v = apc_fetch($k, $success);
  69. if ($success) {
  70. $values[$k] = $v;
  71. } else {
  72. $succeeded = false;
  73. }
  74. }
  75. $success = $succeeded;
  76. return $values;
  77. }
  78. public static function apcu_delete($key)
  79. {
  80. if (!\is_array($key)) {
  81. return apc_delete($key);
  82. }
  83. $success = true;
  84. foreach ($key as $k) {
  85. $success = apc_delete($k) && $success;
  86. }
  87. return $success;
  88. }
  89. }