AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • AbstractCache
  • MemcachedWrapper
  • RedisWrapper
  1 <?php
  2 
  3     namespace Alo\Cache;
  4 
  5     use Redis;
  6 
  7     if (!defined('GEN_START')) {
  8         http_response_code(404);
  9     } else {
 10 
 11         \Alo::loadConfig('redis');
 12 
 13         /**
 14          * A wrapper for PHP's Redis extension.
 15          *
 16          * @author  Art <a.molcanovas@gmail.com>
 17          * @package Cache
 18          */
 19         class RedisWrapper extends AbstractCache {
 20 
 21             /**
 22              * The memcached instance
 23              *
 24              * @var Redis
 25              */
 26             protected $client;
 27 
 28             /**
 29              * Instantiates the class
 30              *
 31              * @author Art <a.molcanovas@gmail.com>
 32              *
 33              * @param boolean $initDefaultServer Whether to add a server on construct
 34              */
 35             function __construct($initDefaultServer = true) {
 36                 $this->client = new Redis();
 37                 if ($initDefaultServer) {
 38                     $this->addServer();
 39                 }
 40                 parent::__construct();
 41 
 42                 \Log::debug('RedisWrapper instantiated.');
 43             }
 44 
 45             /**
 46              * Adds a server to the pool
 47              *
 48              * @author Art <a.molcanovas@gmail.com>
 49              *
 50              * @param string $ip     The server IP
 51              * @param int    $port   The server port
 52              * @param int    $weight The server's weight, ie how likely it is to be used. Currently unused by Redis.
 53              *
 54              * @return boolean
 55              */
 56             function addServer($ip = ALO_REDIS_IP, $port = ALO_REDIS_PORT, $weight = 1) {
 57                 \Log::debug('Added RedisWrapper server ' . $ip . ':' . $port);
 58 
 59                 //Param required as per superclass declaration. Unsetting so it doesn't get flagged
 60                 //as unused
 61                 unset($weight);
 62 
 63                 return $this->client->connect($ip, $port);
 64             }
 65 
 66             /**
 67              * Deletes a memcache key
 68              *
 69              * @author Art <a.molcanovas@gmail.com>
 70              *
 71              * @param string $key The key. Can supply multiple keys as arguments to delete them all.
 72              *
 73              * @return boolean
 74              */
 75             function delete($key) {
 76                 $this->client->delete(func_get_args());
 77 
 78                 //Parameter required as per parent declaration; unsetting to prevent flagging
 79                 //as unused var.
 80                 unset($key);
 81 
 82                 return true;
 83             }
 84 
 85             /**
 86              * Instantiates the class
 87              *
 88              * @author Art <a.molcanovas@gmail.com>
 89              *
 90              * @param boolean $initDefaultServer Whether to add a server on construct
 91              *
 92              * @return RedisWrapper
 93              */
 94             static function redisWrapper($initDefaultServer = true) {
 95                 return new RedisWrapper($initDefaultServer);
 96             }
 97 
 98             /**
 99              * Gets a cached value
100              *
101              * @author Art <a.molcanovas@gmail.com>
102              *
103              * @param string $id The value's key
104              *
105              * @return mixed
106              */
107             function get($id) {
108                 $get = $this->client->get($id);
109                 if (!$get) {
110                     return null;
111                 } else {
112                     return unserialize($get);
113                 }
114             }
115 
116             /**
117              * Checks if Redis is available
118              *
119              * @author Art <a.molcanovas@gmail.com>
120              * @return boolean
121              */
122             static function isAvailable() {
123                 return class_exists('\Redis');
124             }
125 
126             /**
127              * Return all cached keys and values
128              *
129              * @author Art <a.molcanovas@gmail.com>
130              * @return array
131              */
132             function getAll() {
133                 $keys = $this->client->keys('*');
134                 $r    = [];
135                 if ($keys) {
136                     foreach ($keys as $k) {
137                         $r[$k] = $this->get($k);
138                     }
139                 }
140 
141                 return $r;
142             }
143 
144             /**
145              * Clears all items from cache
146              *
147              * @author Art <a.molcanovas@gmail.com>
148              * @return boolean
149              */
150             function purge() {
151                 return $this->client->flushAll();
152             }
153 
154             /**
155              * Sets a cached key/value pair
156              *
157              * @author Art <a.molcanovas@gmail.com>
158              *
159              * @param string $key    The key identifier
160              * @param mixed  $var    The value to set
161              * @param int    $expire When to expire the set data. Defaults to 3600s.
162              *
163              * @return boolean
164              */
165             function set($key, $var, $expire = 3600) {
166                 \Log::debug('Set the RedisWrapper key ' . $key);
167 
168                 return $this->client->setex($key, $expire, serialize($var));
169             }
170 
171         }
172     }
173 
AloFramework documentation API documentation generated byApiGen 2.8.0