Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Container for all Memcache-based cache methods. Inherits additional methods from <CacheCore>. Adheres
4
 * to the ICacheCore interface.
5
 *
6
 * @version 2012.01.28
7
 * @copyright 2006-2012 Ryan Parman
8
 * @copyright 2006-2010 Foleeo, Inc.
9
 * @copyright 2012 Amazon.com, Inc. or its affiliates.
10
 * @copyright 2008-2010 Contributors
11
 * @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
12
 * @link http://github.com/skyzyx/cachecore CacheCore
13
 * @link http://getcloudfusion.com CloudFusion
14
 * @link http://php.net/memcache Memcache
15
 * @link http://php.net/memcached Memcached
16
 */
17
class CacheMC extends CacheCore implements ICacheCore
18
{
19
	/**
20
	 * Holds the Memcache object.
21
	 */
22
	var $memcache = null;
23
 
24
	/**
25
	 * Whether the Memcached extension is being used (as opposed to Memcache).
26
	 */
27
	var $is_memcached = false;
28
 
29
 
30
	/*%******************************************************************************************%*/
31
	// CONSTRUCTOR
32
 
33
	/**
34
	 * Constructs a new instance of this class.
35
	 *
36
	 * @param string $name (Required) A name to uniquely identify the cache object.
37
	 * @param string $location (Required) The location to store the cache object in. This may vary by cache method.
38
	 * @param integer $expires (Required) The number of seconds until a cache object is considered stale.
39
	 * @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
40
	 * @return object Reference to the cache object.
41
	 */
42
	public function __construct($name, $location, $expires, $gzip = true)
43
	{
44
		parent::__construct($name, null, $expires, $gzip);
45
		$this->id = $this->name;
46
 
47
		// Prefer Memcached over Memcache.
48
		if (class_exists('Memcached'))
49
		{
50
			$this->memcache = new Memcached();
51
			$this->is_memcached = true;
52
		}
53
		elseif (class_exists('Memcache'))
54
		{
55
			$this->memcache = new Memcache();
56
		}
57
		else
58
		{
59
			return false;
60
		}
61
 
62
		// Enable compression, if available
63
		if ($this->gzip)
64
		{
65
			if ($this->is_memcached)
66
			{
67
				$this->memcache->setOption(Memcached::OPT_COMPRESSION, true);
68
			}
69
			else
70
			{
71
				$this->gzip = MEMCACHE_COMPRESSED;
72
			}
73
		}
74
 
75
		// Process Memcached servers.
76
		if (isset($location) && sizeof($location) > 0)
77
		{
78
			foreach ($location as $loc)
79
			{
80
				if (isset($loc['port']) && !empty($loc['port']))
81
				{
82
					$this->memcache->addServer($loc['host'], $loc['port']);
83
				}
84
				else
85
				{
86
					$this->memcache->addServer($loc['host'], 11211);
87
				}
88
			}
89
		}
90
 
91
		return $this;
92
	}
93
 
94
	/**
95
	 * Creates a new cache.
96
	 *
97
	 * @param mixed $data (Required) The data to cache.
98
	 * @return boolean Whether the operation was successful.
99
	 */
100
	public function create($data)
101
	{
102
		if ($this->is_memcached)
103
		{
104
			return $this->memcache->set($this->id, $data, $this->expires);
105
		}
106
		return $this->memcache->set($this->id, $data, $this->gzip, $this->expires);
107
	}
108
 
109
	/**
110
	 * Reads a cache.
111
	 *
112
	 * @return mixed Either the content of the cache object, or boolean `false`.
113
	 */
114
	public function read()
115
	{
116
		if ($this->is_memcached)
117
		{
118
			return $this->memcache->get($this->id);
119
		}
120
		return $this->memcache->get($this->id, $this->gzip);
121
	}
122
 
123
	/**
124
	 * Updates an existing cache.
125
	 *
126
	 * @param mixed $data (Required) The data to cache.
127
	 * @return boolean Whether the operation was successful.
128
	 */
129
	public function update($data)
130
	{
131
		if ($this->is_memcached)
132
		{
133
			return $this->memcache->replace($this->id, $data, $this->expires);
134
		}
135
		return $this->memcache->replace($this->id, $data, $this->gzip, $this->expires);
136
	}
137
 
138
	/**
139
	 * Deletes a cache.
140
	 *
141
	 * @return boolean Whether the operation was successful.
142
	 */
143
	public function delete()
144
	{
145
		return $this->memcache->delete($this->id);
146
	}
147
 
148
	/**
149
	 * Implemented here, but always returns `false`. Memcache manages its own expirations.
150
	 *
151
	 * @return boolean Whether the cache is expired or not.
152
	 */
153
	public function is_expired()
154
	{
155
		return false;
156
	}
157
 
158
	/**
159
	 * Implemented here, but always returns `false`. Memcache manages its own expirations.
160
	 *
161
	 * @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
162
	 */
163
	public function timestamp()
164
	{
165
		return false;
166
	}
167
 
168
	/**
169
	 * Implemented here, but always returns `false`. Memcache manages its own expirations.
170
	 *
171
	 * @return boolean Whether the operation was successful.
172
	 */
173
	public function reset()
174
	{
175
		return false;
176
	}
177
}
178
 
179
 
180
/*%******************************************************************************************%*/
181
// EXCEPTIONS
182
 
183
class CacheMC_Exception extends CacheCore_Exception {}