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 APC-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/apc APC
15
 */
16
class CacheAPC extends CacheCore implements ICacheCore
17
{
18
 
19
	/*%******************************************************************************************%*/
20
	// CONSTRUCTOR
21
 
22
	/**
23
	 * Constructs a new instance of this class.
24
	 *
25
	 * @param string $name (Required) A name to uniquely identify the cache object.
26
	 * @param string $location (Required) The location to store the cache object in. This may vary by cache method.
27
	 * @param integer $expires (Required) The number of seconds until a cache object is considered stale.
28
	 * @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
29
	 * @return object Reference to the cache object.
30
	 */
31
	public function __construct($name, $location, $expires, $gzip = true)
32
	{
33
		parent::__construct($name, null, $expires, $gzip);
34
		$this->id = $this->name;
35
	}
36
 
37
	/**
38
	 * Creates a new cache.
39
	 *
40
	 * @param mixed $data (Required) The data to cache.
41
	 * @return boolean Whether the operation was successful.
42
	 */
43
	public function create($data)
44
	{
45
		$data = serialize($data);
46
		$data = $this->gzip ? gzcompress($data) : $data;
47
 
48
		return apc_add($this->id, $data, $this->expires);
49
	}
50
 
51
	/**
52
	 * Reads a cache.
53
	 *
54
	 * @return mixed Either the content of the cache object, or boolean `false`.
55
	 */
56
	public function read()
57
	{
58
		if ($data = apc_fetch($this->id))
59
		{
60
			$data = $this->gzip ? gzuncompress($data) : $data;
61
			return unserialize($data);
62
		}
63
 
64
		return false;
65
	}
66
 
67
	/**
68
	 * Updates an existing cache.
69
	 *
70
	 * @param mixed $data (Required) The data to cache.
71
	 * @return boolean Whether the operation was successful.
72
	 */
73
	public function update($data)
74
	{
75
		$data = serialize($data);
76
		$data = $this->gzip ? gzcompress($data) : $data;
77
 
78
		return apc_store($this->id, $data, $this->expires);
79
	}
80
 
81
	/**
82
	 * Deletes a cache.
83
	 *
84
	 * @return boolean Whether the operation was successful.
85
	 */
86
	public function delete()
87
	{
88
		return apc_delete($this->id);
89
	}
90
 
91
	/**
92
	 * Implemented here, but always returns `false`. APC manages its own expirations.
93
	 *
94
	 * @return boolean Whether the cache is expired or not.
95
	 */
96
	public function is_expired()
97
	{
98
		return false;
99
	}
100
 
101
	/**
102
	 * Implemented here, but always returns `false`. APC manages its own expirations.
103
	 *
104
	 * @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
105
	 */
106
	public function timestamp()
107
	{
108
		return false;
109
	}
110
 
111
	/**
112
	 * Implemented here, but always returns `false`. APC manages its own expirations.
113
	 *
114
	 * @return boolean Whether the operation was successful.
115
	 */
116
	public function reset()
117
	{
118
		return false;
119
	}
120
}
121
 
122
 
123
/*%******************************************************************************************%*/
124
// EXCEPTIONS
125
 
126
class CacheAPC_Exception extends CacheCore_Exception {}