Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • CacheProviderAPC
  • CacheProviderArray
  • CacheProviderFactory
  • CacheProviderMemcache
  • CacheProviderRedis

Interfaces

  • CacheProviderInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alpha\Util\Cache;
  4: 
  5: use Alpha\Util\Logging\Logger;
  6: use Alpha\Util\Config\ConfigProvider;
  7: use Redis;
  8: 
  9: /**
 10:  * An implementation of the CacheProviderInterface interface that uses Redis as the
 11:  * target store.
 12:  *
 13:  * @since 1.2.4
 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 CacheProviderRedis implements CacheProviderInterface
 53: {
 54:     /**
 55:      * Trace logger.
 56:      *
 57:      * @var Alpha\Util\Logging\Logger
 58:      *
 59:      * @since 1.2.4
 60:      */
 61:     private static $logger = null;
 62: 
 63:     /**
 64:      * Connection to the cache server.
 65:      *
 66:      * @var Redis
 67:      *
 68:      * @since 1.2.4
 69:      */
 70:     private $connection;
 71: 
 72:     /**
 73:      * Constructor.
 74:      *
 75:      * @since 1.2.4
 76:      */
 77:     public function __construct()
 78:     {
 79:         self::$logger = new Logger('CacheProviderRedis');
 80: 
 81:         $config = ConfigProvider::getInstance();
 82: 
 83:         try {
 84:             $this->connection = new Redis();
 85:             $this->connection->connect($config->get('cache.redis.host'), $config->get('cache.redis.port'));
 86:             $this->connection->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
 87:             $this->connection->select($config->get('cache.redis.db'));
 88:         } catch (\Exception $e) {
 89:             self::$logger->error('Error while attempting to connect to Redis cache: ['.$e->getMessage().']');
 90:         }
 91:     }
 92: 
 93:     /**
 94:      * {@inheritdoc}
 95:      */
 96:     public function get($key)
 97:     {
 98:         self::$logger->debug('>>get(key=['.$key.'])');
 99: 
100:         try {
101:             $value = $this->connection->get($key);
102: 
103:             self::$logger->debug('<<get: ['.print_r($value, true).'])');
104: 
105:             return $value;
106:         } catch (\Exception $e) {
107:             self::$logger->error('Error while attempting to load a business object from Redis instance: ['.$e->getMessage().']');
108:             self::$logger->debug('<<get: [false])');
109: 
110:             return false;
111:         }
112:     }
113: 
114:     /**
115:      * {@inheritdoc}
116:      */
117:     public function set($key, $value, $expiry = 0)
118:     {
119:         try {
120:             if ($expiry > 0) {
121:                 $this->connection->setex($key, $expiry, $value);
122:             } else {
123:                 $this->connection->set($key, $value);
124:             }
125:         } catch (\Exception $e) {
126:             self::$logger->error('Error while attempting to store a value to Redis instance: ['.$e->getMessage().']');
127:         }
128:     }
129: 
130:     /**
131:      * {@inheritdoc}
132:      */
133:     public function delete($key)
134:     {
135:         try {
136:             $this->connection->delete($key);
137:         } catch (\Exception $e) {
138:             self::$logger->error('Error while attempting to remove a value from Redis instance: ['.$e->getMessage().']');
139:         }
140:     }
141: }
142: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0