Cache caching mechanism used in zendframework

Source: Internet
Author: User
Tags zend

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.}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.