Home >PHP Framework >ThinkPHP >How is Redis used and encapsulated in the ThinkPHP5 framework?
The followingthinkphp frameworkThe tutorial column will introduce to you how to use and encapsulate Redis in ThinkPHP5.0. I hope it will be helpful to friends in need!
Redis is a commonly used non-relational database, mainly used for data caching. The data is saved in the form of key-value, and the key values map to each other. Its data storage is different from MySQL, and its data is stored in memory. , so data reading is relatively fast, which is very good for high concurrency.
ThinkPhP5.0 comes with a Redis extension. Before using it, download php_redis.dll at http://windows. php.net/downloads/pecl/releases/redis/2.2.7/ ;Choose the corresponding version according to your windows operating system. My system is 64-bit and I installed VC2012, so I downloaded php_redis-2.2.7-5.6- ts-vc11-x64.zip
After downloading the compressed package, unzip the php_redis.dll inside to D:\wamp\bin\php\php5.6.25\ext (choose according to the disk where your wamp is located) ), then add extension=php_redis.dll in php.ini and restart apache;
The following is the code I tested myself. It can be used. There are not many packages. You can customize it according to your own needs. Hands-on encapsulation
extend is the extended class library directory of thinkPHP5.0. You can define it yourself
namespace My; //目录我放在thinkphp5.0/extend/My class RedisPackage { protected static $handler = null; protected $options = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, //关闭时间 0:代表不关闭 'expire' => 0, 'persistent' => false, 'prefix' => '', ]; public function __construct($options = []) { if (!extension_loaded('redis')) { //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常) throw new \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 */ public static function set($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 */ public static function get($key) { $fun = is_array($key) ? 'Mget' : 'get'; return self::$handler->{$fun}($key); } /** * 获取值长度 * @param string $key * @return int */ public static function lLen($key) { return self::$handler->lLen($key); } /** * 将一个或多个值插入到列表头部 * @param $key * @param $value * @return int */ public static function LPush($key, $value, $value2 = null, $valueN = null) { return self::$handler->lPush($key, $value, $value2, $valueN); } /** * 移出并获取列表的第一个元素 * @param string $key * @return string */ public static function lPop($key) { return self::$handler->lPop($key); } }
namespace app\index\controller; use think\Controller; use My\RedisPackage; class Redis extends Controller { function redis() { $redis=new RedisPackage(); $redis::set('dede','我就笑笑'); echo $redis::get('dede'); } }
Recommended learning: "thinkphp video tutorial"
The above is the detailed content of How is Redis used and encapsulated in the ThinkPHP5 framework?. For more information, please follow other related articles on the PHP Chinese website!