ar_cache_redisStore   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 5
A get() 0 7 2
A getIfFresh() 0 7 3
A set() 0 4 1
A info() 0 9 1
A clear() 0 3 1
A subStore() 0 3 1
A isFresh() 0 3 1
A purge() 0 3 1
A putvar() 0 3 1
A getvar() 0 3 1
1
<?php
2
3
ar_pinp::allow('ar_cache_redisStore');
4
5
class ar_cache_redisStore implements ar_cacheStoreInterface, arKeyValueStoreInterface {
6
	private $timeout = 7200;
7
	private $redis   = null;
8
9
	public function __construct( $options, $timeout = 7200,  $prefix = 'AR_') {
10
11
		if ( is_string($timeout) ) {
12
			$timeout = strtotime( $timeout, 0);
13
		}
14
15
		$this->timeout = $timeout;
16
		$this->prefix  = $prefix;
0 ignored issues
show
Bug introduced by
The property prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
		$this->redis   = $redis = new Redis();
18
19
		if (isset($options['port'])) {
20
			$redis->pconnect($options['server'],$options['port']);
21
		} else {
22
			$redis->pconnect($options['server']);
23
		}
24
25
		if (isset($options['auth'])) {
26
			$redis->auth($options['auth']);
27
		}
28
29
		if (isset($options['db'])) {
30
			$redis->select($options['db']);
31
		}
32
		$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
33
		$redis->setOption(Redis::OPT_PREFIX, $prefix);
34
	}
35
36
	// key
37
	public function get( $path ) {
38
		$res = $this->redis->exists($path);
39
		if($res === true ) {
40
			return $this->redis->get($path);
41
		}
42
		return null;
43
	}
44
45
	public function getIfFresh( $path, $timeout = 0 ) {
46
		$ttl = $this->redis->ttl($path);
47
		if($ttl > 0 || $ttl === -1 ){ 
48
			return $this->redis->get($path);
49
		}
50
		return null;
51
	}
52
53
	// key value expire
54
	public function set( $path, $value, $timeout = 7200 ) {
55
		$res = $this->redis->setEx($path, $timeout, $value);
56
		return $res;
57
	}
58
59
	// meta info
60
	public function info( $path ) {
61
		$res =  [
62
			'size'    => null,
63
			'timeout' => $this->redis->ttl( $path ),
64
			'ctime'   => null,
65
		];
66
		$res['fresh'] = (bool)$res['timeout'];
67
		return $res;
68
	}
69
70
	// remove key
71
	public function clear( $path = null ) {
72
		return $this->redis->delete($path);
73
	}
74
75
	public function subStore( $path ) {
76
		// geen idee eigenlijk
77
	}
78
79
	// hoeveel tijd hebben we nog
80
	public function isFresh( $path ) {
81
		return (bool)$this->redis->ttl($path);
82
	}
83
84
	// purge, we do not have the option to partial purge, so purge all
85
	public function purge($name=null){
86
		return $this->redis->delete( $this->redis->keys($name .'*'));
87
	}
88
89
	public function putvar( $name, $value ) {
90
		return $this->set( $name, $value );
91
	}
92
93
	public function getvar( $name ) {
94
		return $this->get( $name );
95
	}
96
}
97
98