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