php+redis封装一个文件出来
时间: 2023-05-16 17:06:37 浏览: 85
可以使用phpredis扩展来封装一个Redis类,具体实现可以参考以下代码:
```php
<?php
class Redis {
private $redis;
public function __construct($host, $port) {
$this->redis = new Redis();
$this->redis->connect($host, $port);
}
public function set($key, $value) {
return $this->redis->set($key, $value);
}
public function get($key) {
return $this->redis->get($key);
}
public function del($key) {
return $this->redis->del($key);
}
public function __call($name, $arguments) {
return call_user_func_array([$this->redis, $name], $arguments);
}
}
?>
```
这个类封装了Redis的常用操作,可以通过实例化该类来使用Redis。例如:
```php
$redis = new Redis('127.0.0.1', 6379);
$redis->set('name', 'Tom');
echo $redis->get('name');
```
输出结果为:
```
Tom
```
希望对你有所帮助!