Discuz教程网

PHP缓存类PHP-Cache-Kit

[复制链接]
authicon dly 发表于 2011-1-7 13:55:15 | 显示全部楼层 |阅读模式

  1. <?php
  2. // PHP-Cache-Kit is an example of how to add modular caching to your PHP projects
  3. // It is free for all use, but please do not re-distribute without this header.
  4. // Most recent version available from [url]http://acme-web-design.info/php-cache-kit.html[/url]
  5. // Send suggestions to [email]info@acme-web-design.info[/email]


  6. /*
  7.   // uses two global configuration variables: cache_active, cache_folder

  8.   // To use: set cache_active = true, assign cache_folder
  9.   // Next, call acmeCache::fetch instead of generating a page or module.
  10.   // Only if it returns false, build the section then call acmeCache::save.

  11.   // You only need these two functions and you do not need to
  12.   // create an object -- the class wrapping is just to avoid namespace
  13.   // conflicts with your existing code.

  14.   // Usage example:

  15.   // Do this stuff in your config file
  16.   include_once(\'cache-kit.php\');
  17.   $cache_active = true;
  18.   $cache_folder = \'cache/\';

  19.   // Now you can convert any time-consuming but rarely-changing data module into
  20.   // a fast cached module. This example rebuilds the calendar only every 5 minutes.
  21.   function helloWorld(){
  22.    $result = acmeCache::fetch(\'helloWorld\', 10); // 10 seconds
  23.    if(!$result){
  24.     $result = \'<h2> Hello world</h2> <p>Build time: \'.date("F j, Y, g:i a").\'</p>\';
  25.     acmeCache::save(\'helloWorld\', $result);
  26.    } else echo(\'Cached result<br/>\');
  27.    return $result;
  28.   }

  29.   // now use the content module function just like you normally would -- caching is automatic!
  30.   echo(helloWorld());

  31. */


  32. class acmeCache{

  33. // public functionality, acmeCache::fetch() and acmeCache::save()
  34. // =========================

  35. function fetch($name, $refreshSeconds = 0){
  36.   if(!$GLOBALS[\'cache_active\']) return false;
  37.   if(!$refreshSeconds) $refreshSeconds = 60;
  38.   $cacheFile = acmeCache::cachePath($name);
  39.   if(file_exists($cacheFile) and
  40.    ((time()-filemtime($cacheFile))< $refreshSeconds))
  41.    $cacheContent = file_get_contents($cacheFile);
  42.   return $cacheContent;
  43. }

  44. function save($name, $cacheContent){
  45.   if(!$GLOBALS[\'cache_active\']) return;
  46.   $cacheFile = acmeCache::cachePath($name);
  47.   acmeCache::savetofile($cacheFile, $cacheContent);
  48. }

  49. // for internal use
  50. // ====================
  51. function cachePath($name){
  52.   $cacheFolder = $GLOBALS[\'cache_folder\'];
  53.   if(!$cacheFolder) $cacheFolder = trim($_SERVER[\'DOCUMENT_ROOT\'],\'/\').\'/cache/\';
  54.   return $cacheFolder . md5(strtolower(trim($name))) . \'.cache\';
  55. }

  56. function savetofile($filename, $data){
  57.   $dir = trim(dirname($filename),\'/\').\'/\';
  58.   acmeCache::forceDirectory($dir);
  59.   $file = fopen($filename, \'w\');
  60.   fwrite($file, $data); fclose($file);
  61. }

  62. function forceDirectory($dir){ // force directory structure
  63.   return is_dir($dir) or (acmeCache::forceDirectory(dirname($dir)) and mkdir($dir, 0777));
  64. }

  65. }
  66. ?>[/php]示例:[php]<?php

  67.   // Usage example:

  68.   // Do this stuff in your config file
  69.   include_once(\'cache-kit.php\');
  70.   $cache_active = true;
  71.   $cache_folder = \'cache/\';

  72.   // Now you can convert any time-consuming but rarely-changing data module into
  73.   // a fast cached module. This example rebuilds the calendar only every 5 minutes.
  74.   function helloWorld(){
  75.    $result = acmeCache::fetch(\'helloWorld\', 10); // 10 seconds
  76.    if(!$result){
  77.     $result = \'<h2> Hello world</h2> <p>Build time: \'.date("F j, Y, g:i a").\'</p>\';
  78.     acmeCache::save(\'helloWorld\', $result);
  79.    } else echo(\'Cached result<br/>\');
  80.    return $result;
  81.   }

  82.   // now use the content module function just like you normally would -- caching is automatic!
  83.   echo(helloWorld());

  84. ?>
复制代码







上一篇:Smarty缩图函数
下一篇:比较全的PHP编码转换类
authicon Cute宝贝儿 发表于 2011-6-23 05:09:36 | 显示全部楼层
不错不错,我喜欢
authicon №小乖 发表于 2011-6-25 00:59:58 | 显示全部楼层
顶啦,不错吧
authicon kikiya11 发表于 2011-6-27 00:59:59 | 显示全部楼层
不错不错,我喜欢
authicon 婷婷爱牛牛 发表于 2011-8-10 02:00:02 | 显示全部楼层
看一下啊,嘻嘻
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 08:37

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表