ä½¿ç”¨ç¼“å˜æå‡æ€§èƒ½ ================================ Phalcon æä¾› :doc:`Phalcon\\Cache <cache>` 类,以便更快的访问ç»å¸¸ä½¿ç”¨çš„æ•°æ®æˆ–å·²ç»å¤„ç†è¿‡çš„æ•°æ®ã€‚ :doc:`Phalcon\\Cache <cache>` 使用Cè¯è¨€ç¼–写,实现更高的性能,并å‡å°‘系统开销。 这个类æä¾›å‰ç«¯å’ŒåŽç«¯ä¸¤ä¸ªç»„件,å‰ç«¯ç»„ä»¶ä½œä¸ºè¾“å…¥æºæˆ–接å£ï¼ŒåŽç«¯æä¾›å˜å‚¨é€‰é¡¹ã€‚ 什么时候使用缓å˜? ------------------------ 虽然这个组件是éžå¸¸é«˜æ•ˆå¿«é€Ÿçš„,但如果使用ä¸å½“,也有å¯èƒ½å¯¼è‡´æ€§èƒ½é—®é¢˜ï¼Œä»Žè€Œå¾—ä¸å¿å¤±ã€‚ æˆ‘ä»¬å»ºè®®ä½ åœ¨ä½¿ç”¨ç¼“å˜ä¹‹å‰æ£€æŸ¥ä»¥ä¸‹æƒ…况: * è¿›è¡Œå¤æ‚的数æ®è®¡ç®—ï¼Œæ¯æ¬¡è¿”回相åŒçš„结果(ä¸ç»å¸¸ä¿®æ”¹) * 使用了大é‡çš„助手工具,并且生æˆçš„è¾“å‡ºå‡ ä¹Žé—®é¢˜ä¸€æ ·çš„ * 䏿–的访问数æ®åº“,且这些数æ®å¾ˆå°‘æ”¹å˜ .. highlights:: *注æ„* å³ä¾¿å·²ç»ä½¿ç”¨äº†ç¼“å˜ï¼Œè¿‡ä¸€æ®µæ—¶é—´åŽï¼Œä½ åº”è¯¥æ£€æŸ¥ä½ çš„ç¼“å˜çš„命ä¸çŽ‡ï¼Œå°¤å…¶ä½ ä½¿ç”¨çš„æ˜¯Memcache或者Apc时。使用åŽç«¯æä¾›çš„相关工具,很容易完æˆå‘½ä¸çŽ‡æ£€æŸ¥ã€‚ 缓å˜è¡Œä¸º ---------------- 缓å˜çš„æ‰§è¡Œåˆ†ä¸ºä¸¤ä¸ªéƒ¨åˆ†ï¼š * **Frontend**: 这一部分主è¦è´Ÿè´£æ£€æŸ¥KEY是å¦è¿‡æœŸä»¥åŠåœ¨å˜å‚¨åˆ°backend之å‰/从backendå–æ•°æ®ä¹‹åŽæ‰§è¡Œé¢å¤–的数æ®è½¬æ¢ * **Backend**: 这部分主è¦è´Ÿè´£æ²Ÿé€šï¼Œå¹¶æ ¹æ®å‰ç«¯çš„需求读写数æ®ã€‚ 片æ–ç¼“å˜ ------------------------ 片æ–ç¼“å˜æ˜¯ç¼“å˜HTML或者TEXT文本的一部分,然åŽåŽŸæ ·è¿”å›žã€‚è¾“å‡ºè‡ªåŠ¨æ•获æ¥è‡ªob_* 函数或PHP输出,以便它å¯ä»¥ä¿å˜åˆ°ç¼“å˜ä¸ã€‚ 下é¢çš„例忼”示了这ç§ç”¨æ³•。 它接收PHP生æˆçš„输出,并将其å˜å‚¨åˆ°ä¸€ä¸ªæ–‡ä»¶ä¸ï¼Œæ–‡ä»¶çš„内容172800ç§’(2天)更新一次。 è¿™ç§ç¼“å˜æœºåˆ¶çš„实现,使我们既能获得性能,而åˆä¸æ‰§è¡ŒPhalcon\\Tag::linkTo的调用。 .. code-block:: php <?php //Create an Output frontend. Cache the files for 2 days $frontCache = new Phalcon\Cache\Frontend\Output(array( "lifetime" => 172800 )); // Create the component that will cache from the "Output" to a "File" backend // Set the cache file directory - it's important to keep the "/" at the end of // the value for the folder $cache = new Phalcon\Cache\Backend\File($frontCache, array( "cacheDir" => "../app/cache/" )); // Get/Set the cache file to ../app/cache/my-cache.html $content = $cache->start("my-cache.html"); // If $content is null then the content will be generated for the cache if ($content === null) { //Print date and time echo date("r"); //Generate a link to the sign-up action echo Phalcon\Tag::linkTo( array( "user/signup", "Sign Up", "class" => "signup-button" ) ); // Store the output into the cache file $cache->save(); } else { // Echo the cached output echo $content; } *NOTE* In the example above, our code remains the same, echoing output to the user as it has been doing before. 缓å˜ç»„件逿˜Žçš„æ•èŽ·è¯¥è¾“å‡ºï¼Œå¹¶å°†å…¶å˜å‚¨åˆ°ç¼“å˜æ–‡ä»¶ä¸(å‰ææ˜¯å·²ç»ç”Ÿæˆcache对象),或将其之å‰çš„缓å˜å‘é€ç»™ç”¨æˆ·ï¼Œä»Žè€Œé¿å…代价昂贵的开销。 Caching Arbitrary Data ---------------------- ç¼“å˜æ˜¯åº”用程åºé‡è¦çš„组æˆéƒ¨åˆ†ã€‚缓å˜å¯ä»¥å‡å°‘æ•°æ®åº“负载,é‡å¤ä½¿ç”¨å¸¸ç”¨çš„æ•°æ®ï¼ˆä½†ä¸æ›´æ–°ï¼‰ï¼Œä»Žè€ŒåŠ å¿«äº†æ‚¨çš„åº”ç”¨ç¨‹åºã€‚ File Backend Example ^^^^^^^^^^^^^^^^^^^^ 缓å˜é€‚é…器之一'File',æ¤é€‚é…å™¨çš„å±žæ€§åªæœ‰ä¸€ä¸ªï¼Œå®ƒç”¨æ¥æŒ‡å®šç¼“å˜æ–‡ä»¶çš„å˜å‚¨ä½ç½®ã€‚使用 cacheDir选项进行控制,且 *å¿…é¡»* 以'/'结尾。 .. code-block:: php <?php // Cache the files for 2 days using a Data frontend $frontCache = new Phalcon\Cache\Frontend\Data(array( "lifetime" => 172800 )); // Create the component that will cache "Data" to a "File" backend // Set the cache file directory - important to keep the "/" at the end of // of the value for the folder $cache = new Phalcon\Cache\Backend\File($frontCache, array( "cacheDir" => "../app/cache/" )); // Try to get cached records $cacheKey = 'robots_order_id.cache'; $robots = $cache->get($cacheKey); if ($robots === null) { // $robots is null because of cache expiration or data does not exist // Make the database call and populate the variable $robots = Robots::find(array("order" => "id")); // Store it in the cache $cache->save($cacheKey, $robots); } // Use $robots :) foreach ($robots as $robot) { echo $robot->name, "\n"; } Memcached Backend Example ^^^^^^^^^^^^^^^^^^^^^^^^^ 上é¢çš„例åç¨å¾®æ”¹å˜ä¸€ä¸‹(ä¸»è¦æ˜¯é…置方é¢),就å¯ä»¥ä½¿ç”¨Memcacheåšä¸ºåŽç«¯å˜å‚¨å™¨äº†ã€‚ .. code-block:: php <?php //Cache data for one hour $frontCache = new Phalcon\Cache\Frontend\Data(array( "lifetime" => 3600 )); // Create the component that will cache "Data" to a "Memcached" backend // Memcached connection settings $cache = new Phalcon\Cache\Backend\Memcached($frontCache, array( "host" => "localhost", "port" => "11211" )); // Try to get cached records $cacheKey = 'robots_order_id.cache'; $robots = $cache->get($cacheKey); if ($robots === null) { // $robots is null because of cache expiration or data does not exist // Make the database call and populate the variable $robots = Robots::find(array("order" => "id")); // Store it in the cache $cache->save($cacheKey, $robots); } // Use $robots :) foreach ($robots as $robot) { echo $robot->name, "\n"; } Querying the cache ------------------ 缓å˜å”¯ä¸€æ ‡è¯†ç¬¦å…ƒç´ 为KEY,在åŽç«¯æ–‡ä»¶ä¸ï¼ŒKEY值峿˜¯å®žé™…文件å。从缓å˜ä¸æ£€ç´¢æ•°æ®ï¼Œæˆ‘们åªéœ€è¦é€šè¿‡KEYæ¥è°ƒç”¨å³å¯ã€‚如果该KEYä¸å˜åœ¨ï¼Œget方法将返回null。 .. code-block:: php <?php // Retrieve products by key "myProducts" $products = $cache->get("myProducts"); å¦‚æžœä½ æƒ³çŸ¥é“缓å˜ä¸éƒ½æœ‰å“ªäº›KEYï¼Œä½ å¯ä»¥è°ƒç”¨queryKeys方法: .. code-block:: php <?php // Query all keys used in the cache $keys = $cache->queryKeys(); foreach ($keys as $key) { $data = $cache->get($key); echo "Key=", $key, " Data=", $data; } //Query keys in the cache that begins with "my-prefix" $keys = $cache->queryKeys("my-prefix"); åˆ é™¤ç¼“å˜æ•°æ® ---------------------------- å¾ˆå¤šæ—¶å€™ï¼Œä½ éœ€è¦å¼ºè¡Œåˆ é™¤æ— æ•ˆçš„ç¼“å˜æ¡ç›®(ç”±äºŽæ•°æ®æ›´æ–°çš„åŽŸå› )ï¼Œå”¯ä¸€çš„è¦æ±‚å°±æ˜¯ï¼Œä½ å¾—çŸ¥é“该缓å˜çš„KEY。 .. code-block:: php <?php // Delete an item with a specific key $cache->queryKeys("someKey"); // Delete all items from the cache $keys = $cache->queryKeys(); foreach ($keys as $key) { $cache->delete($key); } æ£€æµ‹ç¼“å˜æ˜¯å¦å˜åœ¨ ------------------------ 通过给定的KEY值,å¯ä»¥æ£€æµ‹ç¼“å˜æ˜¯å¦å˜åœ¨ã€‚ .. code-block:: php <?php if ($cache->exists("someKey")) { echo $cache->get("someKey"); } else { echo "Cache does not exists!"; } å‰ç«¯é€‚é…器 ----------------- The available frontend adapters that are used as interfaces or input sources to the cache are: +---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+ | Adapter | Description | Example | +=========+================================================================================================================================+================================================================================+ | Output | Read input data from standard PHP output | :doc:`Phalcon\\Cache\\Frontend\\Output <../api/Phalcon_Cache_Frontend_Output>` | +---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+ | Data | It's used to cache any kind of PHP data (big arrays, objects, text, etc). The data is serialized before stored in the backend. | :doc:`Phalcon\\Cache\\Frontend\\Data <../api/Phalcon_Cache_Frontend_Data>` | +---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+ | Base64 | It's used to cache binary data. The data is serialized using base64_encode before be stored in the backend. | :doc:`Phalcon\\Cache\\Frontend\\Base64 <../api/Phalcon_Cache_Frontend_Base64>` | +---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+ | None | It's used to cache any kind of PHP data without serializing them. | :doc:`Phalcon\\Cache\\Frontend\\Data <../api/Phalcon_Cache_Frontend_None>` | +---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+ 实现自定义的å‰ç«¯é€‚é…器 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The :doc:`Phalcon\\Cache\\FrontendInterface <../api/Phalcon_Cache_FrontendInterface>` interface must be implemented in order to create your own frontend adapters or extend the existing ones. åŽç«¯é€‚é…器 ---------------- å¯ç”¨çš„åŽç«¯å˜å‚¨å™¨åˆ—表: +-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+ | Adapter | Description | Info | Required Extensions | Example | +===========+================================================+============+=====================+===================================================================================+ | File | Stores data to local plain files | | | :doc:`Phalcon\\Cache\\Backend\\File <../api/Phalcon_Cache_Backend_File>` | +-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+ | Memcached | Stores data to a memcached server | Memcached_ | memcache_ | :doc:`Phalcon\\Cache\\Backend\\Memcache <../api/Phalcon_Cache_Backend_Memcache>` | +-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+ | APC | Stores data to the Alternative PHP Cache (APC) | APC_ | `APC extension`_ | :doc:`Phalcon\\Cache\\Backend\\Apc <../api/Phalcon_Cache_Backend_Apc>` | +-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+ | Mongo | Stores data to Mongo Database | MongoDb_ | `Mongo`_ | :doc:`Phalcon\\Cache\\Backend\\Mongo <../api/Phalcon_Cache_Backend_Mongo>` | +-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+ 实现自定义åŽç«¯é€‚é…器 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The :doc:`Phalcon\\Cache\\BackendInterface <../api/Phalcon_Cache_BackendInterface>` interface must be implemented in order to create your own backend adapters or extend the existing ones. 文件缓å˜é€‰é¡¹ ^^^^^^^^^^^^^^^^^^^^ This backend will store cached content into files in the local server. The available options for this backend are: +----------+-----------------------------------------------------------+ | Option | Description | +==========+===========================================================+ | cacheDir | A writable directory on which cached files will be placed | +----------+-----------------------------------------------------------+ Memcached缓å˜é€‰é¡¹ ^^^^^^^^^^^^^^^^^^^^^^^^^ This backend will store cached content on a memcached server. The available options for this backend are: +------------+---------------------------------------------+ | Option | Description | +============+=============================================+ | host | memcached host | +------------+---------------------------------------------+ | port | memcached port | +------------+---------------------------------------------+ | persistent | create a persitent connection to memcached? | +------------+---------------------------------------------+ APC缓å˜é€‰é¡¹ ^^^^^^^^^^^^^^^^^^^ This backend will store cached content on Alternative PHP Cache (APC_). This cache backend does not require any additional configuration options. Mongo缓å˜é€‰é¡¹ ^^^^^^^^^^^^^^^^^^^^^^^^^ This backend will store cached content on a MongoDB server. The available options for this backend are: +------------+---------------------------------------------+ | Option | Description | +============+=============================================+ | server | A MongoDB connection string | +------------+---------------------------------------------+ | db | Mongo database name | +------------+---------------------------------------------+ | collection | Mongo collection in the database | +------------+---------------------------------------------+ .. _Memcached: http://php.net/manual/en/book.apc.php .. _memcache: http://pecl.php.net/package/memcache .. _APC: http://php.net/manual/en/book.apc.php .. _APC extension: http://pecl.php.net/package/APC .. _MongoDb: http://mongodb.org/ .. _Mongo: http://pecl.php.net/package/mongo