Skip to content

Commit

Permalink
第一版
Browse files Browse the repository at this point in the history
  • Loading branch information
netstu committed Dec 27, 2012
1 parent 031616e commit 80d61cc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 111 deletions.
88 changes: 39 additions & 49 deletions zh/reference/cache.rst
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
Improving Performance with Cache
使用缓存提升性能
================================
Phalcon provides the :doc:`Phalcon\\Cache <cache>` class allowing faster access to frequently used or already processed data.
:doc:`Phalcon\\Cache <cache>` is written in C, achieving higher performance and reducing the overhead when getting items from the backends.
This class uses an internal structure of frontend and backend components. Front-end components act as input sources or
interfaces, while backend components offer storage options to the class.
Phalcon 提供 :doc:`Phalcon\\Cache <cache>` 类,以便更快的访问经常使用的数据或已经处理过的数据。
:doc:`Phalcon\\Cache <cache>` 使用C语言编写,实现更高的性能,并减少系统开销。
这个类提供前端和后端两个组件,前端组件作为输入源或接口,后端提供存储选项。

When to implement cache?
什么时候使用缓存?
------------------------
Although this component is very fast, implementing it in cases that is not needed could lead to loss of performance than gain.
We recommend you check this cases before use cache:
虽然这个组件是非常高效快速的,但如果使用不当,也有可能导致性能问题,从而得不偿失。
我们建议你在使用缓存之前检查以下情况:

* You are making complex calculations that every time return the same result (changing infrequently)
* You are using a lot of helpers and the output generated is almost always the same
* You are accessing database data constantly and these data rarely change
* 进行复杂的数据计算,每次返回相同的结果(不经常修改)
* 使用了大量的助手工具,并且生成的输出几乎问题一样的
* 不断的访问数据库,且这些数据很少改变

.. highlights::

*NOTE* Even after implementing the cache, you should check the hit ratio of your cache over a period of time. This can easily
be done, especially in the case of Memcache or Apc, with the relevant tools that the backends provide.
*注意* 即便已经使用了缓存,过一段时间后,你应该检查你的缓存的命中率,尤其你使用的是Memcache或者Apc时。使用后端提供的相关工具,很容易完成命中率检查。

Caching Behavior
缓存行为
----------------
The caching process is divided into 2 parts:
缓存的执行分为两个部分:

* **Frontend**: This part is responsible for checking if a key has expired and perform additional transformations to the data before storing and after retrieving them from the backend-
* **Backend**: This part is responsible for communicating, writing/reading the data required by the frontend.
* **Frontend**: 这一部分主要负责检查KEY是否过期以及在存储到backend之前/从backend取数据之后执行额外的数据转换
* **Backend**: 这部分主要负责沟通,并根据前端的需求读写数据。

Caching Output Fragments
片断缓存
------------------------
An output fragment is a piece of HTML or text that is cached as is and returned as is. The output is automatically captured
from the ob_* functions or the PHP output so that it can be saved in the cache. The following example demonstrates such usage.
It receives the output generated by PHP and stores it into a file. The contents of the file are refreshed every 172800 seconds (2 days).
片断缓存是缓存HTML或者TEXT文本的一部分,然后原样返回。输出自动捕获来自ob_* 函数或PHP输出,以便它可以保存到缓存中。 下面的例子演示了这种用法。
它接收PHP生成的输出,并将其存储到一个文件中,文件的内容172800秒(2天)更新一次。

The implementation of this caching mechanism allows us to gain performance by not executing the helper Phalcon\\Tag::linkTo
call whenever this piece of code is called.
这种缓存机制的实现,使我们既能获得性能,而又不执行Phalcon\\Tag::linkTo的调用。

.. code-block:: php
Expand Down Expand Up @@ -78,19 +74,16 @@ call whenever this piece of code is called.
echo $content;
}
*NOTE* In the example above, our code remains the same, echoing output to the user as it has been doing before. Our cache component
transparently captures that output and stores it in the cache file (when the cache is generated) or it sends it back to the user
pre-compiled from a previous call, thus avoiding expensive operations.
*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
----------------------
Caching just data is equally important for your application. Caching can reduce database load by reusing commonly used (but not updated) data,
thus speeding up your application.
缓存是应用程序重要的组成部分。缓存可以减少数据库负载,重复使用常用的数据(但不更新),从而加快了您的应用程序。

File Backend Example
^^^^^^^^^^^^^^^^^^^^
One of the caching adapters is 'File'. The only key area for this adapter is the location of where the cache files will be stored.
This is controlled by the cacheDir option which *must* have a backslash at the end of it.
缓存适配器之一'File',此适配器的属性只有一个,它用来指定缓存文件的存储位置。使用 cacheDir选项进行控制,且 *必须* 以'/'结尾。

.. code-block:: php
Expand Down Expand Up @@ -128,7 +121,7 @@ This is controlled by the cacheDir option which *must* have a backslash at the e
Memcached Backend Example
^^^^^^^^^^^^^^^^^^^^^^^^^
The above example changes slightly (especially in terms of configuration) when we are using a Memcached backend.
上面的例子稍微改变一下(主要是配置方面),就可以使用Memcache做为后端存储器了。

.. code-block:: php
Expand Down Expand Up @@ -166,9 +159,7 @@ The above example changes slightly (especially in terms of configuration) when w
Querying the cache
------------------
The elements added to the cache are uniquely identified by a key. In the case of the File backend, the key is the
actual filename. To retrieve data from the cache, we just have to call it using the unique key. If the key does
not exist, the get method will return null.
缓存唯一标识符元素为KEY,在后端文件中,KEY值即是实际文件名。从缓存中检索数据,我们只需要通过KEY来调用即可。如果该KEY不存在,get方法将返回null。

.. code-block:: php
Expand All @@ -177,7 +168,7 @@ not exist, the get method will return null.
// Retrieve products by key "myProducts"
$products = $cache->get("myProducts");
If you want to know which keys are stored in the cache you could call the queryKeys method:
如果你想知道缓存中都有哪些KEY,你可以调用queryKeys方法:

.. code-block:: php
Expand All @@ -194,10 +185,9 @@ If you want to know which keys are stored in the cache you could call the queryK
$keys = $cache->queryKeys("my-prefix");
Deleting data from the cache
删除缓存数据
----------------------------
There are times where you will need to forcibly invalidate a cache entry (due to an update in the cached data).
The only requirement is to know the key that the data has been stored with.
很多时候,你需要强行删除无效的缓存条目(由于数据更新的原因),唯一的要求就是,你得知道该缓存的KEY。

.. code-block:: php
Expand All @@ -213,9 +203,9 @@ The only requirement is to know the key that the data has been stored with.
}
Checking cache existance
检测缓存是否存在
------------------------
It is possible to check if cache is already exists with given key.
通过给定的KEY值,可以检测缓存是否存在。

.. code-block:: php
Expand All @@ -229,7 +219,7 @@ It is possible to check if cache is already exists with given key.
}
Frontend Adapters
前端适配器
-----------------
The available frontend adapters that are used as interfaces or input sources to the cache are:

Expand All @@ -245,13 +235,13 @@ The available frontend adapters that are used as interfaces or input sources to
| None | It's used to cache any kind of PHP data without serializing them. | :doc:`Phalcon\\Cache\\Frontend\\Data <../api/Phalcon_Cache_Frontend_None>` |
+---------+--------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------+

Implementing your own Frontend adapters
实现自定义的前端适配器
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.

Backend Adapters
后端适配器
----------------
The backend adapters available to store cache data are:
可用的后端存储器列表:

+-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+
| Adapter | Description | Info | Required Extensions | Example |
Expand All @@ -265,11 +255,11 @@ The backend adapters available to store cache data are:
| Mongo | Stores data to Mongo Database | MongoDb_ | `Mongo`_ | :doc:`Phalcon\\Cache\\Backend\\Mongo <../api/Phalcon_Cache_Backend_Mongo>` |
+-----------+------------------------------------------------+------------+---------------------+-----------------------------------------------------------------------------------+

Implementing your own Backend adapters
实现自定义后端适配器
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.

File Backend Options
文件缓存选项
^^^^^^^^^^^^^^^^^^^^
This backend will store cached content into files in the local server. The available options for this backend are:

Expand All @@ -279,7 +269,7 @@ This backend will store cached content into files in the local server. The avail
| cacheDir | A writable directory on which cached files will be placed |
+----------+-----------------------------------------------------------+

Memcached Backend Options
Memcached缓存选项
^^^^^^^^^^^^^^^^^^^^^^^^^
This backend will store cached content on a memcached server. The available options for this backend are:

Expand All @@ -293,11 +283,11 @@ This backend will store cached content on a memcached server. The available opti
| persistent | create a persitent connection to memcached? |
+------------+---------------------------------------------+

APC Backend Options
APC缓存选项
^^^^^^^^^^^^^^^^^^^
This backend will store cached content on Alternative PHP Cache (APC_). This cache backend does not require any additional configuration options.

Mongo Backend Options
Mongo缓存选项
^^^^^^^^^^^^^^^^^^^^^^^^^
This backend will store cached content on a MongoDB server. The available options for this backend are:

Expand Down
22 changes: 11 additions & 11 deletions zh/reference/filter.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Filtering and Sanitizing
========================
Sanitizing user input is a critical part of software development. Trusting or neglecting to sanitize user input could lead to unauthorized access to the content of your application, mainly user data, or even the server your application is hosted.
对用户输入的数据进行过滤/消毒是软件开发的重要组成部分。过分信任或忽略过滤用户输入,可能导致用户访问到未经授权的页面,主要是用户数据,甚至是你应用程序的服务器托管的所有内容。

.. figure:: ../_static/img/sql.png
:align: center

Full image (from xkcd)

The :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` component provides a set of commonly used filters and data sanitizing helpers. It provides object-oriented wrappers around the PHP filter extension.
:doc:`Phalcon\\Filter <../api/Phalcon_Filter>` 组件提供了一组常用的用于过滤以及处理用户输入数据的助手工具。它提供了一种面像对象的方式来包装PHP filter扩展。

Sanitizing data
---------------
Sanitizing is the process which removes specific characters from a value, that are not required or desired by the user or application. By sanitizing input we ensure that application integrity will be intact.
Sanitizing 处理从字符串中移除指定字符,这并不是必须的,需要开发者明确指定。sanitizing后的用户输入数据,能确保应用程序的完整和安全。

.. code-block:: php
Expand All @@ -34,7 +34,7 @@ Sanitizing is the process which removes specific characters from a value, that a
Sanitizing from Controllers
---------------------------
You can access a :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` object from your controllers when accessing GET or POST input data (through the request object). The first parameter is the name of the variable to be obtained; the second is the filter to be applied on it.
你可以在控制器中访问 :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` 对象,当需要访问GET或POST输入数据时(通过request对象)。第一个参数是变量的名称,第二个参数是filter类型。

.. code-block:: php
Expand Down Expand Up @@ -63,7 +63,7 @@ You can access a :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` object from your
Filtering Action Parameters
---------------------------
The next example shows you how to sanitize the action parameters within a controller action:
下面的示例将向你展示如何在controller/action中 sanitize Action的参数:

.. code-block:: php
Expand All @@ -86,7 +86,7 @@ The next example shows you how to sanitize the action parameters within a contro
Filtering data
--------------
In addition to sanitizing, :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` also provides filtering by removing or modifying input data to the format we expect.
除了sanitizing功能,:doc:`Phalcon\\Filter <../api/Phalcon_Filter>` 还提供了删除或修改输入数据的过滤功能,以生成我们想要的数据。

.. code-block:: php
Expand All @@ -101,7 +101,7 @@ In addition to sanitizing, :doc:`Phalcon\\Filter <../api/Phalcon_Filter>` also p
$filter->filter(" Hello ", "trim");
Types of Built-in Filters
Filters内置类型
-------------------------
The following are the built-in filters provided by this component:

Expand All @@ -127,9 +127,9 @@ The following are the built-in filters provided by this component:
| upper | Applies the strtoupper_ function |
+-----------+---------------------------------------------------------------------------+

Creating your own Filters
自定义Filters
-------------------------
You can add your own filters to :doc:`Phalcon\\Filter <../api/Phalcon_Filter>`. The filter function could be an anonomyous function:
你可以创建自定义过滤器添加到 :doc:`Phalcon\\Filter <../api/Phalcon_Filter>`。过滤函数可以使用匿名函数的形式:

.. code-block:: php
Expand All @@ -145,7 +145,7 @@ You can add your own filters to :doc:`Phalcon\\Filter <../api/Phalcon_Filter>`.
//Sanitize with the "md5" filter
$filtered = $filter->sanitize($possibleMd5, "md5");
Or, if you prefer, you can implement the filter in a class:
或者,如果你愿意,你也可以实现一个过滤器类:

.. code-block:: php
Expand All @@ -171,7 +171,7 @@ Or, if you prefer, you can implement the filter in a class:
Complex Sanitizing and Filtering
--------------------------------
PHP itself provides an excellent filter extension you can use. Check out its documentation: `Data Filtering at PHP Documentation`_
PHP本身也提供了一个极好的filter扩展,查阅文档:`Data Filtering at PHP Documentation`_

Implementing your own Filter
----------------------------
Expand Down
Loading

0 comments on commit 80d61cc

Please sign in to comment.