thinkphp½Ó¿ÚÔõô»º´æ
˼Ë÷ÃÅ¿òphp¿ò¼ÜÒѳÉΪÖڶ࿪·¢ÕßµÄÊ×Ñ¡¿ò¼ÜÖ®Ò»£¬Æä²»µ«¾ßÓиßЧºÍÒ×ÓÃÐÔ£¬²¢ÇÒ»¹Ö§³ÖÖÖÖÖ»º´æÇý¶¯Æ÷£¬°üÀ¨Îļþ»º´æ¡¢memcached¡¢redisµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùÔÚthinkphpÓ¦ÓóÌÐòÖÐʹÓûº´æÀ´Ìá¸ßЧÂÊ¡£
ThinkPHP»º´æÏÈÈÝ
ThinkPHP¿ò¼ÜÌṩÁËÒ»¸öͳһµÄ»º´æ²Ù×÷½Ó¿Ú£¬¿ª·¢Õß¿ÉÒÔʹÓÃÖÖÖÖ»º´æÇý¶¯Æ÷À´ÖÎÀíºÍ¿ØÖÆÊý¾ÝµÄ»º´æ¡£»º´æ¿ÉÒÔ´æ´¢ÈκÎÀàÐ͵ÄÊý¾Ý£¬ÀýÈç×Ö·û´®¡¢Êý×é¡¢¹¤¾ßµÈ¡£ÓÉÓÚ»º´æÊǽ«Êý¾Ý´æ´¢ÔÚÄÚ´æÖУ¬Òò´Ë¿ÉÒÔÏÔÖø½µµÍÊý¾Ý¿âµÄ¸ºÔز¢Ìá¸ßÓ¦ÓóÌÐòµÄÏìÓ¦ËÙÂÊ¡£
Îļþ»º´æ
Îļþ»º´æÊÇThinkPHP¿ò¼ÜĬÈÏÌṩµÄÒ»ÖÖ»º´æÇý¶¯Æ÷¡£Ëü½«»º´æÊý¾Ý´æ´¢ÔÚÍâµØÎļþϵͳÖУ¬ÕâЩÎļþ¿ÉÒÔÔÚ»º´æÓâÆÚºó×Ô¶¯É¾³ý¡£ÒªÊ¹ÓÃÎļþ»º´æ£¬ÐèÒªÔÚÓ¦ÓóÌÐòÉèÖÃÎļþÖÐÉèÖûº´æÉèÖãº
'cache' => [ // ĬÈÏÇý¶¯Æ÷ΪÎļþ»º´æ 'default' => 'file', // Îļþ»º´æÉèÖà 'stores' => [ 'file' => [ 'type' => 'File', 'path' => APP_PATH . 'runtime/cache/', ], ], ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÖУ¬ÎÒÃǽ«Ä¬ÈÏ»º´æÇý¶¯Æ÷ÉèÖÃΪÎļþ»º´æ£¬²¢½«»º´æÊý¾ÝµÄ´æ´¢Â·¾¶ÉèÖÃΪ./runtime/cache/Ŀ¼¡£
ÒªÔÚÓ¦ÓóÌÐòÖÐʹÓûº´æ£¬ÎÒÃÇ¿ÉÒÔʹÓÃCacheÀࣺ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
use thinkacadeCache; // ÉèÖûº´æ Cache::set('key', 'value', 3600); // »ñÈ¡»º´æ $value = Cache::get('key'); // ɾ³ý»º´æ Cache::delete('key');
µÇ¼ºó¸´ÖÆ
Memcached»º´æ
MemcachedÊÇÒ»ÖÖ¸ßÐÔÄÜ¡¢ÂþÑÜʽÄڴ湤¾ß»º´æϵͳ£¬ÆÕ±éÓ¦ÓÃÓÚWebÓ¦ÓóÌÐòÖС£ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔÇáËɵØʹÓÃMemcached»º´æÇý¶¯Æ÷À´ÖÎÀí»º´æÊý¾Ý¡£ÒªÊ¹ÓÃMemcached»º´æ£¬ÇëÈ·±£ÒѾװÖò¢Æô¶¯ÁËMemcachedЧÀÍ¡£
'cache' => [ // ĬÈÏÇý¶¯Æ÷ΪMemcached»º´æ 'default' => 'memcached', // Memcached»º´æÉèÖà 'stores' => [ 'memcached' => [ 'type' => 'Memcached', 'host' => '127.0.0.1', 'port' => 11211, 'expire' => 3600, 'prefix' => '', ], ], ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÖУ¬ÎÒÃǽ«Ä¬ÈÏ»º´æÇý¶¯Æ÷ÉèÖÃΪMemcached£¬²¢½«»º´æÊý¾ÝµÄÓâÆÚʱ¼äÉèÖÃΪ3600Ãë¡£
ÈôÊÇÒªÔÚÓ¦ÓóÌÐòÖÐʹÓûº´æ£¬ÎÒÃÇ¿ÉÒÔʹÓÃCacheÀࣺ
use thinkacadeCache; // ÉèÖûº´æ Cache::store('memcached')->set('key', 'value', 3600); // »ñÈ¡»º´æ $value = Cache::store('memcached')->get('key'); // ɾ³ý»º´æ Cache::store('memcached')->delete('key');
µÇ¼ºó¸´ÖÆ
Redis»º´æ
RedisÊÇÆÕ±éÓÃÓÚ»º´æºÍÐÂÎÅÐÐÁеĿªÔ´ÄÚ´æÊý¾Ý½á¹¹´æ´¢¡£ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRedis»º´æÇý¶¯³ÌÐòÀ´ÖÎÀíºÍ¿ØÖÆ»º´æÊý¾Ý¡£
'cache' => [ // ĬÈÏÇý¶¯Æ÷ΪRedis»º´æ 'default' => 'redis', // Redis»º´æÉèÖà 'stores' => [ 'redis' => [ 'type' => 'redis', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 3600, 'persistent' => false, 'prefix' => '', ], ], ],
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÖУ¬ÎÒÃǽ«Ä¬ÈÏ»º´æÇý¶¯Æ÷ÉèÖÃΪRedis£¬²¢Ö¸¶¨ÁËRedisЧÀÍÆ÷µÄλÖúͶ˿ںš£
ÒªÔÚÓ¦ÓóÌÐòÖÐʹÓÃRedis»º´æ£¬ÎÒÃÇ¿ÉÒÔʹÓÃCacheÀࣺ
use thinkacadeCache; // ÉèÖûº´æ Cache::store('redis')->set('key', 'value', 3600); // »ñÈ¡»º´æ $value = Cache::store('redis')->get('key'); // ɾ³ý»º´æ Cache::store('redis')->delete('key');
µÇ¼ºó¸´ÖÆ
»º´æ±ê¼Ç
»º´æ±ê¼ÇÊÇÒ»ÖÖÔÚ»º´æÊý¾ÝÖи½¼Ó±ê¼ÇµÄÒªÁ죬ʹÎÒÃÇ¿ÉÒÔ¶Ô²î±ðµÄ»º´æÊý¾Ý¾ÙÐÐÇø·ÖºÍ·ÖÀà¡£ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓûº´æ±ê¼ÇÀ´¶Ô²î±ðµÄ»º´æÊý¾Ý¾ÙÐÐÖÎÀíºÍ¿ØÖÆ¡£
use thinkacadeCache; // ÉèÖûº´æ£¬Í¬Ê±Ìí¼Ó»º´æ±ê¼Ç Cache::store('file')->tag('tag1')->set('key1', 'value1', 3600); // ƾ֤±ê¼Ç»ñÈ¡»º´æ $data = Cache::store('file')->tag('tag1')->get('key1'); // ƾ֤±ê¼Çɾ³ý»º´æ Cache::store('file')->tag('tag1')->clear();
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËÎļþ»º´æÇý¶¯Æ÷£¬²¢Ê¹Óñê¼Çtag1¶Ô»º´æÊý¾Ý¾ÙÐÐÁË·ÖÀà¡£ÎÒÃÇ¿ÉÒÔʹÓÃCacheÀàµÄtag()ÒªÁìÀ´¶Ô»º´æ¾ÙÐбê¼Ç²Ù×÷¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÌÖÂÛÁËÔõÑùÔÚThinkPHPÓ¦ÓóÌÐòÖÐʹÓûº´æÀ´¼ÓËÙÏìÓ¦ËÙÂÊ¡£ÎÒÃÇÏÈÈÝÁ˼¸ÖÖ³£¼ûµÄ»º´æÇý¶¯Æ÷£¬°üÀ¨Îļþ»º´æ¡¢MemcachedºÍRedis¡£ÎÒÃÇ»¹Ìáµ½ÁËʹÓûº´æ±ê¼Ç¶Ô»º´æÊý¾Ý¾ÙÐзÖÀàºÍÖÎÀíµÄÖ÷ÒªÐÔ¡£Í¨¹ýʹÓûº´æ£¬ÎÒÃÇ¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺÍÏìÓ¦ËÙÂÊ£¬Í¬Ê±½µµÍÊý¾Ý¿â¸ºÔØ¡£
ÒÔÉϾÍÊÇthinkphp½Ó¿ÚÔõô»º´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡