1: <?php
2: /**
3: * A cache for Kolab storage.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Storage
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Storage
12: */
13:
14: /**
15: * The Kolab_Cache class provides a cache for Kolab groupware objects.
16: *
17: * The Horde_Kolab_Storage_Cache singleton instance provides caching for all
18: * storage folders. So before operating on the cache data it is necessary to
19: * load the desired folder data. Before switching the folder the cache data
20: * should be saved.
21: *
22: * This class does not offer a lot of safeties and is primarily intended to be
23: * used within the Horde_Kolab_Storage_Data class.
24: *
25: * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
26: *
27: * See the enclosed file COPYING for license information (LGPL). If you
28: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
29: *
30: * @category Kolab
31: * @package Kolab_Storage
32: * @author Gunnar Wrobel <wrobel@pardus.de>
33: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
34: * @link http://pear.horde.org/index.php?package=Kolab_Storage
35: */
36: class Horde_Kolab_Storage_Cache
37: {
38: /**
39: * The link to the horde cache.
40: *
41: * @var Horde_Cache
42: */
43: protected $horde_cache;
44:
45: /**
46: * List cache instances.
47: *
48: * @var array
49: */
50: private $_list_caches;
51:
52: /**
53: * Data cache instances.
54: *
55: * @var array
56: */
57: private $_data_caches;
58:
59: /**
60: * Constructor.
61: *
62: * @param Horde_Cache $cache The global cache for temporary data storage.
63: */
64: public function __construct($cache)
65: {
66: $this->horde_cache = $cache;
67: }
68:
69: /**
70: * Return a data cache.
71: *
72: * @param array $data_params Return the data cache for a data set
73: * with these parameters.
74: *
75: * @return Horde_Kolab_Storage_Cache_Data The data cache.
76: */
77: public function getDataCache($data_params)
78: {
79: $data_id = $this->_getDataId($data_params);
80: if (!isset($this->_data_caches[$data_id])) {
81: $this->_data_caches[$data_id] = new Horde_Kolab_Storage_Cache_Data(
82: $this, $data_params
83: );
84: $this->_data_caches[$data_id]->setDataId($data_id);
85: }
86: return $this->_data_caches[$data_id];
87: }
88:
89: /**
90: * Retrieve data set.
91: *
92: * @param string $data_id ID of the data set.
93: *
94: * @return string The cached data set.
95: */
96: public function loadData($data_id)
97: {
98: return $this->horde_cache->get($data_id, 0);
99: }
100:
101: /**
102: * Cache data set.
103: *
104: * @param string $data_id ID of the data set.
105: * @param string $data The data to be cached.
106: *
107: * @return NULL
108: */
109: public function storeData($data_id, $data)
110: {
111: $this->horde_cache->set($data_id, $data, 0);
112: }
113:
114: /**
115: * Retrieve an attachment.
116: *
117: * @param string $data_id ID of the data set.
118: * @param string $obid Object backend id.
119: * @param string $attachment_id Attachment ID.
120: *
121: * @return resource A stream opened to the attachement data.
122: */
123: public function loadAttachment($data_id, $obid, $attachment_id)
124: {
125: return $this->horde_cache->get(
126: $this->_getAttachmentId($data_id, $obid, $attachment_id),
127: 0
128: );
129: }
130:
131: /**
132: * Store an attachment.
133: *
134: * @param string $data_id ID of the data set.
135: * @param string $obid Object backend id.
136: * @param string $attachment_id Attachment ID.
137: * @param resource $data A stream opened to the attachement data.
138: *
139: * @return NULL
140: */
141: public function storeAttachment($data_id, $obid, $attachment_id, $data)
142: {
143: $this->horde_cache->set(
144: $this->_getAttachmentId($data_id, $obid, $attachment_id),
145: $data,
146: 0
147: );
148: }
149:
150: /**
151: * Delete a cached attachment.
152: *
153: * @param string $data_id ID of the data set.
154: * @param string $obid Object backend id.
155: * @param string $attachment_id Attachment ID.
156: *
157: * @return NULL
158: */
159: public function deleteAttachment($data_id, $obid, $attachment_id)
160: {
161: return $this->horde_cache->expire(
162: $this->_getAttachmentId($data_id, $obid, $attachment_id)
163: );
164: }
165:
166: /**
167: * Return a list cache.
168: *
169: * @param array $connection_params Return the list cache for a connection
170: * with these parameters.
171: *
172: * @return Horde_Kolab_Storage_Cache_List The list cache.
173: */
174: public function getListCache($connection_params)
175: {
176: $list_id = $this->_getListId($connection_params);
177: if (!isset($this->_list_caches[$list_id])) {
178: $this->_list_caches[$list_id] = new Horde_Kolab_Storage_Cache_List(
179: $this, $connection_params
180: );
181: $this->_list_caches[$list_id]->setListId($list_id);
182: }
183: return $this->_list_caches[$list_id];
184: }
185:
186: /**
187: * Retrieve list data.
188: *
189: * @param string $list_id ID of the connection matching the list.
190: *
191: * @return string The data of the object.
192: */
193: public function loadList($list_id)
194: {
195: return $this->horde_cache->get($list_id, 0);
196: }
197:
198: /**
199: * Cache list data.
200: *
201: * @param string $list_id ID of the connection matching the list.
202: * @param string $data The data to be cached.
203: *
204: * @return NULL
205: */
206: public function storeList($list_id, $data)
207: {
208: $this->horde_cache->set($list_id, $data, 0);
209: }
210:
211: /**
212: * Compose the list key.
213: *
214: * @param array $connection_params Return the list ID for a connection with
215: * these parameters.
216: *
217: * @return string The list cache ID.
218: */
219: private function _getListId($connection_params)
220: {
221: foreach (array('host', 'port', 'user') as $key) {
222: $this->_requireParameter($connection_params, 'list', $key);
223: }
224: ksort($connection_params);
225: return md5(serialize($connection_params));
226: }
227:
228: /**
229: * Compose the data key.
230: *
231: * @param array $data_params Return the data ID for a data set with these
232: * parameters.
233: *
234: * @return string The data cache ID.
235: */
236: private function _getDataId($data_params)
237: {
238: foreach (array('host', 'port', 'prefix', 'folder', 'type', 'owner') as $key) {
239: $this->_requireParameter($data_params, 'data', $key);
240: }
241: ksort($data_params);
242: return md5(serialize($data_params));
243: }
244:
245: /**
246: * Compose the attachment key.
247: *
248: * @param string $data_id ID of the data set.
249: * @param string $obid Object backend id.
250: * @param string $attachment_id Attachment ID.
251: *
252: * @return string The attachment cache ID.
253: */
254: private function _getAttachmentId($data_id, $obid, $attachment_id)
255: {
256: return md5(
257: serialize(array('d' => $data_id, 'o' => (string)$obid, 'p' => (string)$attachment_id))
258: );
259: }
260:
261: /**
262: * Determine if a necessary parameter is set.
263: *
264: * @return NULL
265: *
266: * @throws Horde_Kolab_Storage_Exception In case the parameter is missing.
267: */
268: private function _requireParameter($parameters, $type, $key)
269: {
270: if (!isset($parameters[$key])) {
271: throw new Horde_Kolab_Storage_Exception(
272: sprintf(
273: 'Unable to determine the %s cache key: The "%s" parameter is missing!',
274: $type,
275: $key
276: )
277: );
278: }
279:
280: }
281: }
282: