Memcached.php 908 B

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