Redis is a common non-relational database, mainly used as data cache, the data is saved in the form of Key-value, and the key values are mapped. Its data storage is different from MySQL, its data is stored in memory, so the data read relatively quickly, to do high concurrency is very good.
ThinkPhP5.0 has a redis extension and downloads the Php_redis.dll URL http://windows.php.net/downloads/pecl/releases/redis/2.2.7/before use; According to your own Windows operating system to choose the appropriate version, I am the system 64-bit, installed is VC2012 so download is Php_redis-2.2.7-5.6-ts-vc11-x64.zip
After downloading the compressed package, the inside of the Php_redis.dll decompression to D:\wamp\bin\php\php5.6.25\ext (according to their own wamp on the disk to choose), and then add extension=php_ inside the php.ini Redis.dll, restart Apache to do it;
Below is my own test code, can be used, the package is not many, can be based on their own needs to do the package
Extend is thinkPHP5.0.扩展类库目录,可以自己去定义
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596 |
namespaceMy; //目录我放在thinkphp5.0/extend/My classRedisPackage{ protectedstatic$handler= null; protected$options= [ ‘host‘=> ‘127.0.0.1‘, ‘port‘=> 6379, ‘password‘ => ‘‘, ‘select‘=> 0, ‘timeout‘=> 0, //关闭时间 0:代表不关闭 ‘expire‘=> 0, ‘persistent‘=> false, ‘prefix‘=> ‘‘, ]; publicfunction__construct($options= []) { if(!extension_loaded(‘redis‘)) { //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常) thrownew\BadFunctionCallException(‘not support: redis‘); } if(!empty($options)) { $this->options = array_merge($this->options, $options); } $func= $this->options[‘persistent‘] ? ‘pconnect‘: ‘connect‘; //判断是否长连接 self::$handler= new\Redis; self::$handler->$func($this->options[‘host‘], $this->options[‘port‘], $this->options[‘timeout‘]); if(‘‘!= $this->options[‘password‘]) { self::$handler->auth($this->options[‘password‘]); } if (0 != $this->options[‘select‘]) { self::$handler->select($this->options[‘select‘]); } } /** * 写入缓存 * @param string $key 键名 * @param string $value 键值 * @param int $exprie 过期时间 0:永不过期 * @return bool */ publicstaticfunctionset($key, $value, $exprie= 0) { if ($exprie== 0) { $set= self::$handler->set($key, $value); } else{ $set= self::$handler->setex($key, $exprie, $value); } return $set; } /** * 读取缓存 * @param string $key 键值 * @return mixed */ publicstaticfunctionget($key) { $fun= is_array($key) ? ‘Mget‘ : ‘get‘; returnself::$handler->{$fun}($key); } /** * 获取值长度 * @param string $key * @return int */ publicstaticfunctionlLen($key) { returnself::$handler->lLen($key); } /** * 将一个或多个值插入到列表头部 * @param $key * @param $value * @return int */ publicstaticfunctionLPush($key, $value, $value2 = null, $valueN= null) { returnself::$handler->lPush($key, $value, $value2, $valueN); } /** * 移出并获取列表的第一个元素 * @param string $key * @return string */ publicstaticfunctionlPop($key) { returnself::$handler->lPop($key); }} |
Use and encapsulation of Redis in ThinkPHP5.0