Memcache.php 924 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace ActiveRecord;
  3. class Memcache
  4. {
  5. const DEFAULT_PORT = 11211;
  6. private $memcache;
  7. /**
  8. * Creates a Memcache instance.
  9. *
  10. * Takes an $options array w/ the following parameters:
  11. *
  12. * <ul>
  13. * <li><b>host:</b> host for the memcache server </li>
  14. * <li><b>port:</b> port for the memcache server </li>
  15. * </ul>
  16. * @param array $options
  17. */
  18. public function __construct($options)
  19. {
  20. $this->memcache = new \Memcache();
  21. $options['port'] = isset($options['port']) ? $options['port'] : self::DEFAULT_PORT;
  22. if (!$this->memcache->connect($options['host'],$options['port']))
  23. throw new CacheException("Could not connect to $options[host]:$options[port]");
  24. }
  25. public function flush()
  26. {
  27. $this->memcache->flush();
  28. }
  29. public function read($key)
  30. {
  31. return $this->memcache->get($key);
  32. }
  33. public function write($key, $value, $expire)
  34. {
  35. $this->memcache->set($key,$value,null,$expire);
  36. }
  37. }
  38. ?>