简易的Cache系统

Posted by Cofyc, on June 13, 2008, 10:06 pm

不想使用庞大的Zend Framework,就模仿Zend_Cache写了一个简易的Cache系统。一个文件,共247行,功能、用法与Zend_Cache类似。

支持三种常用的缓存机制:File和Memcache以及Apc。

支持两种缓存方法:变量(Variables)和输出(Output)缓存。

不支持tags。

代码:http://pastebin.org/45146

Update:

0.1 permission issues fixed

0.2 Apc cache backend added

 

用法如下:

1. 配置

1.1 使用文件缓存(File Cache)

    $frontend_options = array(
        'backend' => 'file'
    );
    $backend_options = array(
        'lifetime' => '3600',
        'hash_level' => 1,
        'cache_dir' => '/path/to/cache_dir'
    );

 

1.2 使用Memcache模块

    $frontend_options    = array(
        'backend' => 'memcache'
    );
   
    $backend_options = array(
        'lifetime' => '3600',
        'compression' => false,
        'servers' => array(
                array('host' => '127.0.0.1',
                        'port' => 11211,
                        'persistent' => true
                    )
                )
    );

1.3 使用Apc extensions

    $frontend_options    = array(
        'backend' => 'apc''
    );

    $backend_options = array();

 

2. 使用

$cache = new Cache($frontend_options, $backend_options);

2.1 缓存variables

    if ($output = $cache->load($opt['id'])) {
    } else {
        $output = $this->outputing();
        $cache->save($output);
    }
    echo $output;

2.2 缓存output buffer

    if (!$cache->start($opt['id'])) {
        // output as usual:
        echo $this->outputing();
        $cache->end();

    }

2 comments  - Tags: zend framework, cache, file, memcache, output

comments

1. June 25, 2008, 9:30 pm by muxi

这个很不错,我最近也在研究zendframe,感觉就像在看Java的包、类库一样,太多了,不知道你是怎么学的,能不能给个建议?

2. June 26, 2008, 7:55 pm by Cofyc

我说一下我的经验吧。
ZF的每个component都一套不断在完善的设计理论,所以在看代码时,就必须先知道这个component里面的设计如何,有那些classes共同实现它的每个功能。
这些清楚了并懂得factory等机制,看代码就很容易了,因为zf的一个文件就是一个类。

Post a comment

Reply

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License
Powered by Project Neverland, Theme modified from gluedideas