1: <?php
2:
3: namespace Alpha\Util\Cache;
4:
5: use Alpha\Util\Logging\Logger;
6:
7: /**
8: * An implementation of the CacheProviderInterface interface that uses APC/APCu as the
9: * target store.
10: *
11: * @since 1.2.4
12: *
13: * @author John Collins <dev@alphaframework.org>
14: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
15: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
16: * All rights reserved.
17: *
18: * <pre>
19: * Redistribution and use in source and binary forms, with or
20: * without modification, are permitted provided that the
21: * following conditions are met:
22: *
23: * * Redistributions of source code must retain the above
24: * copyright notice, this list of conditions and the
25: * following disclaimer.
26: * * Redistributions in binary form must reproduce the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer in the documentation and/or other
29: * materials provided with the distribution.
30: * * Neither the name of the Alpha Framework nor the names
31: * of its contributors may be used to endorse or promote
32: * products derived from this software without specific
33: * prior written permission.
34: *
35: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
40: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48: * </pre>
49: */
50: class CacheProviderAPC implements CacheProviderInterface
51: {
52: /**
53: * Trace logger.
54: *
55: * @var Alpha\Util\Logging\Logger
56: *
57: * @since 1.2.4
58: */
59: private static $logger = null;
60:
61: /**
62: * Constructor.
63: *
64: * @since 1.2.4
65: */
66: public function __construct()
67: {
68: self::$logger = new Logger('CacheProviderAPC');
69: }
70:
71: /**
72: * {@inheritdoc}
73: */
74: public function get($key)
75: {
76: self::$logger->debug('>>get(key=['.$key.'])');
77:
78: try {
79: $value = apc_fetch($key);
80:
81: self::$logger->debug('<<get: ['.print_r($value, true).'])');
82:
83: return $value;
84: } catch (\Exception $e) {
85: self::$logger->error('Error while attempting to load a business object from APC cache: ['.$e->getMessage().']');
86: self::$logger->debug('<<get: [false])');
87:
88: return false;
89: }
90: }
91:
92: /**
93: * {@inheritdoc}
94: */
95: public function set($key, $value, $expiry = 0)
96: {
97: try {
98: if ($expiry > 0) {
99: apc_store($key, $value, $expiry);
100: } else {
101: apc_store($key, $value);
102: }
103: } catch (\Exception $e) {
104: self::$logger->error('Error while attempting to store a value to APC cache: ['.$e->getMessage().']');
105: }
106: }
107:
108: /**
109: * {@inheritdoc}
110: */
111: public function delete($key)
112: {
113: try {
114: apc_delete($key);
115: } catch (\Exception $e) {
116: self::$logger->error('Error while attempting to remove a value from APC cache: ['.$e->getMessage().']');
117: }
118: }
119: }
120: