source: spip-zone/_plugins_/xcache/inc/xcache.php @ 32432

Last change on this file since 32432 was 32432, checked in by fil@…, 11 years ago

filecache.inc et nocache.inc

File size: 1.0 KB
Line 
1<?php
2
3# xcache ?
4if (function_exists('xcache_set'))
5        require_once dirname(__FILE__).'/'.'xcache.inc';
6else
7        require_once dirname(__FILE__).'/'.'filecache.inc';
8
9
10//
11// Cache a function's result cache_me()
12// (c) Fil 2009 - Double-licensed under the GNU/LGPL and MIT licenses
13// http://zzz.rezo.net/-SPIP-
14// $ttl = time to live
15// $vars = other variables that could change the result
16// (the function's variables are automatically taken into account)
17//
18// Usage: require_once 'xcache.php';
19// In any cacheable function add at top:
20// if(!is_null($c=cache_me())) return$c;
21if (!function_exists('debug_backtrace')) {
22        function cache_me() {return;}
23} else {
24        function cache_me($vars=null, $ttl=3600) {
25                $trace = debug_backtrace();
26                $trace = $trace[1];
27                $key = md5(
28                        $trace['function']
29                        .serialize($trace['args'])
30                        .serialize($vars)
31                );
32                if (!cache_isset($key)) {
33                        cache_set($key, null, $ttl);
34                        $r = call_user_func_array($trace['function'], $trace['args']);
35                        cache_set($key, $r, $ttl);
36                        return $r;
37                }
38                return cache_get($key);
39        }
40}
41
42?>
Note: See TracBrowser for help on using the repository browser.