Completed
Push — master ( 922be9...285e65 )
by Nick
41s queued 31s
created

Memcache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 36
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 2
A get() 0 12 3
A __construct() 0 8 3
1
<?php
2
3
/**
4
 * Memcache wrapper
5
 *
6
 * @package TheyWorkForYou
7
 */
8
9
namespace MySociety\TheyWorkForYou;
10
11
class Memcache {
12
13
    static $memcache;
14
15 2
    public function __construct() {
16 2
        if (!self::$memcache) {
17 1
            if (class_exists('\Memcached')) {
18 1
                self::$memcache = new \Memcached;
19 1
                self::$memcache->addServer('localhost', 11211);
20
            } else {
21
                self::$memcache = new \Memcache;
22
                self::$memcache->connect('localhost', 11211);
23
            }
24
        }
25 2
    }
26
27 2
    public function set($key, $value, $timeout = 3600) {
28 2
        if (class_exists('\Memcached')) {
29 2
            self::$memcache->set(OPTION_TWFY_DB_NAME.':'.$key, $value, $timeout);
30
        } else {
31
            self::$memcache->set(OPTION_TWFY_DB_NAME.':'.$key, $value, MEMCACHE_COMPRESSED, $timeout);
32
        }
33 2
    }
34
35 1
    public function get($key) {
36
        // see http://php.net/manual/en/memcache.get.php#112056 for explanation of this
37 1
        $was_found = false;
38 1
        if (class_exists('\Memcached')) {
39 1
            $value = self::$memcache->get(OPTION_TWFY_DB_NAME.':'.$key, null, $was_found);
40
        } else {
41
            $value = self::$memcache->get(OPTION_TWFY_DB_NAME.':'.$key, $was_found);
42
        }
43 1
        if ($was_found === false) {
0 ignored issues
show
introduced by
The condition $was_found === false is always true.
Loading history...
44 1
            return false; // mmmmm
45
        } else {
46
            return $value;
47
        }
48
    }
49
}
50