summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Transport/Memcache.php
blob: fb56cdf477deb50d6ecec4b40b2b88fd8a7ccb12 (plain)
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
<?php
namespace Elastica\Transport;

use Elastica\Exception\Connection\MemcacheException;
use Elastica\Exception\InvalidException;
use Elastica\Exception\PartialShardFailureException;
use Elastica\Exception\ResponseException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

/**
 * Elastica Memcache Transport object.
 *
 * @author Nicolas Ruflin <spam@ruflin.com>
 *
 * @deprecated The memcached transport is deprecated as of ES 1.5, and will be removed in ES 2.0
 */
class Memcache extends AbstractTransport
{
    const MAX_KEY_LENGTH = 250;

    /**
     * Makes calls to the elasticsearch server.
     *
     * @param \Elastica\Request $request
     * @param array             $params  Host, Port, ...
     *
     * @throws \Elastica\Exception\ResponseException
     * @throws \Elastica\Exception\InvalidException
     *
     * @return \Elastica\Response Response object
     */
    public function exec(Request $request, array $params)
    {
        $memcache = new \Memcache();
        $memcache->connect($this->getConnection()->getHost(), $this->getConnection()->getPort());

        $data = $request->getData();

        $content = '';

        if (!empty($data) || '0' === $data) {
            if (is_array($data)) {
                $content = JSON::stringify($data);
            } else {
                $content = $data;
            }

            // Escaping of / not necessary. Causes problems in base64 encoding of files
            $content = str_replace('\/', '/', $content);
        }

        $responseString = '';

        $start = microtime(true);

        switch ($request->getMethod()) {
            case Request::POST:
            case Request::PUT:
                $key = $request->getPath();
                $this->_checkKeyLength($key);
                $memcache->set($key, $content);
                break;
            case Request::GET:
                $key = $request->getPath().'?source='.$content;
                $this->_checkKeyLength($key);
                $responseString = $memcache->get($key);
                break;
            case Request::DELETE:
                $key = $request->getPath().'?source='.$content;
                $this->_checkKeyLength($key);
                $responseString = $memcache->delete($key);
                break;
            default:
            case Request::HEAD:
                throw new InvalidException('Method '.$request->getMethod().' is not supported in memcache transport');
        }

        $end = microtime(true);

        $response = new Response($responseString);
        $response->setQueryTime($end - $start);

        if ($response->hasError()) {
            throw new ResponseException($request, $response);
        }

        if ($response->hasFailedShards()) {
            throw new PartialShardFailureException($request, $response);
        }

        return $response;
    }

    /**
     * Check if key that will be used dont exceed 250 symbols.
     *
     * @param string $key
     *
     * @throws Elastica\Exception\Connection\MemcacheException If key is too long
     */
    private function _checkKeyLength($key)
    {
        if (strlen($key) >= self::MAX_KEY_LENGTH) {
            throw new MemcacheException('Memcache key is too long');
        }
    }
}