PHP模拟redis字符串结构

class Sdshrd{
 		public $len;#已经占用的空间长度
 		public $free;#还剩余空间数量
 		public $arr;#存储数据位置
 		public  $defaut=512;
 		public function __construct($value){
 			$this->arr = new SplFixedArray($this->defaut);
 			for($i=0;$i<strlen($value);$i++){
 				$this->arr[$i]=$value[$i];
 			}
 			$this->len=strlen($value);
 			$this->free = $this->defaut-strlen($value);
 		}
 	}

 	class Ticket{
 		private $check=array();
 		private $default=512;
 		public function __construct(){
 			$this->check = new SplFixedArray($this->default);
 		}
 		private function hash($name){
 			$total=0;
 			for($i=0;$i<strlen($name);$i++){
 				$total+=ord($name[$i]);
 			}
 			return $total%$this->default;
 		}
 		public function set($name,$value){
 			$index  = $this->hash($name);
 			$objsds = new Sdshrd($value);
 			$this->check[$index]=$objsds;
 		}

 		public function get($name){
 			$str="";
 			$index = $this->hash($name);
 			$obj = $this->check[$index];
 			foreach ($obj->arr as $key => $value) {
 				$str.=$value;
 			}

 			return $str;
 		}
 	}
 	$objticek = new Ticket();
 	$name="name";
 	$value="lisi";
 	$objticek->set($name,$value);
 	$str = $objticek->get($name);
 	echo $str;

  

猜你喜欢

转载自www.cnblogs.com/zh718594493/p/12099593.html