Issues (404)

classes/Memcache.php (3 issues)

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