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