Fix function level comments that start with /* not /**
[lhc/web/wiklou.git] / includes / objectcache / BagOStuff.php
1 <?php
2 /**
3 * Classes to cache objects in PHP accelerators, SQL database or DBA files
4 *
5 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Cache
25 */
26
27 /**
28 * @defgroup Cache Cache
29 */
30
31 /**
32 * interface is intended to be more or less compatible with
33 * the PHP memcached client.
34 *
35 * backends for local hash array and SQL table included:
36 * <code>
37 * $bag = new HashBagOStuff();
38 * $bag = new SqlBagOStuff(); # connect to db first
39 * </code>
40 *
41 * @ingroup Cache
42 */
43 abstract class BagOStuff {
44 private $debugMode = false;
45
46 public function setDebug( $bool ) {
47 $this->debugMode = $bool;
48 }
49
50 /* *** THE GUTS OF THE OPERATION *** */
51 /* Override these with functional things in subclasses */
52
53 /**
54 * Get an item with the given key. Returns false if it does not exist.
55 * @param $key string
56 */
57 abstract public function get( $key );
58
59 /**
60 * Set an item.
61 * @param $key string
62 * @param $value mixed
63 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
64 */
65 abstract public function set( $key, $value, $exptime = 0 );
66
67 /**
68 * Delete an item.
69 * @param $key string
70 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
71 */
72 abstract public function delete( $key, $time = 0 );
73
74 public function lock( $key, $timeout = 0 ) {
75 /* stub */
76 return true;
77 }
78
79 public function unlock( $key ) {
80 /* stub */
81 return true;
82 }
83
84 public function keys() {
85 /* stub */
86 return array();
87 }
88
89 /* *** Emulated functions *** */
90
91 public function add( $key, $value, $exptime = 0 ) {
92 if ( !$this->get( $key ) ) {
93 $this->set( $key, $value, $exptime );
94
95 return true;
96 }
97 }
98
99 public function replace( $key, $value, $exptime = 0 ) {
100 if ( $this->get( $key ) !== false ) {
101 $this->set( $key, $value, $exptime );
102 }
103 }
104
105 /**
106 * @param $key String: Key to increase
107 * @param $value Integer: Value to add to $key (Default 1)
108 * @return null if lock is not possible else $key value increased by $value
109 */
110 public function incr( $key, $value = 1 ) {
111 if ( !$this->lock( $key ) ) {
112 return null;
113 }
114
115 $value = intval( $value );
116
117 if ( ( $n = $this->get( $key ) ) !== false ) {
118 $n += $value;
119 $this->set( $key, $n ); // exptime?
120 }
121 $this->unlock( $key );
122
123 return $n;
124 }
125
126 public function decr( $key, $value = 1 ) {
127 return $this->incr( $key, - $value );
128 }
129
130 public function debug( $text ) {
131 if ( $this->debugMode ) {
132 $class = get_class( $this );
133 wfDebug( "$class debug: $text\n" );
134 }
135 }
136
137 /**
138 * Convert an optionally relative time to an absolute time
139 */
140 protected function convertExpiry( $exptime ) {
141 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
142 return time() + $exptime;
143 } else {
144 return $exptime;
145 }
146 }
147 }
148
149