MemCacheProxy.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /** @package verysimple::Util */
  3. require_once("verysimple/Phreeze/CacheMemCache.php");
  4. /**
  5. * MemCacheProxy provides simple access to memcache pool but ignores
  6. * if the server is down instead of throwing an error. if the server
  7. * could not be contacted, ServerOffline will be set to true.
  8. *
  9. * @package verysimple::Util
  10. * @author VerySimple Inc.
  11. * @copyright 1997-2007 VerySimple, Inc.
  12. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  13. * @version 1.0
  14. */
  15. class MemCacheProxy extends CacheMemCache
  16. {
  17. public $ServerOffline = false;
  18. public $LastServerError = '';
  19. /**
  20. * Acts as a proxy for a MemCache server and fails gracefull if the pool cannot be contacted
  21. * @param array in host/port format: array('host1'=>'11211','host2'=>'11211')
  22. * @param string a unique string. prevents conflicts in case multiple apps are using the same memcached server bank
  23. */
  24. public function __construct($server_array = array('localhost'=>'11211'),$uniquePrefix = "CACHE-")
  25. {
  26. if (class_exists('Memcache'))
  27. {
  28. $memcache = new Memcache();
  29. foreach (array_keys($server_array) as $host)
  30. {
  31. // print "adding server $host " . $server_array[$host];
  32. $memcache->addServer($host, $server_array[$host]);
  33. }
  34. parent::__construct($memcache,$uniquePrefix,true);
  35. }
  36. else
  37. {
  38. $this->LastServerError = 'Memcache client module not installed';
  39. $this->ServerOffline = true;
  40. }
  41. }
  42. /**
  43. * @inheritdocs
  44. */
  45. public function Get($key,$flags=null)
  46. {
  47. // prevent hammering the server if it is down
  48. if ($this->ServerOffline) return null;
  49. return parent::Get($key,$flags);
  50. }
  51. /**
  52. * @inheritdocs
  53. */
  54. public function Set($key, $val, $flags = null, $timeout = 0)
  55. {
  56. // prevent hammering the server if it is down
  57. if ($this->ServerOffline) return null;
  58. return parent::Set($key, $val, $flags, $timeout);
  59. }
  60. /**
  61. * @inheritdocs
  62. */
  63. public function Delete($key)
  64. {
  65. // prevent hammering the server if it is down
  66. if ($this->ServerOffline) return null;
  67. return parent::Delete($key);
  68. }
  69. }
  70. ?>