Guest User

memory.php

a guest
Aug 8th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. class Memory
  4. {
  5.     private static $_instance = null;
  6.     private $_memcached = null;
  7.     private $_status = false;
  8.     private $_defaultExp;
  9.     private $_prefix = "rs";
  10.  
  11.  
  12.     private function __construct()
  13.     {
  14.         $memcached = new Memcached;
  15.         $host = '127.0.0.1';
  16.         $port = '11211';
  17.  
  18.  
  19.         $isMemcacheAvailable = $memcached->addServer($host,$port);
  20.         if ($isMemcacheAvailable) {
  21.             $this->_status = true;
  22.             $this->_memcached = $memcached;
  23.         }
  24.  
  25.         $this->_defaultExp = 60*60*24*7;
  26.     }
  27.  
  28.     public static function getInstance()
  29.     {
  30.         if(!isset(static::$_instance)){
  31.             static::$_instance = new Memory();
  32.         }
  33.         return static::$_instance;
  34.     }
  35.  
  36.     public function active()
  37.     {
  38.         return $this->_status;
  39.     }
  40.  
  41.     public function get($key)
  42.     {
  43.         return $this->_memcached->get(str_replace(' ','_',$this->_prefix . '_' . $key));
  44.     }
  45.     public function forget($key)
  46.     {
  47.         return $this->_memcached->delete(str_replace(' ','_',$this->_prefix . '_' . $key));
  48.     }
  49.     public function set($key,$value,$expiration = null)
  50.     {
  51.         $this->_memcached->set(str_replace(' ','_',$this->_prefix . '_' . $key),$value,$expiration);
  52.     }
  53.     public function error()
  54.     {
  55.         return $this->_memcached->getResultCode();
  56.     }
  57. }
Add Comment
Please, Sign In to add comment