XcacheCache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use BadMethodCallException;
  4. use const XC_TYPE_VAR;
  5. use function ini_get;
  6. use function serialize;
  7. use function unserialize;
  8. use function xcache_clear_cache;
  9. use function xcache_get;
  10. use function xcache_info;
  11. use function xcache_isset;
  12. use function xcache_set;
  13. use function xcache_unset;
  14. /**
  15. * Xcache cache driver.
  16. *
  17. * @deprecated
  18. *
  19. * @link www.doctrine-project.org
  20. */
  21. class XcacheCache extends CacheProvider
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function doFetch($id)
  27. {
  28. return $this->doContains($id) ? unserialize(xcache_get($id)) : false;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function doContains($id)
  34. {
  35. return xcache_isset($id);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function doSave($id, $data, $lifeTime = 0)
  41. {
  42. return xcache_set($id, serialize($data), (int) $lifeTime);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function doDelete($id)
  48. {
  49. return xcache_unset($id);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function doFlush()
  55. {
  56. $this->checkAuthorization();
  57. xcache_clear_cache(XC_TYPE_VAR);
  58. return true;
  59. }
  60. /**
  61. * Checks that xcache.admin.enable_auth is Off.
  62. *
  63. * @return void
  64. *
  65. * @throws BadMethodCallException When xcache.admin.enable_auth is On.
  66. */
  67. protected function checkAuthorization()
  68. {
  69. if (ini_get('xcache.admin.enable_auth')) {
  70. throw new BadMethodCallException(
  71. 'To use all features of \Doctrine\Common\Cache\XcacheCache, '
  72. . 'you must set "xcache.admin.enable_auth" to "Off" in your php.ini.'
  73. );
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function doGetStats()
  80. {
  81. $this->checkAuthorization();
  82. $info = xcache_info(XC_TYPE_VAR, 0);
  83. return [
  84. Cache::STATS_HITS => $info['hits'],
  85. Cache::STATS_MISSES => $info['misses'],
  86. Cache::STATS_UPTIME => null,
  87. Cache::STATS_MEMORY_USAGE => $info['size'],
  88. Cache::STATS_MEMORY_AVAILABLE => $info['avail'],
  89. ];
  90. }
  91. }