手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

phpQuery的一个小疑问。

最近在使用phpQuery处理xml的时候发生了一点小问题。由于xml还算是比较规范,但我又不想用simplexml_load_string,所以就偷懒用phpQuery处理了。
但在处理的时候发现一个问题,比如我要处理的内容是:

XML/HTML代码
  1. <title>揭秘日本巨型OLED地球仪:实时显示地球变化</title>  
  2. <link>http://news.dili360.com/gclw/xxjs/2012/0312/32136.shtml</link>  
  3. <description><a href='http://news.dili360.com/gclw/xxjs/2012/0312/32136.shtml'><img src='http://image.dili360.com/news/gclw/xxjs/2012/0312/36_5132136203_20120312094816.jpg' style='border: 1px solid #000000;'/></a>   新浪科技讯 北京时间3月12日消息,据国外媒体报道,如果你前往日本东京,别忘了去参观一下“未来科学馆”(Miraikan),这里展示着一些最尖端的技术成就。就在去年年中,这里揭幕了全世界首个大型OLED显示屏,直径超过19英尺(约合5.8米)。尽管名叫“Geo-Cosmos”,但这并非一般的地球仪,它几乎能实时显示我们这颗星球上正在发生的一切!全球各地的科学家和研究机构将数据发送给Geo-Cosmos,后者将其呈现给观众。   这..</description>  
  4. <category>中国国家地理网地理资讯频道</category>  
  5. <author>dili360.com</author>  
  6. <pubdate>2012-03-12 09:48:16</pubdate>  

请看加红的那一段。
当我用phpQuery处理完后,发现,右边的</link>不见了。其他元素都正常。我的心一下子就碎了。
开始以为页面有问题,但怎么处理都是这样,最终只能将link换成了url来 进行处理。说实话,心是哇凉哇凉的。。

但是,同事在win下面就没有这个问题,我在ubuntu下就有这个问题。(我现在不知道是否我的PHP版本有问题,还是平台的问题,也没有心思深究了)【同事是5.3.9,我是5.3.6】,

Tags: phpquery

Using Redis as a backend for Active Record

说白了,yiiredis中的AR功能的model其实就是一个hash,只是稍作封装了而已

官方有例子:

Using Redis as a backend for Active Record

It is possible to store active record like structures in redis using ARedisRecord.

Note: this is experimental functionality and may be subject to change

$record = ARedisRecord::model()->findByPk(1); // loads a record with a unique id of 1 $record->name = "a test name"; // sets the name attribute on the record $record->somethingElse = "some other value"; $record->save(); // saves the record to redis $record->delete(); // deletes the record from redis

不过,上面的例子不能直接拿来用,因为ARedisRecord是一个abstract类,所以你得写一个类继承于他才行。
顺便,如果用setAttributes的时候,记得加第二个参数(除非你写了rules),否则一定要加第二个参数,不然save不成功哦。
昨天被它折腾了好久才发现
----
笔记而已

Tags: redis, yiiredis, phpredis

开始启用Yii的redis插件

该死的command+左箭头。。。写了十分钟的东西全没了。
-------重新开始-----
由于目前的一个项目涉及到的数据库需要跨库跨表操作,而且该库会被频繁的插入、更新、删除,所以相对速度会较慢。但查询量又较大,在一台服务器的情况下,怎么办?分端口主从吧,意义不大,因为某些操作会导致CPU瞬间100%。
这时候我想到了key/value的数据库,想用它来做中间处理,比如大量的内容先经过它,再真正入库,毕竟我不需要过分实时,也不涉及到金钱交易。于是乎就在redis和mongo中间犹豫了。
在这期间咨询了三个人:老王(基于博客http://huoding.com/2012/02/29/146,基于Redis消息系统实现);11爷(redis有pub/sub功能);烂桔(mongo不太适用于单机,redis有内存模式)

基于上述原因,于是选择了redis,那么就开始我的Redis之旅吧
1、安装 Redis ,参考:http://library.linode.com/databases/redis/ubuntu-10.10-maverick(如果我没记错,这应该是11爷推荐的地址,他本来推荐的是Centos下的,但我用的是ubuntu,所以就参考这个了)
  先来三个常规操作

XML/HTML代码
  1. apt-get update  
  2. apt-get upgrade  
  3. apt-get install build-essential  

 完事之后,接着:

XML/HTML代码
  1. cd /opt/  
  2.     mkdir /opt/redis  
  3. wget http://redis.googlecode.com/files/redis-2.4.8.tar.gz  
  4. tar -zxvf /opt/redis-2.4.8.tar.gz  
  5. cd /opt/redis-2.4.8/  
  6. make  

make完后做如下操作,将一些配置文件拷贝到/opt/redis目录下:

XML/HTML代码
  1. cp /opt/redis-2.4.8/redis.conf /opt/redis/redis.conf.default  
  2. cp /opt/redis-2.4.8/src/redis-benchmark /opt/redis/  
  3. cp /opt/redis-2.4.8/src/redis-cli /opt/redis/  
  4. cp /opt/redis-2.4.8/src/redis-server /opt/redis/  
  5. cp /opt/redis-2.4.8/src/redis-check-aof /opt/redis/  
  6. cp /opt/redis-2.4.8/src/redis-check-dump /opt/redis/  
  7. cp /opt/redis/redis.conf.default /opt/redis/redis.conf  

接着,更新/opt/redis/redis.conf:

XML/HTML代码
  1. daemonize yes  
  2. pidfile /var/run/redis.pid  
  3. logfile /var/log/redis.log  
  4.   
  5. port 6379  
  6. bind 127.0.0.1  
  7. timeout 300  
  8.   
  9. loglevel notice  
  10.   
  11. ## Default configuration options  
  12. databases 16  
  13.   
  14. save 900 1  
  15. save 300 10  
  16. save 60 10000  
  17.   
  18. rdbcompression yes  
  19. dbfilename dump.rdb  
  20.   
  21. dir /opt/redis/  
  22. appendonly no  

以上是linode里的配置,你可以参考一下做处理。大部分都一样,但timeout/loglevel/dir这三个参数不太一样。可以看着注释改一下。 linode 中有一个:glueoutputbuf yes ,这个在2.4.8下面会出错,所以我就没有启用它。
接着是加入启用脚本,既然linode有配置参考,那么它就有启动脚本,默认redis是没有给你这些启动脚本的,所以偷懒一下吧,下载linode的脚本:

XML/HTML代码
  1. cd /opt/  
  2. wget -O init-deb.sh http://library.linode.com/assets/630-redis-init-deb.sh  
  3. adduser --system --no-create-home --disabled-login --disabled-password --group redis  
  4. mv /opt/init-deb.sh /etc/init.d/redis  
  5. chmod +x /etc/init.d/redis  
  6. chown -R redis:redis /opt/redis  
  7. touch /var/log/redis.log  
  8. chown redis:redis /var/log/redis.log  
  9. update-rc.d -f redis defaults  

在这一步之后,你就可以直接用/etc/init.d/redis 来 start和stop redis服务了。

2、安装phpredis(https://github.com/nicolasff/phpredis)
在安装之前,先看readme:https://github.com/nicolasff/phpredis/blob/master/README.markdown
看完readme之后你会发现,原来一切是这样的简单啊
OK,先wget 回源代码,然后tar解开目录。。
接着3步搞定:

XML/HTML代码
  1. phpize  
  2. ./configure  
  3. make && make install  

速度超快,make install后,会告诉你redis.so生成在哪里,你将这extension=redis.so,加入到你的php.ini中即可。

3、下载YiiRedis项目。(由于我是用Yii的,所以直接用这个插件了,否则,还是参考一下phpredis的一些用法)
同样,在这里有readme:https://github.com/phpnode/YiiRedis/blob/master/README.md
项目地址:https://github.com/phpnode/YiiRedis(请自行下载)
使用方法也很简单,和db一样,在components下加一个组件:

XML/HTML代码
  1. "components" => array(  
  2.     "redis" => array(  
  3.         "class" => "packages.redis.ARedisConnection",  
  4.         "hostname" => "localhost",  
  5.         "port" => 6379  
  6.     ),  
  7.     //...  
  8. ),  

加完它之后就可以写上一段测试代码了:

PHP代码
  1. Yii::import("ext.yiiredis.*");  
  2. $list = new ARedisList("aNameForYourListGoesHere");  
  3. $list->add("cats");  
  4. $list->add("dogs");  
  5. $list->add("goods");  
  6. foreach($list as $i => $val) {  
  7.     print_r($val) ;  
  8.     echo "<br />";  
  9. }  
  10. $list->clear(); // delete the list  

直接输出在浏览器中。是不是很HIGH?
------
OK,就介绍到这里,请试用吧。

=------

备注:

$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);   // don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);    // use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);   // use igBinary serialize/unserialize

$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys

由于redis默认不做序列化,因此如果要存储PHP的数据,如:数组、对象,那就必须用第二个setOption进行设置,以便 让数据自动序列化

Tags: redis, yiiredis, phpredis

Yii Framework DB 中的一些参数

Yii的AR一直在被使用,当然,偶尔也只是看看其中的参数,都是直接把main.php中的数据注释掉就完事了
但事实上我们都忽略了一点点现实,那就是默认参数是没有什么性能提升的。

1、'schemaCachingDuration'=>3600,
这个在默认参数中是不存在的,如果你的表结构不太变动,完全可以不需要每次都show columns,show create table之类的。加上这个参数,可以将表结构缓存一小时(嗯,前提是,你配置了cache组件)

2、emulatePrepare=true ,这个是默认里面就带有的,它有什么用呢?
官方是这样说的:whether to turn on prepare emulation. Defaults to false, meaning PDO will use the native prepare support if available. For some databases (such as MySQL), this may need to be set true so that PDO can emulate the prepare support to bypass the buggy native prepare support. Note, this property is only effective for PHP 5.1.3 or above.
好吧,为了以防万一,还是打开算了

3、'enableProfiling'=>true
如果你想优化代码的话,可以尝试设置为true看看。官方在guide里说:By setting CDbConnection::enableProfiling to be true in the application configuration, every SQL statement being executed will be profiled. The results can be readily displayed using the aforementioned CProfileLogRoute, which can show us how much time is spent in executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the total number SQL statements executed and their total execution time.

4、 'enableParamLogging'=>true,
这个就相对比较简单了,如果你设置为True,你在log中,就可以看到你的每次参数的参数是什么了,而不是:y01:y02这样的顺序变量。

5、tablePrefix = "xxx",这没什么好解释的。。。

6、charset="xxx" ,也没有什么好解释的,utf-8的话,中间的减号不要。。


这些都是常用的。。所以列出来

Tags: yii

PHP 5.4.0 released!

[01-Mar-2012]

The PHP development team is proud to announce the immediate availability of PHP 5.4.0. This release is a major leap forward in the 5.x series, which includes a large number of new features and bug fixes.

Some of the key new features include: traits, a shortened array syntax, a built-in webserver for testing purposes and more. PHP 5.4.0 significantly improves performance, memory footprint and fixes over 100 bugs.

For users upgrading from PHP 5.3 there is a migration guide available here, detailing the changes between those releases and PHP 5.4.0.

Further details about the PHP 5.4.0 release can be found in the release announcement, and the full list of changes are available in the ChangeLog.

Please note that it may take a while until the release is available on all mirrors.

----------

你还在犹豫什么?而且,传说中性能提高了超级多,20倍?50倍?太夸张了,不过提升一点点我还是可信的

diogin在大致浏览了下,稍作了整理:

增加  cli-server SAPI
FPM SAPI 增加 process.max 配置项
增加 http_response_code(), get_declared_traits(), trait_exists(), header_register_callback(), class_uses() 函数
增加 Closure::bind(), Closure::bindTo() 方法
mysql, mysqli 扩展默认使用 mysqlnd
增加 trait
增加关键字 callable,insteadof
E_ALL 包含了 E_STRICT
增加了 <?=
支持 (new Foo)->bar()
支持 0b001001101
支持 $a = [1, 2, 3, 4]; $b = ['one' => 1, 'two' => 2, 'three' => 3];
支持 Class::{expr}()
支持 foo()[0]
闭包里支持 $this 引用
continue $v,break $v 不再支持
缺省时区设为 UTC
缺省编码设为 UTF-8

------

一个MINI的测试server还是有点用的。难道它也想象nodejs那样?谁知道呢