File indexing completed on 2024-04-28 05:55:13

0001 <?php
0002 /**
0003  *
0004  *  ocs-fileserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *  This file is part of ocs-fileserver.
0009  *
0010  *  ocs-fileserver is free software: you can redistribute it and/or modify
0011  *  it under the terms of the GNU Affero General Public License as published by
0012  *  the Free Software Foundation, either version 3 of the License, or
0013  *  (at your option) any later version.
0014  *
0015  *  ocs-fileserver is distributed in the hope that it will be useful,
0016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *  GNU Affero General Public License for more details.
0019  *
0020  *  You should have received a copy of the GNU Affero General Public License
0021  *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  *
0024  */
0025 
0026 class RedisCache
0027 {
0028     const NAMESPACE_SEPARATOR = ':_';
0029     /**
0030      * @var int|mixed
0031      */
0032     private $ttl;
0033     /**
0034      * @var mixed|string
0035      */
0036     private $namespace;
0037     /**
0038      * @var Redis
0039      */
0040     private $redisCache;
0041 
0042     public function __construct($config = null)
0043     {
0044         if (!extension_loaded('redis')) {
0045             throw new Exception("Redis extension is not loaded");
0046         }
0047 
0048         $hostname = isset($config['host']) ? $config['host'] : '127.0.0.1';
0049         $port = isset($config['port']) ? $config['port'] : 6379;
0050         $this->ttl = isset($config['ttl']) ? $config['ttl'] : null;
0051         $this->namespace = isset($config['namespace']) ? $config['namespace'] . self::NAMESPACE_SEPARATOR : '';
0052         $password = isset($config['password']) ? $config['password'] : null;
0053 
0054         $this->redisCache = new Redis();
0055         //Connecting to Redis
0056         if (false === $this->redisCache->connect($hostname, $port)) {
0057             throw new Exception("Redis connection could not instantiated");
0058         }
0059 
0060         if ($password) {
0061             $this->redisCache->auth('password');
0062         }
0063 
0064         $resultPing = $this->redisCache->ping();
0065         if (!$resultPing) {
0066             throw new Exception("Redis connection is inaccessible: {$resultPing}");
0067         }
0068         //$this->redisCache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
0069     }
0070 
0071     public function get($key)
0072     {
0073         return unserialize($this->redisCache->get($this->namespace . $key));
0074     }
0075 
0076     public function set($key, $value, $ttl = null)
0077     {
0078         return $this->redisCache->set($this->namespace . $key, serialize($value), $ttl);
0079     }
0080 
0081     public function has($key)
0082     {
0083         return $this->redisCache->exists($this->namespace . $key);
0084     }
0085 
0086 }