PHP高效文件缓存类FCache

自己用的阿里云低配置,最近访问不错,经常出现mysql崩溃的问题,提单说请加内存,这回复还不如直接说:请交钱!

于是自己在git找了php的文件缓存,发现都是将缓存放到一个缓存文件!!!意味着:

1.无论你是读取多大的数据,都需要从磁盘读出整个文件到内存,然后解析,获取你要的部分数据;

2.在缓存数据很大的时候,并不能起到缓存加速网站访问的目的,同时增加磁盘的读写负荷;

3.在某一个临界点,可能会导致缓存性能比数据库还差;

4.未经过严格测试,个人预估一般网站的数据都会达到100M以上,如果每次访问需要读取100M的数据,然后解析,性能非常低效。

感觉这些写开源项目的作者写完之后,自己肯定没有做相关的时间测试,虽然我自己也没有做测试,于是自己按照自己的需求写了一个fcache.inc.php,放到git上了(https://github.com/hustcc/php-file-cache )

PHP File Cache, Used to instead of memcache in Host which can not install memcache.

1.one cache, one file, reduce calculate of key and value, to gain hign performaance.

2.simple api to use. similar with memcache.

使用很简单,Demo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
require_once ( 'fcache.inc.php' );
//example
$cache = new FCache();
$storeData = array (
       'time'   => time(),
       'str' => 'test' ,
       'int'   => 1321
);
$cache ->add( 'select * from table;' , $storeData );
$cache ->add( 'select * from table;' , $storeData );
$cache ->add( 'select * from table;' , $storeData );
$cache ->add( 'select * from table;' , $storeData );
print_r( $storeData = $cache ->get( 'select * from table;' ));
$cache -> delete ( 'select * from table;' );
print_r ( $cache ->get( 'select * from table;' ) ? 'exist' : 'has no cache' );
$cache ->add( 'select * from table1;' , 123);
$cache ->add( 'select * from table2;' , 234);
$cache ->add( 'select * from table3;' , 345);
$cache -> flush ();
print_r ( $cache ->get( 'select * from table3;' ) ? 'exist' : 'has no cache' );
?>
如果有其他的需求,请回复~Enjoy~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值