load->driver('cache'); // 加载缓存驱动 } public function login() { $user_id =123; // ... 验证账号密码成功,得到 $user_id // $data = [ // 'user_id' => $user_id, // 'created_at' => time() // ]; // $this->cache->save("lvhaoceshi1", $data, 100); echo "
";
print_r($this->cache->get("lvhaoceshi1"));
die;
//$this->load->driver('cache'); // 确保缓存可用
// 生成唯一 token(可以用 JWT 或随机字符串)
$token = bin2hex(random_bytes(32));
$cache_key = 'token:' . md5($token);
// 写入缓存,有效期7200秒(2小时)
$this->cache->save($cache_key, $user_id, 7200);
// 返回 token 给客户端
$this->output
->set_content_type('application/json')
->set_output(json_encode([
'status' => true,
'token' => $token,
'expire' => 7200
]));
}
}