Redis   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 67
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 3 1
A instance() 0 3 1
A __construct() 0 14 1
A get() 0 3 1
A delete() 0 3 1
A exists() 0 3 1
A inc() 0 3 1
A dec() 0 3 1
A set() 0 4 2
A flush() 0 3 1
1
<?php
2
3
/**
4
 * Cache\Redis
5
 *
6
 * Core\Cache Redis Driver.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @author [email protected]
11
 * @version 1.0.1
12
 * @copyright Caffeina srl - 2014-2016 - http://caffeina.com
13
 */
14
15
16
namespace Cache;
17
18
class Redis implements Adapter {
19
20
  protected $redis = null;
21
  protected $options = [
22
    'scheme'      => 'tcp',
23
    'host'        => '127.0.0.1',
24
    'port'        => 6379,
25
    'timeout'     => 1,
26
    'reconnect'   => 100,
27
    'prefix'      => '',
28
    'serialize'   => true,
29
    'database'    => 0,
30
    'exceptions'  => true,
31
  ];
32
33
  public static function valid(){
34
    return true;
35
  }
36
37
  public function instance(){
38
    return $redis;
0 ignored issues
show
Bug introduced by
The variable $redis does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
  }
40
41
  public function __construct($opt=[]){
42
    /**
43
     * Predis Docs:
44
     * https://github.com/nrk/predis
45
     */
46
    $this->options = array_merge($this->options, $opt);
47
    $this->redis   = new \Predis\Client($this->options['scheme'].'://'.$this->options['host'].':'.$this->options['port'].'/', [
48
      'prefix'              => 'core:'.$this->options['prefix'],
49
      'exceptions'          => $this->options['exceptions'],
50
      'connection_timeout'  => $this->options['timeout'],
51
      'database'            => $this->options['database'],
52
    ]);
53
    $this->redis->select($this->options['database']);
54
  }
55
56
  public function get($key){
57
    return unserialize($this->redis->get($key));
58
  }
59
60
  public function set($key,$value,$expire=0){
61
    $expire = (int)$expire;
62
    return $expire > 0 ? $this->redis->setex($key,$expire,serialize($value)) : $this->redis->set($key,serialize($value));
63
  }
64
65
  public function delete($key){
66
  	$this->redis->del($key);
67
  }
68
69
  public function exists($key){
70
    return $this->redis->exists($key);
71
  }
72
73
  public function flush(){
74
    return $this->redis->flushdb();
75
  }
76
77
  public function inc($key,$value=1){
78
  	return $this->redis->incrby($key,$value);
79
  }
80
81
  public function dec($key,$value=1){
82
    return $this->redis->decrby($key,$value);
83
  }
84
}
85