Completed
Push — master ( 178b97...0fcbd7 )
by Clinton
02:30
created

Redis::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace cvweiss\projectbase;
4
5
class Redis
6
{
7
    private static $pid = 0;
8
    private static $redis = null;
9
10
    public static function get()
11
    {
12
        if (self::$redis === null || self::$pid !== getmypid()) {
13
            $config = Config::getInstance();
14
15
            self::$redis = new \Redis();
16
            self::$redis->connect($config->get('redisServer', '127.0.0.1'), $config->get('redisPort', 6379), 3600);
17
            self::$pid = getmypid();
18
        }
19
        return self::$redis;
20
    }
21
22
    public static function canRun(string $key, int $mutex = 60)
23
    {
24
        $redis = self::get();
25
        $time = time();
26
        $time = $time - ($time % $mutex);
27
        
28
        $key = "$key:$time";
29
        $locked = $redis->set($key, true, Array('nx', 'ex' => $mutex));
30
        return $locked;
31
    }
32
}
33