1: <?php
  2:   3:   4:   5:   6:   7: 
  8: 
  9: namespace Wei;
 10: 
 11:  12:  13:  14:  15: 
 16: class ArrayCache extends BaseCache
 17: {
 18:      19:  20:  21:  22: 
 23:     protected $data = array();
 24: 
 25:      26:  27: 
 28:     public function get($key, $expire = null, $fn = null)
 29:     {
 30:         $oriKey = $key;
 31:         $key = $this->prefix . $key;
 32:         $result = array_key_exists($key, $this->data) ? $this->data[$key] : false;
 33:         return $this->processGetResult($oriKey, $result, $expire, $fn);
 34:     }
 35: 
 36:      37:  38: 
 39:     public function set($key, $value, $expire = 0)
 40:     {
 41:         $this->data[$this->prefix . $key] = $value;
 42:         return true;
 43:     }
 44: 
 45:      46:  47: 
 48:     public function remove($key)
 49:     {
 50:         if (isset($this->data[$this->prefix . $key])) {
 51:             unset($this->data[$this->prefix . $key]);
 52:             return true;
 53:         } else {
 54:             return false;
 55:         }
 56:     }
 57: 
 58:      59:  60: 
 61:     public function exists($key)
 62:     {
 63:         return array_key_exists($this->prefix . $key, $this->data);
 64:     }
 65: 
 66:      67:  68: 
 69:     public function add($key, $value, $expire = 0)
 70:     {
 71:         if ($this->exists($key)) {
 72:             return false;
 73:         } else {
 74:             return $this->set($key, $value);
 75:         }
 76:     }
 77: 
 78:      79:  80: 
 81:     public function replace($key, $value, $expire = 0)
 82:     {
 83:         if (!$this->exists($key)) {
 84:             return false;
 85:         } else {
 86:             $this->data[$this->prefix . $key] = $value;
 87:             return true;
 88:         }
 89:     }
 90: 
 91:      92:  93: 
 94:     public function incr($key, $offset = 1)
 95:     {
 96:         if ($this->exists($key)) {
 97:             return $this->data[$this->prefix . $key] += $offset;
 98:         } else {
 99:             return $this->data[$this->prefix . $key] = $offset;
100:         }
101:     }
102: 
103:     104: 105: 
106:     public function clear()
107:     {
108:         $this->data = array();
109:         return true;
110:     }
111: }
112: