aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/memcache.php
blob: 2df93cbaf720e44babf8dc43f8908af44f85b676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
	/**
	 * Elgg memcache support.
	 * 
	 * Requires php5-memcache to work.
	 * 
	 * @package Elgg
	 * @subpackage API
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd <info@elgg.com>
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 */


	class ElggMemcache extends ElggCache
	{
		/**
		 * Namespace variable used for key
		 *
		 * @var string
		 */
		private $namespace;
		
		/**
		 * Memcache object
		 */
		private $memcache;
		
		/**
		 * Expiry of saved items (defaults forever)
		 */
		private $expires = 0;
		
		/**
		 * The version of memcache running
		 */
		private $version = 0;
		
		/**
		 * Keys so far.
		 * This variable holds a list of keys we have seen so that when we call ->clear() we invalidate only those keys.
		 * TODO: Could this be done better?
		 */
		private $keys_so_far = array();
		
		/**
		 * Connect to memcache.
		 * 
		 * @param string $cache_id The namespace for this cache to write to - note, namespaces of the same name are shared!
		 */
		function __construct($namespace = 'default')
		{	
			global $CONFIG;
			
			$this->namespace = $namespace;
			
			// Do we have memcache?
			if (!class_exists('Memcache'))
				throw new ConfigurationException(elgg_echo('memcache:notinstalled'));

			// Create memcache object
			$this->memcache	= new Memcache;
			
			// Now add servers
			if (!$CONFIG->memcache_servers)
				throw new ConfigurationException(elgg_echo('memcache:noservers'));
				
			foreach ($CONFIG->memcache_servers as $server)
			{
				if (is_array($server))
				{
					$this->memcache->addServer(
						$server[0], 
						isset($server[1]) ? $server[1] : 11211,
						isset($server[2]) ? $server[2] : true,
						isset($server[3]) ? $server[3] : null,
						isset($server[4]) ? $server[4] : 1,
						isset($server[5]) ? $server[5] : 15,
						isset($server[6]) ? $server[6] : true
					);
					
				}
				else
					$this->memcache->addServer($server, 11211);
			}
			
			// Get version
			$this->version = $this->memcache->getversion();
			if (version_compare($this->version, '2.0.0', '<'))
				throw new ConfigurationException(sprintf(elgg_echo('memcache:versiontoolow'), $this->version));
			
			// Set some defaults
			if (isset($CONFIG->memcache_expires))
				$this->expires = $CONFIG->memcache_expires;
			
		}
		
		/**
		 * Combine a key with the namespace.
		 * Memcache can only accept <250 char key. If the given key is too long it is shortened.
		 * 
		 * @param string $key The key
		 * @return string The new key. 
		 */
		private function make_memcache_key($key)
		{
			$prefix = $this->namespace . ":";
			
			if (strlen($prefix.$key)> 250)
				$key = md5($key);
				
			return $prefix.$key;
		}
		
		public function save($key, $data) 
		{
			$key = $this->make_memcache_key($key);
			
			return $this->memcache->add($key, $data, null, $this->expires);	
		}
		
		public function load($key, $offset = 0, $limit = null)
		{
			$key = $this->make_memcache_key($key);
			
			return $this->memcache->get($key);
		}
		
		public function delete($key) 
		{
			$key = $this->make_memcache_key($key);
			
			return $this->memcache->delete($key, 0);
		}
		
		public function clear()
		{
			foreach ($this->keys_so_far as $key)
				$this->memcache->delete($key, 0);
				
			$this->keys_so_far = array();
			
			return true;
		}
	}
?>