MemcachedCache::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Cache;
13
14
use Carbon\Carbon;
15
16
/**
17
 * Memcached Cache Driver.
18
 */
19
class MemcachedCache extends BaseCache
20
{
21
    /**
22
     * The Memcached instance.
23
     *
24
     * @var \Memcached
25
     */
26
    protected $memcached;
27
28
    /**
29
     * A string that should be prepended to keys.
30
     *
31
     * @var string
32
     */
33
    protected $prefix;
34
35
    /**
36
     * Container.
37
     *
38
     * @var array
39
     */
40
    private $storage = [];
0 ignored issues
show
Unused Code introduced by
The property $storage is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
42
    /**
43
     * Create a new Memcached store.
44
     *
45
     * @param \Memcached $memcached
46
     * @param string     $prefix
47
     *
48
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
49
     */
50
    public function __construct($memcached, $prefix = '')
51
    {
52
        $this->setPrefix($prefix);
53
        $this->memcached = $memcached;
54
    }
55
56
    /**
57
     * Store an item in the cache.
58
     *
59
     * @param string $key
60
     * @param mixed  $value
61
     * @param int    $minutes
62
     */
63
    public function set($key, $value, $minutes = 0)
64
    {
65
        $this->memcached->set($this->prefix.$key, $value, $this->toTimestamp($minutes));
66
    }
67
68
    /**
69
     * Retrieve an item from the cache by key.
70
     *
71
     * @param string $key
72
     *
73
     * @return mixed Null when not found, cached value otherwise
74
     */
75
    public function get($key)
76
    {
77
        $value = $this->memcached->get($this->prefix.$key);
78
        if ($this->memcached->getResultCode() == 0) {
79
            return $value;
80
        }
81
    }
82
83
    /**
84
     * Clear all cache.
85
     */
86
    public function flush()
87
    {
88
        $this->memcached->flush();
89
    }
90
91
    /**
92
     * Remove cached value.
93
     *
94
     * @param string $key
95
     */
96
    public function remove($key)
97
    {
98
        return $this->memcached->delete($this->prefix.$key);
99
    }
100
101
    /**
102
     * Get the UNIX timestamp for the given number of minutes.
103
     *
104
     * @parma  int  $minutes
105
     *
106
     * @return int
107
     */
108
    protected function toTimestamp($minutes)
109
    {
110
        return $minutes > 0 ? Carbon::now()->addMinutes($minutes)->getTimestamp() : 0;
111
    }
112
113
    /**
114
     * Get the cache key prefix.
115
     *
116
     * @return string
117
     */
118
    public function getPrefix()
119
    {
120
        return $this->prefix;
121
    }
122
123
    /**
124
     * Set the cache key prefix.
125
     *
126
     * @param string $prefix
127
     *
128
     * @return void
129
     */
130
    public function setPrefix($prefix)
131
    {
132
        $this->prefix = !empty($prefix) ? $prefix.':' : '';
133
    }
134
}
135