Passed
Pull Request — master (#16)
by Mihail
11:07 queued 02:40
created

MemcachedConfiguration::getTtl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching\Configuration;
14
15
use Koded\Stdlib\Immutable;
16
use Koded\Stdlib\Interfaces\Configuration;
17
18
/**
19
 * Class MemcachedConfiguration
20
 *
21
 * @see https://github.com/kodedphp/stdlib/blob/master/Interfaces.php#L158
22
 */
23
final class MemcachedConfiguration extends Immutable implements Configuration
24
{
25
26
    /**
27
     * MemcachedConfiguration constructor.
28
     *
29
     * @param array $options [optional] Memcached options. Used here:
30
     *
31
     * OPT_DISTRIBUTION          - consistent, if one node goes down it's keys are distributed to other nodes
32
     * OPT_CONNECT_TIMEOUT       - milliseconds after server is considered dead
33
     * OPT_SERVER_FAILURE_LIMIT  - number of connection failures before server is marked as dead and removed
34
     * OPT_REMOVE_FAILED_SERVERS - (bool) to remove dead server or not
35
     * OPT_RETRY_TIMEOUT         - try a dead server after this seconds (tweak for long running processes)
36
     *
37
     * @link http://php.net/manual/en/memcached.constants.php
38
     */
39
    public function __construct(array $options = [])
40
    {
41
        parent::__construct([
42
            'id' => $options['id'] ?? null,
43
            'servers' => $options['servers'] ?? [],
44
            'options' => array_replace([
45
                \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
46
                \Memcached::OPT_CONNECT_TIMEOUT => 10,
47
                \Memcached::OPT_SERVER_FAILURE_LIMIT => 2,
48
                \Memcached::OPT_REMOVE_FAILED_SERVERS => true,
49
                \Memcached::OPT_RETRY_TIMEOUT => 1,
50
                \Memcached::OPT_PREFIX_KEY => null
51
            ], $options['options'] ?? []),
52
            'ttl' => $options['ttl'] ?? null
53
        ]);
54
    }
55
56
    /**
57
     * Order of precedence when selecting the servers array
58
     *
59
     *  1. "servers" directive that holds an array of memcached servers
60
     *  2. environment variable "MEMCACHED_POOL" serialized as JSON [['ip', port],...]
61
     *  3. defaults to one server at localhost:11211
62
     *
63
     * @return array Memcached options.
64
     * The "MEMCACHED_POOL" is ignored if "servers" is provided in the configuration directives.
65
     *
66
     * @link http://php.net/manual/en/memcached.addservers.php
67
     */
68
    public function getServers(): array
69
    {
70
        if ($servers = $this->get('servers')) {
71
            return $servers;
72
        }
73
74
        if ($servers = json_decode(getenv('MEMCACHED_POOL'), true)) {
75
            return $servers;
76
        }
77
78
        return [
79
            ['127.0.0.1', 11211]
80
        ];
81
    }
82
83
    /**
84
     * To add Memcached options
85
     *
86
     *  - use the class constructor options argument
87
     *  - use the Config factory methods (if applicable)
88
     *  - use the class methods
89
     *
90
     * To remove options
91
     *
92
     *  - set the option(s) with NULL value
93
     *  - use the class methods
94
     *
95
     * @return array Filtered Memcached options
96
     */
97
    public function getOptions(): array
98
    {
99
        return array_filter($this->toArray()['options'], function($value) {
100
            return null !== $value;
101
        });
102
    }
103
104
    /**
105
     * Returns the global TTL in seconds, or NULL for never-expire value.
106
     *
107
     * @return int|null
108
     */
109
    public function getTtl(): ?int
110
    {
111
        if (null === $ttl = $this->get('ttl')) {
112
            return null;
113
        }
114
115
        return (int)$ttl;
116
    }
117
}
118