关于PEAR的DB和MDB2方法对比

在从前的php4时代,我很喜欢使用pear的DB库,使用非常方便。但是现在pear官方站已经建议我们采用MDB2来代替它,虽然MDB2的前身可能有一些DB的影子,但是使用方法还是有一些不同,这些天一边使用,一边做些笔记。

MDB2 DB
queryAll getAll
queryRow getRow
queryCol getCol
queryOne getOne

其它方法貌似一致。

评论

memcache的几个旁支

最近留意了一下,memcache出现了几个旁支项目,很有一点意思,也许在日后的项目中可以用的上。

memcached-tag

给memcache增加了tag功能,新增的命令如下:

  • tag_add <tag> <key>
  • tag_delete <tag>

Memcached is a high-performance, distributed memory object caching system.

We add “Tag Function” for memcached. Propose is remove several keys with the same tag in one operation. This function will help the API programmers (such as php) do the delete operation easily, and reduce the network load. We use hash and splay tree, make the “tag_add” and “tag_delete” commands very quickly, and save memory as much as possible.

memcached for Win32

顾名思义,这个是一个在win32下跑的memcached,某些人用的上吧,也许…

This is a port of memcached to the win32 architecture by Kronuz
The win32 version of memcached can be run both as a NT Service or from the command line.

memcachedb

新浪互动社区技术团队提供的一个memcache版本。我们知道memcache是将数据保存在内存里的,要是停机或重启,biu的一下,就什么都没了,虽然这能满足cache的需要,但是如果有一个key-value的持久化存储db,也是不错的。

memcachedb就是这样一个基于memcache + berkeley db的持久存储,仍然可以使用之前的memcache client api,比如这些方法:

  • get(also mutiple get)
  • set, add, replace
  • incr, decr
  • delete
  • stats

我测试了一下,还不支持flush奥,但是没有关系,问题不大。测试的时候,读写速度相当快。据memcachedb的主页上说,这个不是拿来作为一个cache的解决方案的,而是一个快速的key-value持久存储db。

Memcachedb is a distributed key-value storage system designed for persistent. It is NOT a cache solution, but a persistent storage engine for fast and reliable key-value based object storage and retrieval. It conforms to memcache protocol(not completed, see below), so any memcached client can have connectivity with it. Memcachedb uses Berkeley DB as a storing backend, so lots of features including transaction and replication are supported.

评论

如何避免使用php的require_once

我们知道,在php中使用require_once/include_once虽然方便,但是代价昂贵,据测试数据来看,require_once比require慢3-4倍,所以在php开发中,我们应该尽量使用require/include。

列一下俺常用的避免require/include的方法。

使用__autoload

php5可以使用__autoload来避免require,用的好的话,代码里头甚至看不到几个require,实在是安逸啊。测试结果表明,使用__autoload之后的new Foo;require_once 'foo.php'; new Foo; 大概要快3倍左右。

使用defined检测是否载入过

在代码开头使用defined检测是否定义过对应的常量,如果有的话,直接return。

PHP:
  1. <?php
  2. if(!defined('_MYCLASS_'))
  3.     return;
  4.  
  5. define('_MYCLASS_', 1);
  6. class MyClass { ... }
  7. ?>

测试了一下,defined的性能也不是太好...

require前检查

用class_exists或者function_exists检查一下,确认没有载入过再出手,至少比require_once能快上3倍。php4也可以用上。

CODE:
  1. class_exists('myClass') or require('/path/to/myClass.class.php');

评论 (2)

使用PDO的一些备忘

之前在论坛上灌水的时候,也曾经看到别人提到过PDO的一些生僻用法。但是当时觉得短期内不会用上,所以不是太在意。等到要用的时候,满世界也找不到出处。

这使我下定决心,做点PDO的小笔记,慢慢补。

设定PDO的fetchMode

初始化pdo的时候,就设定好PDO的fetchMode,应该能省点事,比如我最喜欢的fetchMode是FETCH_OBJ。

PHP:
  1. $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO:FETCH_OBJ);

其中PDO::ATTR_DEFAULT_FETCH_MODE是php 5.2.0之后才新增的常量

还可以在实例化PDO对象的时候就完成这个设定:

PHP:
  1. $dbh = new PDO("mysql:dbname=dbname", "user", "password",
  2.     array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO:FETCH_OBJ));

评论

wordpress plugin - search engine related posts

Table of content

Description

中文说明

当用户从google搜索到你的博客上,这个wordpress 插件会根据用户搜索的关键词显示你的博客上更多的相关内容。目前也只对google生效,下一个版本将对baidu生效。

这个插件完全使用javascript加上google的api完成,不需要占用服务器端的资源,环保且安全。

English version

When someone is referred from a search engine like Google, the plugin show your blog content matched the terms they search for.

Usage

中文说明:

  • 解压
  • 在wordpress模板中添加一个id为search_content的html标签,比如
    CODE:
    1. <div id="search_content" style="display:none;">
    2. <h1>相关搜索结果</h1>
    3. </div>

  • 复制search_related_posts.php到wp-contents/plugin目录,并激活插件.
  • done! 现在你可以试着从google搜索上先搜到自己的博客,然后点击进去看看插件的效果。

English version:

  • unzip it
  • Put <div style="display:none" id="search_content"></div> at the place in your template where you want the list of related posts
  • Copy search_related_posts.php to direcotry wp-contents/plugin and activate the plugin.
  • done!

Download

latest search engine related posts

History

评论 (3)

深切怀念php的short_tag

最近两年干的php所有活儿,都是在php的short_tag关闭的情况下完成的。我也不爱使用那些乱七八糟的template engine,没意义,所以代码里面会有很多php的标签,看着有点晕,这时候会怀念当年php的short_tag,简单是美,短小精悍...应该也是美的吧?

贴个形式对比:

现在使用的

PHP:
  1. I have <?php echo $foo?> foo.

从前的好日子

PHP:
  1. I have <?=$foo?> foo.

可以少写9个字节,要是代码按字节算钱,boss应该可以省下一小笔。

评论 (4)

AIR入门者推荐阅读 — AIR for JavaScript Developers

O'reilly的电子书 AIR for JavaScript 更新了。可以在这里免费下载

这本书按照 创造共用方式授权,这意味着你不仅仅可以合法的免费下载,还可以按照自己的意愿做些修改。我翻看了一些章节,这本书对于AIR的html + js开发者来说,是一本很好的入门教程。这次更新的内容还包括AIR的安全模型介绍。

评论 (1)