Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
831 florian 1
<?php
2
 
3
////////////////////////////////////////////////////////////////////////////////
4
//                                                                            //
5
//   Copyright (C) 2006  Phorum Development Team                              //
6
//   http://www.phorum.org                                                    //
7
//                                                                            //
8
//   This program is free software. You can redistribute it and/or modify     //
9
//   it under the terms of either the current Phorum License (viewable at     //
10
//   phorum.org) or the Phorum License that was distributed with this file    //
11
//                                                                            //
12
//   This program is distributed in the hope that it will be useful,          //
13
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
14
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
15
//                                                                            //
16
//   You should have received a copy of the Phorum License                    //
17
//   along with this program.                                                 //
18
////////////////////////////////////////////////////////////////////////////////
19
 
20
/*
21
 * Memcached-based caching-layer
22
 * Memcached -> http://www.danga.com/memcached/
23
 * using the pecl-module for accessing memcached
24
 * -> http://pecl.php.net/package/memcache/
25
 */
26
if(!defined("PHORUM")) return;
27
 
28
$PHORUM['memcache_obj'] = new Memcache;
29
$PHORUM['memcache_obj']->connect('127.0.0.1', 11211);
30
//$PHORUM['memcache_obj'] = memcache_connect('127.0.0.1', 11211);
31
 
32
 
33
 
34
/*
35
 * This function returns the cached data for the given key
36
 * or NULL if no data is cached for this key
37
 */
38
function phorum_cache_get($type,$key) {
39
	if(is_array($key)) {
40
		$getkey=array();
41
		foreach($key as $realkey) {
42
			$getkey[]=$type."_".$realkey;
43
		}
44
	} else {
45
		$getkey=$type."_".$key;
46
	}
47
 
48
    $ret=$GLOBALS['PHORUM']['memcache_obj']->get($getkey);
49
 
50
    // rewriting them as we need to strip out the type :(
51
    if(is_array($getkey)) {
52
    	$typelen=(strlen($type)+1);
53
    	foreach($ret as $retkey => $retdata) {
54
    		$ret[substr($retkey,$typelen)]=$retdata;
55
    		unset($ret[$retkey]);
56
    	}
57
    }
58
    if($ret === false || (is_array($ret) && count($ret) == 0))
59
    	$ret=NULL;
60
 
61
    return $ret;
62
 
63
}
64
 
65
/*
66
 * Puts some data into the cache
67
 * returns number of bytes written (something 'true') or false ...
68
 * depending of the success of the function
69
 */
70
function phorum_cache_put($type,$key,$data,$ttl=PHORUM_CACHE_DEFAULT_TTL) {
71
 
72
	$ret=$GLOBALS['PHORUM']['memcache_obj']->set($type."_".$key, $data, 0, $ttl);
73
    return $ret;
74
}
75
 
76
 
77
/*
78
 * Removes a key from the cache
79
 */
80
function phorum_cache_remove($type,$key) {
81
 
82
    $ret=$GLOBALS['PHORUM']['memcache_obj']->delete( $type."_".$key, 0);
83
 
84
    return $ret;
85
}
86
 
87
/*
88
 * Clears all data from the cache
89
 */
90
function phorum_cache_clear() {
91
 
92
    $ret=$GLOBALS['PHORUM']['memcache_obj']->flush();
93
 
94
    return $ret;
95
}
96
 
97
/*
98
 type can be nearly each value to specify a group of data
99
 used are currently:
100
 'user'
101
 'message'
102
*/
103
 
104
 
105
?>