redis = &load_cache('redis'); } public function index_get() { $this->load->library('MyCaptcha'); $myCaptcha = new MyCaptcha(); $re = $myCaptcha->create(5, ['width' => 16, 'height' => 20, 'pnum' => 50, 'base64' => 1]); $code_key = $this->generateSign(); $this->redis->save($code_key, $re['text'], 5 * 60); $data = ['base64' => $re['base64'], 'code_key' => $code_key]; $this->return_response($data); } function generateSign() { $sign = md5(mt_rand() . 'mycaptchamarket'); // 拼接上签名作为 Redis 的 key $key = 'code_' . $sign; if ($this->redis->exists($key)) { // 如果生成的 Sign 已存在,就进行递归,直到生成出一个不存在的。 return $this->generateSign(); } return $key; } }