(1) Call the Redis class that comes with the frame directly:
Path: \thinkphp\library\think\cache\driver\redis.class.php.
Public functionTest () {//Create a Redis object $redis=New\redis (); //Connect to a local Redis service $redis->connect (' 127.0.0.1 ', 6379); //password verification, if there is no can not set $redis->auth (' 123456 '); //See if the service is running Echo"Server is running:".$redis-Ping (); Echo ' <br/> '; //setting up the cache $redis->set (' username ', ' Zhang San ', 3600); //Get Cache $user _name=$redis->get (' username '); Var_dump($user _name); }
Operation Result:
Server is running: +pong
String (9) "Zhang San"
(2) using the S method:
To add a configuration to a configuration file
' Data_cache_type ' = ' Redis ',
' Redis_host ' = ' 127.0.0.1 ',
' Redis_port ' = 6379,
One, Redis does not set the password case:
Public function Set_info () { S (' study ', ' 123 '); } Public function Get_info () { echo C (' Data_cache_type '); Echo ' <br/> '; $a = S (' study '); Echo $a ; }
Visit Set_info First, then visit Get_info and return the results:
Redis
123
Second, the Redis set the password case:
Using the S method directly, the result is error:
Noauth Authentication required.
And then add the settings
' Redis_auth ' = 123456,
Find the Redis class, find that no password is set, and add code in Redis.class.php's __construct method:
Then test the S method again:
Public functionSet_info () {$a= S (' study ', ' 1223 '); Var_dump($a);//true } Public functionGet_info () {EchoC (' Data_cache_type ');//Redis Echo' <br/> '; $a= S (' study '); Echo $a;//1223}
thinkphp 3.2 using Redis