用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5957|回复: 10
收起左侧

MP_Cache 中文用户指南--译:莞动力网络

[复制链接]
发表于 2012-6-9 17:04:09 | 显示全部楼层 |阅读模式
MP_Cache: User Guide

MP_Cache: 用户指南

译:莞动力网络 - Avin
原文地址 : http://www.dgpower.net/news/shownews/408.html
MP_Cache was written as a more flexible caching solution than those offered by CodeIgniter. As such it offers you a way to cache anything from a single variable, to a SQL query or the fully generated page. MP_Cache saves these entries serialized to files and has functions to retrieve and delete these.

MP_Cache 类是为CodeIgniter提供更多灵活性的缓存解决方案。 你能使用它来缓存从数据库查询获得的单一数据变量或者缓存整个页面。 MP_Cache 把序列化后的数据保存进文件,同时提供函数方法去回收重建和删除该缓存文件。

Additionally you can set expiration time and dependencies to force a main cache to refresh itself after one of its subcaches has been refreshed or deleted.

同时,你能够设置缓存过期时间和为某个主缓存与多个其它缓存建立依赖(关联缓存),使得主缓存的更新和删除,都依赖于子缓存。(也即是,当子缓存过期更新或者被删除时,主缓存会自动同步更新或者被删除)

Installing MP_Cache
•Download the file here and unzip it to your application/libraries directory.
•下载此文件并解压到你项目的application/libraries目录

•Create a directory for you cache, for example application/mp_cache
•建立一个缓存目录,例如在:application/mp_cache

•Add a value 'mp_cache_dir' to an autoloaded config file, for instance application/config/config.php and set it to the mp_cache directory relative to your index.php file.
For example: $config['mp_cache_dir'] = APPPATH.'mp_cache/';
•在项目的:application/config/config.php里增加一个变量值'mp_cache_dir'进自动加载配置文件。例如:$config['mp_cache_dir'] = './mp_cache/';

•Optional: add a value 'mp_cache_default_expires' to the same autoloaded config file and set it to a default expiration time in seconds if you want it set by default. You can still make something never expire by setting it to 0.
•可选项:增加一个缓存默认过期时间写在自动加载的config文件里,你仍然通过设置其过期时间为零来让缓存永远不过期。例如:$config['mp_cache_default_expires'] =3600; (3600是一个小时),或者$config['mp_cache_default_expires'] = 0;(永不过期)

•That's it, you can start using MP_Cache.
•现在,你可以开始正式使用MP_Cache了

Initializing MP_Cache
初始化 MP_Cache

Like most other classes in CodeIgniter, the MP_Cache class is initialized in your controller using the $this->load->library function:

就像在CodeIgniter里调用所有其它类一样,MP_Cache类在使用前需要在你的controller里使用如下语句加载:$this->load->library function

$this->load->library('MP_Cache');Once the library is loaded it will be ready for use. The MP_Cache library object you will use to call all functions is: $this->mp_cache.

一旦类库加载完成,则随时可以使用了,你可以使用如下方式来调用MP_Cache类的对象:$this->mp_cache

Basic caching without additional settings
基本的缓存操作不需要其它设置

To just cache something within your controller or model, the usage is like this:

此处只是缓存一些简单的控制器或者模型里的数据,使用方法如下:

$data = $this->mp_cache->get('example');if ($data === false){    // example model-function that generates your $data variable    $data = $this->Pages->read($page);        $this->mp_cache->write($data, 'example');}The variable to be cached can be of any kind, though object-references will be lost when loaded from cache.

可以缓存任何形式的变量,当加载缓存时尽管对象引用会丢失

Alternative syntax for the example above
上面的例子可用另一种语法实现

You can also use the cache in a more object oriented way. In this case you always call the set_name() method before any other because it will reset the object to its default state.

你还可以使用面向对象的方式使用缓存. 在这例子里,你在使用其它方法前总是调用 set_name()函数 ,它将会重置这对象和默认状态。

$data = $this->mp_cache->set_name('example')->get();if ($data === false){     // example model-function that generates your $data variable    $data = $this->Pages->read($page);        $this->mp_cache->set_contents($data)->write();}Warning: You can't combine the syntaxes, using the parameterized versions of write() and get() will reset the mp_cache object to prevent contamination of your current cache operation from a previous one.

警告:在使用任何一个缓存前,你调用带参数的write()和get()函数都将会重置mp_cache对象,使得保证当前操作的对象值不被污染,所以这两个函数同时使用时,不能使用联合语法进行操作(联合语法如:$this->mp_cache->write()->get();)。

Using subdirectories
使用子目录

For PHP5 subdirectories with virtual unlimited depth are supported, PHP4 only supports 1 level. You can set subdirectories by simply adding them in the cache name. The first line of the alternative syntax example is below in the directory "directory" which in turn is in the directory "sub".

使用PHP5 能够支持无限级层次目录。PHP4只支持一层,你可以设置在在缓存名称上设置目录层次,第一段是一个目录的同时又是主目录下的子级目录

$data = $this->mp_cache->set_name('sub/directory/example')->get();Adding an expiration time
增加一个过期时间

Expiration time is added when writing the cache. Exparation is set to 0 by default which means no experation. If you want it to expire after a certain time you have to specify it in seconds after it was generated. To do so you have to change the write function like this when using the write() function with parameters:

当写入缓存的时候可以增加缓存过期时间。过期时间默认设置为零即是永久不过期。如果你希望设置一个准确的过期时间,你可以特别地在write()方法的第三个参数设置。你可以用以下的写法修改write()方法的参数:

// Cache it for 2 hours (7200 seconds = 60 seconds * 60 minutes * 2 hours)$this->mp_cache->write($data, 'example', 7200);Or do it by using the set_expires() function:

或者使用set_expires()函数来进行设置:

$this->mp_cache->set_name('example')->set_contents($data)->set_expires(7200)->write();Adding dependencies
增加缓存依赖(缓存关联)

You can add cache file on which the expiration of the current cache file is dependend. When one of those files has been deleted or refreshed after the current cache was made, the current cache will be deleted. So lets say we have a menu called mainmenu which we have cached in a subdirectory called menus. Using the parameterized write() function you can add the dependency like this (expiration set to none):

你可以增加另外的缓存文件与当前缓存文件的过期依赖关系。一个或者多个与之关联的缓存文件文件会被删除或者更新,当前缓存也会被删除。比如我有一个缓存在menus子目录下,名字叫"mainmenu"的菜单,使用使用带参数的write()函数可与之加入进依赖(关联),(过期时间设置为null)

$this->mp_cache->write($data, 'example', null, array('menus/mainmenu'));Or using the set_dependencies() function (which takes an array or a string):

或者使用set_dependencies()函数 (需要传递一个数组或者一个字符串作为参数):

$this->mp_cache->set_name('example')->set_contents($data)->set_dependencies('menus/mainmenu')->write();Deleting individual caches
删除单个缓存

You can delete cache by using the delete() function:

你可以使用delete()函数删除单个缓存文件

$this->mp_cache->delete('example');Or in the alternative way:

或者使用以下方式:

$this->mp_cache->set_name('example')->delete();Deleting the entire cache or subdirectories of it
删除所有缓存或者某个子目录下的所有缓存

To delete the entire cache you can use the delete_all() function:

删除整个所有缓存,可以使用delete_all()函数:

$this->mp_cache->delete_all();Or you can delete a subdirectory of the main cache, like all the menus from the example for dependencies:

或者 你可以删除主目录下某个子目录,如下这个带有依赖(关联)的所有菜单的例子:

$this->mp_cache->delete_all('menus');This will delete all the files in the subdirectory menus of the main cache. All the subdirectories of the menus directory and files within them will also be deleted.

这将会删除与所有在menus目录下的所有缓存有依懒(关联)关系的主缓存。以及所有menus子目录下的文件都会被删除

Full functions overview
reset()
Resets the current state of the mp_cache object. It is called automatically from the set_name() function or when the write() & get() functions are used with parameters. This is done to prevent any previous usage of the mp_cache object from contaminating your current usage with settings you would expect to be the default ones.

$this->mp_cache->reset();set_name() & get_name()
Sets or returns the full filename of the cache.
Warning: the set_name() function automatically calls reset(), so always use it as the first function when using the alternative syntax.

$this->mp_cache->set_name('example');// after this is set the variable below will be set to the string 'example'$var = $this->mp_cache->get_name();set_contents() & get_contents()
Sets or returns the contents you are caching or retrieving.

$this->mp_cache->set_contents(array(1,2,3,4,5));// The function below will return the array set above$array = $this->mp_cache->get_contents();set_dependencies(), add_dependencies() & get_dependencies()
Sets the dependencies, adds to the current dependencies or returns the set dependencies.

$this->mp_cache->set_dependencies('example');// this sets the dependencies to the 'example' cache file$this->mp_cache->set_dependencies(array('cachefile1', 'cachefile2'));// this has overwritten the first statement and set the dependencies to 'cachefile1' & 'cachefile2'$this->mp_cache->add_dependencies('example')// this has added 'example' as a dependency in addition to 'cachefile1' & 'cachefile2'All functions take both strings and arrays of strings.

set_expires() & get_expires()
Sets the expiration time and retrieves the time on which the cache will expire.

$this->mp_cache->set_expires(7200);// sets the expiration to a timestamp 2 hours from now$expires = $this->mp_cache->get_expires();// $expires is set to the timestamp 2 hours from now as defined by set_expires()get_created()
Returns the timestamp from the moment the retrieved cache was created, returns NULL when used while writing cache.

$created_on = $this->mp_cache->get_created();
 楼主| 发表于 2012-6-9 17:13:51 | 显示全部楼层
个人觉得,MP_Cache最吸引人的地方,是关联依赖这个特性了。刚开始看英文指南时有点不解。后来试用了下,真的不错。{:soso_e100:}推荐一个。
发表于 2012-6-10 16:24:03 | 显示全部楼层
人不猥琐枉少年!

我來就是對你最大的支持
发表于 2012-6-12 09:50:00 | 显示全部楼层
{:soso_e179:}
发表于 2012-6-12 10:15:27 | 显示全部楼层
楼主在东莞吗?小弟也是在东莞呢~~~
发表于 2012-6-12 17:49:12 | 显示全部楼层
可以尝试下
发表于 2012-7-3 17:11:41 | 显示全部楼层
试试看呵呵
发表于 2012-9-25 10:38:00 | 显示全部楼层

教程真不错啊。。。
英文教程。。看得头昏眼花的。。
lz真是厉害
 楼主| 发表于 2013-1-6 01:20:42 | 显示全部楼层
以前只是翻译了下。 一直没有真正运用在项目上。项目上一直用CI原生的页面缓存$this->output->cache(10);。后来发现当有用户登陆后,跳回到被cache过的页面。 会员的已登陆信息显示不出来。也就是原来页面的cache还没有被更新。现在索性把全项目的cache换成MP_Cache,用起来很灵活。
发表于 2013-1-16 11:09:44 | 显示全部楼层
  $data = $this->Pages->read($page);
楼主,可否解释下这个类从哪来的?CI自带的库里没有呀

本版积分规则