在PHP源碼中redis是能夠直接new redis的,可是在thinkPHP5中直接new的話會直接報致命的錯誤,錯誤的信息說沒有引用名空間。如下是簡單的調用redis的代碼html
thinkPHP5.0redis
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;
PHPapp
$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' }
更多PHP調用Redis方法另一位博主寫了 連接ui
經驗很淺提供僅提供參考!spa