Build the project as shown in the following figure:
The code is as follows:
1.<?php
2.
3./**
4. * Indexcontroller-the Default Controller class
5. *
6. * @author
7. * @version
8. */
9.
10.require_once ' zend/controller/action.php ';
11.require_once ' zend/cache.php ';
12.require_once ' zend/registry.php ';
13.require_once ' zend/db.php ';
14.require_once ' zend/db/table.php ';;
15.
16.class Indexcontroller extends Zend_controller_action
17.{
Public Function init ()
19. {
$params = Array (' host ' => ' localhost '),
' username ' => ' root ',
' Password ' => ' root ',
' dbname ' => ' MySQL ');
$db = zend_db::factory (' Pdo_mysql ', $params);
Zend_db_table::setdefaultadapter ($DB);
Zend_registry::set (' db ', $db);
27.}
28.
Public Function indexaction ()
30. {
$frontendOptions = Array (
' LifeTime ' => 7200,//two-hour cache lifetime
' Automatic_serialization ' => true
34.);
35.
$backendOptions = Array (
Cache_dir ' => ' c:/tmp/'//cache directory of cached files
38.);
39.
40.//Get a Zend_cache_core object
$cache = zend_cache::factory (' Core ',
' File ',
$frontendOptions,
$backendOptions);
if (! $result = $cache->load (' Myresult ')) {
46.//Cache not hit, connect to database
$dbAdapter = Zend_registry::get (' db ');
$result = $dbAdapter->query (' SELECT * from user ')->fetchall ();
$cache->save ($result, ' myresult ');
echo "This one's from database!<br/>";
A.} else {
hit!//cache Shout so we know
echo "This one's from cache!<br/>";
54.}
Print_r ($result);
56.}
57.}