Completed
Push — master ( d87efb...5aade8 )
by Auke
22s
created

ar_cache_redisStore::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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