Some time ago, the company needed to use redis to implement the cache function. Therefore, I wrote this article to share the installation and usage skills of redis.
First, download the redisserver and execute redis-server.exe. This process must be enabled all the time. We can also download a redis management tool: phpRedisAdmin
To facilitate the management of redis setting values, you need to pay attention to some specifications when setting their key values. For example, we have three systems running on the same server at the same time, and all of them use redis for caching, if one of the key values in the system is repeated, it will be overwritten by the later settings.
The following is a redis predis usage encapsulation class written by myself.
<? Php/*** implement redis client predis cache function * @ author bilehai@qq.com **/class Redis {private $ redis; // predis get object private $ statue; // predis status private $ time = 1000; // default expiration time public $ redis_start = FALSE; // whether to open an account Cache/*** initialize **/public function _ construct () {}/*** enable cache * @ param string **/public function redis_start ($ param = '') {$ this-> redis_start = TRUE; $ param = empty ($ param )? '': $ Param. ':'; if (file_exists ('predis/lib/predis/Autoloader. php ') {require_once ('predis/lib/predis/Autoloader. php '); Predis \ Autoloader: register (); $ this-> redis = new Predis \ Client ('', array ('prefix' => 'www .example.com :'. $ param) ;}else {return FALSE ;}} /*** common cache data ** @ param key input value parameter * @ param value input cache value parameter **/public function set ($ key, $ value) {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ key) | empty ($ value) {return FALSE ;} return $ this-> redis-> set ($ key, $ value );} /*** set cache data ** @ param key input value parameter * @ param value input cache value parameter * @ param time set expiration time * @ return string **/public function setex ($ key, $ value, $ time = '') {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ time) {$ time = $ this-> time;} if (empty ($ key) | empty ($ value) {return FALSE;} return $ this-> redis-> setex ($ key, $ time, $ value );} /*** get cache data ** @ param key cache key parameter * @ return string **/public function get ($ key) {if ($ this-> redis_start! = TRUE) {return FALSE;} if (empty ($ key) {return FALSE;} if ($ this-> redis-> exists ($ key )) {return $ this-> redis-> get ($ key);} else {return FALSE ;}}$ username =$ _ GET ['username']; $ redis = new Redis (); $ redis-> redis_start (); $ get_redis = $ redis-> get ('username'); if ($ get_redis = FALSE) {$ redis-> setex ('username', $ username);} var_dump ($ get_redis);?>