codeigniter的功能纵然强大,也有不足之处。其cache模式在于针对不同的uri就会生成cache文件,如果URL中参数不同,则 cache文件就会不同,从而产生了漏洞。如果访问者构建自动生成URI,不断向服务器发起请求,就会瞬间产生大量的垃圾文件,导致系统文件臃肿。
参看CI中国论坛:脆弱的CI缓存系统,1天攻陷你的CI网站http://codeigniter.org.cn/forums/thread- 1690-1-1.html
因此如果要用到cache功能就必须找到第三方的开发库。Pear中,Cache_Lite属于较为轻量级的缓存系统,且功能强大,可以作为CI原 生cache的替代品,将其加入自定义的library文件即可完成。
步骤如下:
1. 下载Cache_Lite
http://download.pear.php.net/package/Cache_Lite-1.7.7.tgz
2. 在要使用Cache_Lite的Controller中,加入:
如:读取缓存:$string = $this->cc->get($id);
生成缓存:$this->cc->save($outputString);
首先,在config.php中设置一下缓存目录,这个目录要存在且可写入
现在看看代码
两个注意点:
1,缓存数据,必须是data,而不是resource id。如,在使用AR后用result(),result_array()等返回的数据
2,缓存名不要重复。可以使用子文件夹分隔。如,$listNews = $this->mp_cache->get(‘news/listNews’);同样创建缓存数据片段 用$this->mp_cache->write($listNews, ‘news/listNews’);(php5下news文件夹会自动创建)
其他方法:
删除名为$filename的cache.
删除 $dirname目录及其下所有缓存.如果$dirname没有设置,则删除所有缓存.
http://mpsimple.mijnpraktijk.com/mp_cache/user_guide.htm
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.
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.
Like most other classes in CodeIgniter, the MP_Cache class is initialized in your controller using the
$this->load->library function :
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 .
To just cache something within your controller or model, the usage is like this:
The variable to be cached can be of any kind, though object-references will be lost when loaded from cache.
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.
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.
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 ".
Expiration time is added when writing the cache. Exparation is set to NULL by default which means using the default expiration, you can set it to 0 for no experation (if the default is set to something larger than 0). 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:
Or do it by using the set_expires() function:
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):
Or using the set_dependencies() function (which takes an array or a string):
You can delete cache by using the delete() function:
Or in the alternative way:
To delete the entire cache you can use the delete_all() function:
Or you can delete a subdirectory of the main cache, like all the menus from the example for dependencies:
This will delete all the files in the subdirectory menus of the main cache. All the subdirectories of the menusdirectory and files within them will also be deleted.
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.
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.
Sets or returns the contents you are caching or retrieving.
Sets the dependencies, adds to the current dependencies or returns the set dependencies.
All functions take both strings and arrays of strings.
Sets the expiration time and retrieves the time on which the cache will expire.
Returns the timestamp from the moment the retrieved cache was created, returns NULL when used while writing cache.