This article mainly introduces a simple example of using apsaradb for redis in PHP. It mainly shows you how to use apsaradb for redis in php.
This article mainly introduces a simple example of using apsaradb for redis in PHP. It mainly shows you how to use apsaradb for redis in php.
The example is very simple. It is explained in the annotations, so there is not much nonsense here.
The Code is as follows:
<? Php
/* Obtain the database name from the platform */
$ Dbname = "";
/* Obtain host, port, user, pwd from environment variables */
$ Host = '';
$ Port = '';
$ User = '';
$ Pwd = '';
Try {
/* After the connection is established, perform auth verification before performing the set operation */
$ Redis = new Redis ();
$ Ret = $ redis-> connect ($ host, $ port );
If ($ ret = false ){
Die ($ redis-> getLastError ());
}
$ Ret = $ redis-> auth ($ user. "-". $ pwd. "-". $ dbname );
If ($ ret = false ){
Die ($ redis-> getLastError ());
}
/* Next, you can perform operations on the database. For detailed operation methods, see the official phpredis Documentation */
$ Redis-> flushdb ();
$ Ret = $ redis-> set ("key", "value ");
If ($ ret = false ){
Die ($ redis-> getLastError ());
} Else {
Echo "OK". $ redis-> get ("key ");
}
} Catch (RedisException $ e ){
Die ("Uncaught exception". $ e-> getMessage ());
}
?>
The above is all the content of the code described in this article. I hope you will like it.