MyMemcache.php 803 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. // memcache操作类
  3. class MyMemcache{
  4. public $conn;
  5. public $isMemcache =true;
  6. public function __construct($host="127.0.0.1",$port='11211')
  7. {
  8. // 建立连接
  9. if(class_exists('MyMemcache')){
  10. $obj =new \memcached();
  11. }else{
  12. $this ->isMemcache =false;
  13. $obj =new \memcached();
  14. }
  15. $obj ->addServer($host,$port);
  16. $this ->conn =$obj;
  17. }
  18. //获取数据
  19. public function get($key)
  20. {
  21. return $this->conn->get($key);
  22. }
  23. //设置数据
  24. public function set($key,$value,$expire=0)
  25. {
  26. if($this->isMemcache){
  27. $this->conn->set($key,$value,$expire);
  28. }else{
  29. // Memcached扩展的操作方式
  30. $this->conn->set($key,$value,$expire);
  31. }
  32. }
  33. }