aboutsummaryrefslogtreecommitdiff
path: root/caches/MemcachedCache.php
blob: 8619c2557e470d3a417a87c7bed96bacac7d7e1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

class MemcachedCache implements CacheInterface
{
    private $scope;
    private $key;
    private $conn;
    private $expiration = 0;
    private $time = false;
    private $data = null;

    public function __construct()
    {
        if (!extension_loaded('memcached')) {
            returnServerError('"memcached" extension not loaded. Please check "php.ini"');
        }

        $host = Configuration::getConfig(get_called_class(), 'host');
        $port = Configuration::getConfig(get_called_class(), 'port');
        if (empty($host) && empty($port)) {
            returnServerError('Configuration for ' . get_called_class() . ' missing. Please check your ' . FILE_CONFIG);
        } elseif (empty($host)) {
            returnServerError('"host" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
        } elseif (empty($port)) {
            returnServerError('"port" param is not set for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
        } elseif (!ctype_digit($port)) {
            returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
        }

        $port = intval($port);

        if ($port < 1 || $port > 65535) {
            returnServerError('"port" param is invalid for ' . get_called_class() . '. Please check your ' . FILE_CONFIG);
        }

        $conn = new Memcached();
        $conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
        $this->conn = $conn;
    }

    public function loadData()
    {
        if ($this->data) {
            return $this->data;
        }
        $result = $this->conn->get($this->getCacheKey());
        if ($result === false) {
            return null;
        }

        $this->time = $result['time'];
        $this->data = $result['data'];
        return $result['data'];
    }

    public function saveData($datas)
    {
        $time = time();
        $object_to_save = [
            'data' => $datas,
            'time' => $time,
        ];
        $result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);

        if ($result === false) {
            returnServerError('Cannot write the cache to memcached server');
        }

        $this->time = $time;

        return $this;
    }

    public function getTime()
    {
        if ($this->time === false) {
            $this->loadData();
        }
        return $this->time;
    }

    public function purgeCache($duration)
    {
        // Note: does not purges cache right now
        // Just sets cache expiration and leave cache purging for memcached itself
        $this->expiration = $duration;
    }

    /**
    * Set scope
    * @return self
    */
    public function setScope($scope)
    {
        $this->scope = $scope;
        return $this;
    }

    /**
    * Set key
    * @return self
    */
    public function setKey($key)
    {
        if (!empty($key) && is_array($key)) {
            $key = array_map('strtolower', $key);
        }
        $key = json_encode($key);

        if (!is_string($key)) {
            throw new \Exception('The given key is invalid!');
        }

        $this->key = $key;
        return $this;
    }

    private function getCacheKey()
    {
        if (is_null($this->key)) {
            returnServerError('Call "setKey" first!');
        }

        return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
    }
}