PHP—Redis

  在PHP源码中redis是可以直接new redis的,但是在thinkPHP5中直接new的话会直接报致命的错误,错误的信息说没有引用名空间。以下是简单的调用redis的代码

  致命错误: Class 'app\home\controller\Redis' not found

thinkPHP5.0

use think\cache\driver\Redis;

class Index extends LoginController
{

    public function index(){
    $config = [
      'host' => '127.0.0.1',
      'port' => 6379,
      'password' => '',
      'select' => 0,
      'timeout' => 0,
      'expire' => 0,
      'persistent' => false,
      'prefix' => '',
    ];

    $redis = new Redis($config);//调用thinkPHP->redis

    $redis->set("name1","aaaaa");//添加str类型的数据
    
    die;

PHP

$redis = new redis();
$redis -> connect('127.0.0.1',6379);
$redis -> flushAll();

// This first case: hash 值不存在 $redis -> hSet('myhash','favorite_fruit','cherry'); var_dump($redis -> hGet('myhash','favorite_fruit')); // string 'cherry' // This second case: hash 值存在, 替换 if($redis -> exists('myhash')){   $redis -> hSet('myhash','favorite_fruit','pineapple');   var_dump($redis -> hGet('myhash','favorite_fruit')); // string 'pineapple' }

经验很浅提供仅提供参考!

猜你喜欢

转载自www.cnblogs.com/TuWenHao/p/9158851.html
今日推荐