Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Cache;
4
 
5
use Memcached;
6
 
7
class MemcachedConnector
8
{
9
    /**
10
     * Create a new Memcached connection.
11
     *
12
     * @param  array  $servers
13
     * @param  string|null  $connectionId
14
     * @param  array  $options
15
     * @param  array  $credentials
16
     * @return \Memcached
17
     */
18
    public function connect(array $servers, $connectionId = null, array $options = [], array $credentials = [])
19
    {
20
        $memcached = $this->getMemcached(
21
            $connectionId, $credentials, $options
22
        );
23
 
24
        if (! $memcached->getServerList()) {
25
            // For each server in the array, we'll just extract the configuration and add
26
            // the server to the Memcached connection. Once we have added all of these
27
            // servers we'll verify the connection is successful and return it back.
28
            foreach ($servers as $server) {
29
                $memcached->addServer(
30
                    $server['host'], $server['port'], $server['weight']
31
                );
32
            }
33
        }
34
 
35
        return $memcached;
36
    }
37
 
38
    /**
39
     * Get a new Memcached instance.
40
     *
41
     * @param  string|null  $connectionId
42
     * @param  array  $credentials
43
     * @param  array  $options
44
     * @return \Memcached
45
     */
46
    protected function getMemcached($connectionId, array $credentials, array $options)
47
    {
48
        $memcached = $this->createMemcachedInstance($connectionId);
49
 
50
        if (count($credentials) === 2) {
51
            $this->setCredentials($memcached, $credentials);
52
        }
53
 
54
        if (count($options)) {
55
            $memcached->setOptions($options);
56
        }
57
 
58
        return $memcached;
59
    }
60
 
61
    /**
62
     * Create the Memcached instance.
63
     *
64
     * @param  string|null  $connectionId
65
     * @return \Memcached
66
     */
67
    protected function createMemcachedInstance($connectionId)
68
    {
69
        return empty($connectionId) ? new Memcached : new Memcached($connectionId);
70
    }
71
 
72
    /**
73
     * Set the SASL credentials on the Memcached connection.
74
     *
75
     * @param  \Memcached  $memcached
76
     * @param  array  $credentials
77
     * @return void
78
     */
79
    protected function setCredentials($memcached, $credentials)
80
    {
81
        [$username, $password] = $credentials;
82
 
83
        $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
84
 
85
        $memcached->setSaslAuthData($username, $password);
86
    }
87
}