php如何利用内存作为php 缓存数据在内存使用

扫一扫体验手机阅读
PHP服务缓存加速优化实战
<span type="1" blog_id="1967166" userid='
分享到朋友圈
关注作者,不错过每一篇精彩PHP可以直接读取内存中的数据,并使用吗???我想做一个分页的预加载技术 ,就是当你点击某一页的时候,那么我直接读取这一页以后的5页,或者更多,而这5页,我想把它存入内存中,当点第二页的时候,再判断如果内存中有这个数据,那么直接调用,这样既可以提高读取速度,也可以减轻数据库的负担,或者各位有什么更好的办法???拿出来分享一下啊
该问题被发起重新开启投票
投票剩余时间:
之前被关闭原因:
该问题被发起删除投票
投票剩余时间:
距离悬赏到期还有:
参与关闭投票者:
关闭原因:
该问题已经被锁定
锁定原因:()
保护原因:避免来自新用户不合宜或无意义的致谢、跟帖答案。
该问题已成功删除,仅对您可见,其他人不能够查看。
不幸地告诉你,PHP是直接操作内存的,你声明的一个变量,新建的对象,都是放在内存里的。。--------------分割线------------------------其实,你要的是一个缓存,每次读取的时候,先读cache,cache里没有的话,,你预加载5页,放到memcached(一个比较常用的Cache)里;如果有的话,就直接读cache。
同上,不过补充PHP还有一个shmop功能集,可以操作共享内存段,不过可能VPS也不会开放这个功能,如果是自己的机器,可以利用这个高效的东东
最简单的cache,用文件的方式存储。
创建一个mysql的内存表,表结构为key和value。
你都已经说了VPS提供商了,既然是VPS那你自己装一个不就得了。不要告诉我你的VPS不能自由安装软件,那样的话它根本就不是VPS。虚拟主机的话如果不提供就确实没有办法。但你可以考虑使用MYSQL的内存表当CACHE用。但也许对方也禁止创建内存表,这样就没办法了。不过你可以利用HTML5的客户端数据库在客户端做缓存。至于用文件做缓存,一般来说效率很低。即使要缓存,你把缓存数据放进Mysql也比用文件快吧(我没有验证过,也许应该实际试试才知道哪个更快。)
不是您所需,查看更多相关问题与答案
德问是一个专业的编程问答社区,请
后再提交答案
关注该问题的人
共被浏览 (7331) 次php内存缓存实现方法
转载 &更新时间:日 11:38:33 & 投稿:shichen2014
这篇文章主要介绍了php内存缓存实现方法,分析了Memcached缓存的用法并比较了APC、EC、Zend加速器的用法,需要的朋友可以参考下
本文实例讲述了php内存缓存实现方法。分享给大家供大家参考。具体如下:
在php中缓存分为很多种类型如,内存缓存,文件缓存,页面缓存。本文要来讲述关于php中内存缓存的一些方法,这里我们将介绍Memcached缓存和php自带的APC缓存方法.
1.Memcached缓存。
memcached是高性能的分布式内存缓存服务器,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度,memcached 使用了“Key=&Value”方式组织数据,可以允许不同主机上的多个用户同时访问这个缓存系统,一般用于大型网站使用,memcached使用内存缓存数据,所以它是易失的,当服务器重启,或者memcached进程中止,数据便会丢失,所以 memcached不能用来持久保存数据.
用过php_memcache的人都会觉得 PHP内存缓存是一个很复杂的东西,其实不然memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速 WEB 动态应用程序.
这里介绍memcached在WIN32下的配置及其使用.
一、PHP内存缓存的配置,WIN32环境
1、下载php_memcache.rar
解压压缩包:php_memcache.rar
php_memcache.rar压缩包里主包含的文件有:
/memcached-1.2.1-win32/memcached.exe
/php_memcache/php_memcache.dll
2、打开命令提示符,指向到memcached.exe所在路径,运行memcached.exe -d start。
3、将php_memcache.dll文件拷贝到PHP的动态文件库的文件夹下。
4、在php.ini文件中加入一行extension=php_memcache.dll。
5、重新启动Apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!
例,代码如下:
代码如下:&&#63;php
//包含 memcached 类文件
require_once('memcached-client.php');
//选项设置
$options = array(
&'servers' =& array('www.jb51.net:11211'),//memcached 服务的地址、端口
&'debug' =& true,//是否打开debug
&'compress_threshold' =& 10240,//超过多少字节的数据时进行压缩
&'persistant' =& false//是否使用持久连接
//实例化memcached对象
$memcached = new memcached($options);
$sql = 'SELECT * FROM table1';
$key = md5($sql);
//如果在memcached中没有缓存数据,把缓存数据写入memcached
if(!($datas = $memcached-&get($key)))
&$conn = mysql_connect('localhost', 'hxsd', '123456');
&mysql_select_db('hxsd');
&$result = mysql_query($sql);
&while($row = mysql_fetch_object($result))
& $datas[] = $
&//将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。
&$memcached-&add($key, $datas);
&//直接使用memcached中的缓存数据$datas
内存缓存二,APC、EC、Zend加速器的比较
APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”,主页是 http://pecl.php.net/package/apc,php帮助手册页面: http://cn.php.net/apc
APC是个优化器,自安装之日起,就默默地在后台为您的PHP应用服务了.您的所有PHP代码会被缓存起来,针对php opcode.
另外,APC可提供一定的内存缓存功能.但是这个功能并不是十分完美,有报告说如果频繁使用APC缓存的写入功能,会导致不可预料的错误.如果想使用这个功能,可以看看apc_fetch,apc_store等几个与apc缓存相关的函数.
安装,代码如下: 代码如下:# pecl install APC
配置:/etc/php.inc,代码如下:
代码如下:extension=apc.so
代码如下:apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 30
apc.optimization = 0
apc.ttl = 7200
apc.user_ttl = 7200
apc.num_files_hint = 1000
apc.mmap_file_mask = /tmp/apc.XXXXXX
希望本文所述对大家的php程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具博客分类:
PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方式,但是推荐使用目前稳定的版本1.7.6,因为之前的版本都会不同程度的存在bug,以下是其官方文档:
PHPExcel1.7.6官方文档 写道
PHPExcel uses an average of about 1k/cell in your worksheets, so large workbooks can quickly use up available memory. Cell caching provides a mechanism that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. This allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data.
PHPExcel平均下来使用1k/单元格的内存,因此大的文档会导致内存消耗的也很快。单元格缓存机制能够允许PHPExcel将内存中的小的单元格对象缓存在磁盘或者APC,memcache或者Wincache中,尽管会在读取数据上消耗一些时间,但是能够帮助你降低内存的消耗。
PHPExcel1.76.官方文档 写道
By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. To enable cell caching, you must call the PHPExcel_Settings::setCacheStorageMethod() method, passing in the caching method that you wish to use.
默认情况下,PHPExcel依然将单元格对象保存在内存中,但是你可以自定义。你可以使用PHPExcel_Settings::setCacheStorageMethod()方法,将缓存方式作为参数传递给这个方法来设置缓存的方式。
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
PHPExcel1.7.6官方文档 写道
setCacheStorageMethod() will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled).
setCacheStorageMethod()方法会返回一个BOOL型变量用于表示是否成功设置(比如,如果APC不能使用的时候,你设置使用APC缓存,将会返回false)
PHPExcel1.7.6官方文档 写道
A separate cache is maintained for each individual worksheet, and is automatically created when the worksheet is instantiated based on the caching method and settings that you have configured. You cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet.
每一个worksheet都会有一个独立的缓存,当一个worksheet实例化时,就会根据设置或配置的缓存方式来自动创建。一旦你开始读取一个文件或者你已经创建了第一个worksheet,就不能在改变缓存的方式了。
PHPExcel1.7.6官方文档 写道
Currently, the following caching methods are available.
目前,有以下几种缓存方式可以使用:
PHPExcel_CachedObjectStorageFactory::cache_in_
PHPExcel1.7.6官方文档 写道
The default. If you don’t initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at present.
默认情况下,如果你不初始化任何缓存方式,PHPExcel将使用内存缓存的方式。
===============================================
PHPExcel_CachedObjectStorageFactory::cache_in_memory_
PHPExcle1.7.6官方文档 写道
Using this caching method, cells are held in PHP memory as an array of serialized objects, which reduces the memory footprint with minimal performance overhead.
使用这种缓存方式,单元格会以序列化的方式保存在内存中,这是降低内存使用率性能比较高的一种方案。
===============================================
PHPExcel_CachedObjectStorageFactory::cache_in_memory_
PHPExcel1.7.6官方文档 写道
Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects, but gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.
与序列化的方式类似,这种方法在序列化之后,又进行gzip压缩之后再放入内存中,这回跟进一步降低内存的使用,但是读取和写入时会有一些慢。
===========================================================
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
PHPExcel1.7.6官方文档 写道
When using cache_to_discISAM all cells are held in a temporary disk file, with only an index to their location in that file maintained in PHP memory. This is slower than any of the cache_in_memory methods, but significantly reduces the memory footprint.
The temporary disk file is automatically deleted when your script terminates.
当使用cache_to_discISAM这种方式时,所有的单元格将会保存在一个临时的磁盘文件中,只把他们的在文件中的位置保存在PHP的内存中,这会比任何一种缓存在内存中的方式都慢,但是能显著的降低内存的使用。临时磁盘文件在脚本运行结束是会自动删除。
===========================================================
PHPExcel_CachedObjectStorageFactory::cache_to_phpT
PHPExcel1.7.6官方文档 写道
Like cache_to_discISAM, when using cache_to_phpTemp all cells are held in the php://temp I/O stream, with only an index to their location maintained in PHP memory. In PHP, the php://memory wrapper stores data in the memory: php://temp behaves similarly, but uses a temporary file for storing the data when a certain memory limit is reached. The default is 1 MB, but you can change this when initialising cache_to_phpTemp.
类似cache_to_discISAM这种方式,使用cache_to_phpTemp时,所有的单元格会还存在php://temp I/O流中,只把他们的位置保存在PHP的内存中。PHP的php://memory包裹器将数据保存在内存中,php://temp的行为类似,但是当存储的数据大小超过内存限制时,会将数据保存在临时文件中,默认的大小是1MB,但是你可以在初始化时修改它:
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpT
$cacheSettings = array( ' memoryCacheSize '
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel1.7.6官方文档 写道
The php://temp file is automatically deleted when your script terminates.
php://temp文件在脚本结束是会自动删除。
===========================================================
PHPExcel_CachedObjectStorageFactory::cache_to_
PHPExcle1.7.6官方文档 写道
When using cache_to_apc, cell objects are maintained in APC
with only an index maintained in PHP memory to identify that the cell exists. By default, an APC cache timeout of 600 seconds is used, which should be enough for most applications: although it is possible to change this when initialising cache_to_APC.
当使用cach_to_apc时,单元格保存在APC中,只在内存中保存索引。APC缓存默认超时时间时600秒,对绝大多数应用是足够了,当然你也可以在初始化时进行修改:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_APC;
$cacheSettings = array( 'cacheTime'
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel1.7.6官方文档 写道
When your script terminates all entries will be cleared from APC, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
当脚本运行结束时,所有的数据都会从APC中清楚(忽略缓存时间),不能使用此机制作为持久缓存。
===========================================================
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
PHPExcel1.7.6官方文档 写道
When using cache_to_memcache, cell objects are maintained in memcache
with only an index maintained in PHP memory to identify that the cell exists.
By default, PHPExcel looks for a memcache server on localhost at port 11211. It also sets a memcache timeout limit of 600 seconds. If you are running memcache on a different server or port, then you can change these defaults when you initialise cache_to_memcache:
使用cache_to_memory时,单元格对象保存在memcache中,只在内存中保存索引。默认情况下,PHPExcel会在localhost和端口11211寻找memcache服务,超时时间600秒,如果你在其他服务器或其他端口运行memcache服务,可以在初始化时进行修改:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_
$cacheSettings = array( 'memcacheServer'
=& 'localhost',
'memcachePort'
'cacheTime'
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
从初始化设置的形式上看,MS还不支持多台memcache服务器轮询的方式,比较遗憾。
PHPExcel1.7.6官方文档 写道
When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
当脚本结束时,所有的数据都会从memcache清空(忽略缓存时间),不能使用该机制进行持久存储。
===========================================================
PHPExcel_CachedObjectStorageFactory::cache_to_
PHPExcel1.7.6官方文档 写道
When using cache_to_wincache, cell objects are maintained in Wincache
with only an index maintained in PHP memory to identify that the cell exists. By default, a Wincache cache timeout of 600 seconds is used, which should be enough for most applications: although it is possible to change this when initialising cache_to_wincache.
使用cache_towincache方式,单元格对象会保存在Wincache中,只在内存中保存索引,默认情况下Wincache过期时间为600秒,对绝大多数应用是足够了,当然也可以在初始化时修改:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_
$cacheSettings = array( 'cacheTime'
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
PHPExcel官方文档1.7.6 写道
When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value, so it cannot be used for persistent storage using this mechanism.
呃,终于“又”写完了,之前写的一版,有的文字是直接从word粘过来的,带了一大堆格式,被截断了,悲了个剧的,翻译文档也不是件容易的事啊……PHPExcel还是比较强大的,最大的问题就是内存占用的问题,我之前用的1.7.2,还没有这种机制,导出2W+数据,占用了400M+内存,改成1.7.6,使用cach_to_diskISAM方式,内存降低到200M-,效果还是很明显的,不过依然还是够高的,excel文件5.1M,就使用了200M-和未知大小的磁盘空间,PHPExcel啥时候能出一个轻量级的版本,不需要那么多花哨的功能,只需要导出最普通的数据的版本就好了!
浏览 11222
不知道为什么,您能帮忙解决下吗? 谢谢了看看占用内存的大小,windows用任务管理器,linux用top
浏览: 224339 次
来自: 北京
浏览量:69572
价值观这种东西,说不好听是口号,其实是公司每天潜移默化的感染, ...
stef831018 写道你想多了,企业价值观就两个字:利益怎 ...
你想多了,企业价值观就两个字:利益
mangguo 写道推荐大家去看看我写的python入门,可以 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'1、全页面静态化缓存
也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解析的流程。此种方式,在CMS系统中比较常见,比如dedecms;
一种比较常用的实现方式是用输出缓存:
Ob_start()******要运行的代码*******$content = Ob_get_contents();****将缓存内容写入html文件*****Ob_end_clean();
2、页面部分缓存
该种方式,是将一个页面中不经常变的部分进行静态缓存,而经常变化的块不缓存,最后组装在一起显示;可以使用类似于ob_get_contents的方式实现,也可以利用类似ESI之类的页面片段缓存策略,使其用来做动态页面中相对静态的片段部分的缓存(ESI技术,请baidu,此处不详讲)。
该种方式可以用于如商城中的商品页;
3、数据缓存
顾名思义,就是缓存数据的一种方式;比如,商城中的某个商品信息,当用商品id去请求时,就会得出包括店铺信息、商品信息等数据,此时就可以将这些数据缓存到一个php文件中,文件名包含商品id来建一个唯一标示;下一次有人想查看这个商品时,首先就直接调这个文件里面的信息,而不用再去数据库查询;其实缓存文件中缓存的就是一个php数组之类;
Ecmall商城系统里面就用了这种方式;
4、查询缓存
其实这跟数据缓存是一个思路,就是根据查询语句来缓存;将查询得到的数据缓存在一个文件中,下次遇到相同的查询时,就直接先从这个文件里面调数据,不会再去查数据库;但此处的缓存文件名可能就需要以查询语句为基点来建立唯一标示;
按时间变更进行缓存
其实,这一条不是真正的缓存方式;上面的2、3、4的缓存技术一般都用到了时间变更判断;就是对于缓存文件您需要设一个有效时间,在这个有效时间内,相同的访问才会先取缓存文件的内容,但是超过设定的缓存时间,就需要重新从数据库中获取数据,并生产最新的缓存文件;比如,我将我们商城的首页就是设置2个小时更新一次;
5、按内容变更进行缓存
这个也并非独立的缓存技术,需结合着用;就是当数据库内容被修改时,即刻更新缓存文件;
比如,一个人流量很大的商城,商品很多,商品表必然比较大,这表的压力也比较重;我们就可以对商品显示页进行页面缓存;
当商家在后台修改这个商品的信息时,点击保存,我们同时就更新缓存文件;那么,买家访问这个商品信息时,实际上访问的是一个静态页面,而不需要再去访问数据库;
试想,如果对商品页不缓存,那么每次访问一个商品就要去数据库查一次,如果有10万人在线浏览商品,那服务器压力就大了;
6、内存式缓存
提到这个,可能大家想到的首先就是Memcached;memcached是高性能的分布式内存缓存服务器。 一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、 提高可扩展性。
它就是将需要缓存的信息,缓存到系统内存中,需要获取信息时,直接到内存中取;比较常用的方式就是 key–&value方式;
$memcachehost = '192.168.6.191';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new M
$memcache-&connect($memcachehost,$memcacheport) or die ("Could not connect");
$memcache-&set('key','缓存的内容');
$get = $memcache-&get($key); //获取信息?&
7、apache缓存模块
apache安装完以后,是不允许被cache的。如果外接了cache或squid服务器要求进行web加速的话,就需要在htttpd.conf里进行设置,当然前提是在安装apache的时候要激活mod_cache的模块。
安装apache时:./configure –enable-cache –enable-disk-cache –enable-mem-cache
8、php APC缓存扩展
Php有一个APC缓存扩展,windows下面为php_apc.dll,需要先加载这个模块,然后是在php.ini里面进行配置:
extension=php_apc.dll
apc.rfc1867 = on
upload_max_filesize = 100M
post_max_size = 100M
apc.max_file_size = 200M
upload_max_filesize = 1000M
post_max_size = 1000M
max_execution_time = 600 ; 每个PHP页面运行的最大时间值(秒),默认30秒
max_input_time = 600 ; 每个PHP页面接收数据所需的最大时间,默认60
memory_limit = 128M ; 每个PHP页面所吃掉的最大内存,默认8M
9、Opcode缓存
我们知道,php的执行流程可以用下图来展示:
首先php代码被解析为Tokens,然后再编译为Opcode码,最后执行Opcode码,返回结果;所以,对于相同的php文件,第一次运行时可以缓存其Opcode码,下次再执行这个页面时,直接会去找到缓存下的opcode码,直接执行最后一步,而不再需要中间的步骤了。
比较知名的是XCache、Turck MM Cache、PHP Accelerator等。
阅读(...) 评论()

我要回帖

更多关于 php 内存使用情况 的文章

 

随机推荐