Memcache.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Heanup\Frame\Library;
  3. /**
  4. * 缓存设置,使用memcache缓存数据
  5. */
  6. class Memcache extends \Heanup\Frame\Library
  7. {
  8. /**
  9. * 获取连接池
  10. * @return \Memcached
  11. */
  12. public function getConnection()
  13. {
  14. static $pool = array();
  15. $connectionName = $this->getFlag(true);
  16. if (!isset($pool[$connectionName]) || ($pool[$connectionName] == false)) {
  17. $cfg = $this->getConfig();
  18. $pool[$connectionName] = $this->connect($cfg);
  19. }
  20. return $pool[$connectionName];
  21. }
  22. /**
  23. * 连接Memcache
  24. * @param $cfg
  25. * @return \Memcached
  26. */
  27. protected function connect($cfg)
  28. {
  29. $connection = new \Memcached;
  30. $connection->setOption(\Memcached::OPT_COMPRESSION, true);
  31. $connection->setOption(\Memcached::OPT_DISTRIBUTION, true);
  32. $connection->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
  33. $connection->setOption(\Memcached::OPT_NO_BLOCK, true);
  34. $connection->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 50);
  35. $connection->setOption(\Memcached::OPT_POLL_TIMEOUT, 50);
  36. $connection->addServers($cfg);
  37. return $connection;
  38. }
  39. }