Redis   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canRun() 0 10 1
A get() 0 11 3
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