/[drupal]/contributions/modules/memcache/dmemcache.inc
ViewVC logotype

Contents of /contributions/modules/memcache/dmemcache.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue May 22 08:58:15 2007 UTC (2 years, 6 months ago) by robertDouglass
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-5--1-0-BETA2, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
refactored all dmemcache functions into separate include
1 <?php
2 // $Id$
3
4 /*
5 * Core dmemcache functions required by:
6 * memcache.inc
7 * memcache.db.inc
8 * session-memcache.inc
9 * session-memcache.db.inc
10 */
11
12 global $_memcache_statistics;
13 $_memcache_statistics = array('get' => array(), 'set' => array(), 'hit' => array());
14
15 /*
16 * A memcache API for Drupal.
17 */
18
19 /**
20 * Place an item into memcache
21 *
22 * @param $key The string with with you will retrieve this item later.
23 * @param $value The item to be stored.
24 * @param $exp Parameter expire is expiration time in seconds. If it's 0, the item never expires
25 * (but memcached server doesn't guarantee this item to be stored all the time, it could be
26 * deleted from the cache to make place for other items).
27 * @param $bin The name of the Drupal subsystem that is making this call. Examples could be
28 * 'cache', 'alias', 'taxonomy term' etc. It is possible to map different $bin values to
29 * different memcache servers.
30 *
31 * @return bool
32 */
33 function dmemcache_set($key, $value, $exp = 0, $bin = 'cache') {
34 global $_memcache_statistics;
35 $_memcache_statistics['set'][] = $key;
36 $_memcache_statistics['bins'][] = $bin;
37 if ($mc = dmemcache_object($bin)) {
38 $full_key = dmemcache_key($key, $bin);
39 if (!$mc->set($full_key, $value, TRUE, $exp)) {
40 watchdog('memcache', 'Failed to set key: ' . $full_key, WATCHDOG_ERROR);
41 }
42 else {
43 return TRUE;
44 }
45 }
46 return FALSE;
47 }
48
49 /**
50 * Retrieve a value from the cache.
51 *
52 * @param $key The key with which the item was stored.
53 * @param $bin The bin in which the item was stored.
54 *
55 * @return The item which was originally saved or FALSE
56 */
57 function dmemcache_get($key, $bin = 'cache') {
58 global $_memcache_statistics;
59 $_memcache_statistics['get'][] = $key;
60 $_memcache_statistics['bins'][] = $bin;
61 if ($mc = dmemcache_object($bin)) {
62 $full_key = dmemcache_key($key, $bin);
63 $result = $mc->get($full_key);
64 if ($result) {
65 $_memcache_statistics['hit'][] = $key;
66 }
67 return $result;
68 }
69 }
70
71 /**
72 * Deletes an item from the cache.
73 *
74 * @param $key The key with which the item was stored.
75 * @param $bin The bin in which the item was stored.
76 *
77 * @return Returns TRUE on success or FALSE on failure.
78 */
79 function dmemcache_delete($key, $bin = 'cache') {
80 if ($mc = dmemcache_object($bin)) {
81 $full_key = dmemcache_key($key, $bin);
82 return $mc->delete($full_key);
83 }
84 return FALSE;
85 }
86
87 /**
88 * Immediately invalidates all existing items. dmemcache_flush doesn't actually free any
89 * resources, it only marks all the items as expired, so occupied memory will be overwritten by
90 * new items.
91 *
92 * @param $bin The bin to flush. Note that this will flush all bins mapped to the same server
93 * as $bin. There is no way at this time to empty just one bin.
94 *
95 * @return Returns TRUE on success or FALSE on failure.
96 */
97 function dmemcache_flush($bin = 'cache') {
98 if ($mc = dmemcache_object($bin)) {
99 return $mc->flush();
100 }
101 }
102
103 function dmemcache_stats($bin = 'cache', $type = '') {
104 if ($mc = dmemcache_object($bin)) {
105 return $mc->getExtendedStats($type);
106 }
107 }
108
109 /**
110 * Returns an Memcache object based on the bin requested. Note that there is
111 * nothing preventing developers from calling this function directly to get the
112 * Memcache object. Do this if you need functionality not provided by this API
113 * or if you need to use legacy code. Otherwise, use the dmemcache (get, set,
114 * delete, flush) API functions provided here.
115 *
116 * @param $bin The bin which is to be used.
117 *
118 * @param $flush Rebuild the bin/server/cache mapping.
119 *
120 * @return an Memcache object or FALSE.
121 */
122 function dmemcache_object($bin = NULL, $flush = FALSE) {
123 static $memcacheCache = array(), $memcache_servers, $memcache_bins;
124
125 if ($flush) {
126 foreach ($memcacheCache as $cluster) {
127 $cluster->close();
128 }
129 $memcacheCache = array();
130 }
131
132 if (empty($memcacheCache) || empty($memcacheCache[$bin])) {
133 // $memcache_servers and $memcache_bins originate from settings.php.
134 // $memcache_servers_custom and $memcache_bins_custom get set by
135 // memcache.module. They are then merged into $memcache_servers and
136 // $memcache_bins, which are statically cached for performance.
137 if (empty($memcache_servers)) {
138 // Values from settings.php
139 $memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
140 $memcache_bins = variable_get('memcache_bins', array('cache' => 'default'));
141 }
142
143 // If there is no cluster for this bin in $memcache_bins, cluster is 'default'.
144 $cluster = empty($memcache_bins[$bin]) ? 'default' : $memcache_bins[$bin];
145
146 // If this bin isn't in our $memcache_bins configuration array, and the
147 // 'default' cluster is already initialized, map the bin to 'cache' because
148 // we always map the 'cache' bin to the 'default' cluster.
149 if (empty($memcache_bins[$bin]) && !empty($memcacheCache['cache'])) {
150 $memcacheCache[$bin] = &$memcacheCache['cache'];
151 }
152 else {
153 // Create a new Memcache object. Each cluster gets its own Memcache object.
154 $memcache = new Memcache;
155 // A variable to track whether we've connected to the first server.
156 $init = FALSE;
157
158 // Link all the servers to this cluster.
159 foreach ($memcache_servers as $s => $c) {
160 if ($c == $cluster) {
161 list($host, $port) = explode(':', $s);
162
163 // This is a server that belongs to this cluster.
164 if (!$init) {
165 // First server gets the connect.
166 if (@$memcache->addServer($host, $port)) {
167 // Only set init to true if a connection was made.
168 $init = TRUE;
169 }
170 }
171 else {
172 // Subsequent servers gett addServer.
173 @$memcache->addServer($host, $port);
174 }
175 }
176 }
177
178 if ($init) {
179 // Map the current bin with the new Memcache object.
180 $memcacheCache[$bin] = $memcache;
181
182 // Now that all the servers have been mapped to this cluster, look for
183 // other bins that belong to the cluster and map them too.
184 foreach ($memcache_bins as $b => $c) {
185 if ($c == $cluster && $b != $bin) {
186 // Map this bin and cluster by reference.
187 $memcacheCache[$b] = &$memcacheCache[$bin];
188 }
189 }
190 }
191 }
192 }
193
194 return empty($memcacheCache[$bin]) ? FALSE : $memcacheCache[$bin];
195 }
196
197 function dmemcache_key($key, $bin = 'cache') {
198 static $prefix;
199 // memcache_key_prefix can be set in settings.php to support site namespaces
200 // in a multisite environment.
201 if (empty($prefix)) {
202 $prefix = variable_get('memcache_key_prefix', '');
203 }
204 $full_key = ($prefix ? $prefix. '-' : '') . $bin . '-' . $key;
205 return urlencode($full_key);
206 }

  ViewVC Help
Powered by ViewVC 1.1.2