thinkphp s »º´æɥʧÔõô°ì
ÔÚʹÓÃthinkphp¿ò¼Ü¾ÙÐпª·¢Ê±£¬ÎÒÃdz£³£»áʹÓûº´æÀ´Ìá¸ßÍøÕ¾µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£È»¶ø£¬ÓÐʱ¼ä»á·¢Ã÷»º´æɥʧµÄÎÊÌ⣬Õâ»áÑÏÖØÓ°ÏìÍøÕ¾µÄÔËÐÐЧÂʺÍÓû§µÄÌåÑé¡£
±¾ÎĽ«Ì½ÌÖThinkPHP¿ò¼ÜÖлº´æɥʧµÄÔµ¹ÊÔÓÉÏ¢Õù¾ö²½·¥¡£
Ôµ¹ÊÔÓÉÒ»£º»º´æʱ¼äÓâÆÚ
ThinkPHP¿ò¼ÜÖÐ×Ô´øµÄ»º´æϵͳ֧³Ö¶àÖÖÀàÐ͵Ļº´æ£¬ÈçÎļþ»º´æ¡¢Redis»º´æµÈ¡£ÔÚÉèÖûº´æʱ£¬ÎÒÃÇ¿ÉÒÔÉèÖûº´æµÄÓÐÓÃʱ¼ä£¬ÔÚÕâ¶Îʱ¼äÄÚ»º´æ»áÉúЧ£¬Áè¼ÝÁËÕâ¶Îʱ¼ä»º´æ¾Í»áÓâÆÚ¡£
µ±»º´æʱ¼äÓâÆÚºó£¬ÏÂÒ»´ÎŲÓøûº´æʱ£¬¾Í»á´ÓÊý¾Ý¿â»òÆäËüÊý¾ÝÔ´ÖÐÖØлñÈ¡Êý¾Ý²¢ÖØд洢µ½»º´æÖУ¬ÕâÑù¾Í»áµ¼ÖÂ֮ǰ´æ´¢ÔÚ»º´æÖеÄÊý¾Ýɥʧ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
½â¾ö²½·¥£ºµ÷½â»º´æʱ¼ä
ÎÒÃÇ¿ÉÒÔͨ¹ý¼ì²é»º´æµÄÓÐÓÃʱ¼äÀ´Åжϻº´æÊÇ·ñÒÑÓâÆÚ£¬µ±»º´æÓâÆÚʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÖØÐÂÉèÖûº´æʱ¼äÀ´×èÖ¹»º´æµÄɥʧ£¬ÀýÈ磺
$key = 'user_info'; $user_info = cache($key); if (!$user_info) { //»º´æ²»±£´æ£¬´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý $user_info = Db::name('user')->where('id', 1)->find(); //ÉèÖûº´æ²¢ÉèÖÃÓÐÓÃʱ¼äΪ10·ÖÖÓ cache($key, $user_info, 600); } return $user_info;
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬µ±»º´æ²»±£´æʱ£¬ÎÒÃÇ´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý²¢½«Æä´æ´¢µ½»º´æÖУ¬²¢ÉèÖÃÓÐÓÃʱ¼äΪ10·ÖÖÓ£¬ÈôÊÇ»º´æÒѱ£´æµ«ÓâÆÚÁË£¬ÄÇôÔÚŲÓÃcacheº¯Êýʱ£¬ÏµÍ³»á×Ô¶¯½«Ð»ñÈ¡µÄÊý¾Ý´æ´¢µ½»º´æÖв¢ÖØÖÃÓÐÓÃʱ¼ä£¬ÕâÑù¾Í¿ÉÒÔ×èÖ¹»º´æµÄɥʧ¡£
Ôµ¹ÊÔÓɶþ£º»º´æ±»É¨³ý
³ýÁË»º´æʱ¼äÓâÆÚÍ⣬ÎÒÃÇÓÐʱ¿ÉÄÜ»áÊÖ¶¯É¨³ý»º´æ»òÕßͨ¹ýϵͳ¾ç±¾×Ô¶¯É¨³ý»º´æ£¬ÕâÑù¾Í»áµ¼ÖÂ֮ǰ´æ´¢ÔÚ»º´æÖеÄÊý¾Ý±»É¨³ý£¬²¢ÇÒÎÞ·¨ÔÙ»ñÈ¡¡£
½â¾ö²½·¥£º×èÖ¹Îóɾ»º´æ
ÎÒÃÇ¿ÉÒÔͨ¹ý¶Ô»º´æ¾ÙÐзÖÀà»òÕ߸øÿ¸ö»º´æÉèÖÃ×ÔÁ¦µÄ¼üÖµÀ´×èÖ¹Îóɾ»º´æ£¬ÀýÈ磺
//·ÖÀ໺´æ $key = 'user_info_' . $user_id; $user_info = cache($key); //×ÔÁ¦¼üÖµ $key = 'user_info'; $user_info = cache($key . '_' . $user_id);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇ̫ͨ¹ýÀ໺´æ»òÉèÖÃ×ÔÁ¦¼üÖµÀ´×èÖ¹Îóɾ»º´æ£¬ÕâÑù×ÝȻij¸ö»º´æ±»ÎóɾÁË£¬ÆäËü»º´æÒ²²»»áÊܵ½Ó°Ïì¡£
Ôµ¹ÊÔÓÉÈý£º»º´æ´æ´¢¿Õ¼äȱ·¦
ÔÚʹÓûº´æʱ£¬ÎÒÃÇÐèҪ˼Á¿»º´æÕ¼ÓõĴ洢¿Õ¼ä£¬ÈôÊÇ´æ´¢¿Õ¼äȱ·¦£¬¾Í»áµ¼Ö»º´æɥʧ¡£
½â¾ö²½·¥£ºÔöÌí»º´æ´æ´¢¿Õ¼ä
ÎÒÃÇ¿ÉÒÔͨ¹ýÔöÌí»º´æµÄ´æ´¢¿Õ¼äÀ´×èÖ¹»º´æɥʧ£¬ÀýÈ磺
//ʹÓÃRedis»º´æ $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); $redis->setOption(Redis::OPT_PREFIX, 'thinkphp_'); //ÉèÖÃRedis×î´óÄÚ´æΪ512MB $redis->config('SET', 'maxmemory', '512mb'); //»ñÈ¡Ä¿½ñRedisʹÓõÄ×ÜÄÚ´æºÍÊ£ÓàÄÚ´æ $used_memory = $redis->info('memory')['used_memory']; $free_memory = $redis->info('memory')['maxmemory'] - $used_memory; //µ÷½â»º´æ´æ´¢¿Õ¼ä if ($free_memory config('SET', 'maxmemory', '1024mb'); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃRedis»º´æ£¬²¢ÉèÖÃÆä×î´óÄÚ´æΪ512MB£¬ÔÚ»ñÈ¡Ä¿½ñRedisʹÓõÄ×ÜÄÚ´æºÍÊ£ÓàÄÚ´æºó£¬ÈôÊÇÊ£ÓàÄÚ´æСÓÚ100MB£¬¾Í½«×î´óÄÚ´æµ÷½âΪ1GB£¬ÕâÑù¾Í¿ÉÒÔ×èÖ¹ÓÉÓÚ´æ´¢¿Õ¼äȱ·¦µ¼ÖµĻº´æɥʧ¡£
×ÛÉÏËùÊö£¬»º´æɥʧÊÇÒ»¸ö³£¼ûµÄÎÊÌ⣬µ«Í¨¹ýºÏÀíÉèÖûº´æʱ¼ä¡¢·ÖÀ໺´æºÍÔöÌí»º´æ´æ´¢¿Õ¼äµÈ²½·¥£¬ÎÒÃÇ¿ÉÒÔÓÐÓõØ×èÖ¹»º´æɥʧ£¬Ìá¸ßÍøÕ¾µÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£
ÒÔÉϾÍÊÇthinkphp s »º´æɥʧÔõô°ìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡