XcacheCache.php 2.1 KB

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