Completed
Branch master (1afc22)
by Dan
09:51 queued 07:56
created

AbstractStorage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 108
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withTtl() 0 5 1
A getTtlTimestamp() 0 10 2
A getTtl() 0 3 1
set() 0 1 ?
has() 0 1 ?
get() 0 1 ?
delete() 0 1 ?
clear() 0 1 ?
1
<?php
2
/**
3
 * Src/Cache/Storage/AbstractStorage.php
4
 *
5
 * @package     Ds\Cache\Storage
6
 * @subpackage  Cache
7
 * @author      Dan Smith <[email protected]>
8
 * @version     v.1 (20/03/2017)
9
 * @copyright   Copyright (c) 2017, Dan Smith
10
 */
11
namespace Ds\Cache\Storage;
12
13
use DateInterval;
14
use DateTime;
15
use Ds\Cache\CacheStorageInterface;
16
17
/**
18
 * Class MemcacheStorage
19
 *
20
 * @package Ds\Cache\Storage
21
 */
22
abstract class AbstractStorage implements CacheStorageInterface
23
{
24
    /**
25
     * @var DateInterval
26
     */
27
    private $ttl;
28
29
    /**
30
     * Abstract Storage constructor.
31
     * @param DateInterval $ttl
32
     */
33 25
    public function __construct(DateInterval $ttl)
34
    {
35 25
        $this->ttl = $ttl;
36 25
    }
37
38
    /**
39
     * With default ttl.
40
     *
41
     * @param DateInterval $ttl
42
     * @return CacheStorageInterface
43
     */
44 1
    public function withTtl(DateInterval $ttl){
45 1
        $new = clone $this;
46 1
        $new->ttl = $ttl;
47 1
        return $new;
48
    }
49
50
    /**
51
     * Get ttl as integer.
52
     *
53
     * @param null|int $ttl
54
     * @return int
55
     */
56 9
    public function getTtlTimestamp($ttl = null){
57
58 9
        if (null === $ttl){
59 3
            $dateTime = new DateTime();
60 3
            $dateTime->add( $this->ttl );
61 3
            $ttl = $dateTime->getTimestamp();
62
        }
63
64 9
        return (int)$ttl;
65
    }
66
67
    /**
68
     * Get ttl as integer.
69
     *
70
     * @return DateInterval
71
     */
72 2
    public function getTtl(){
73 2
        return $this->ttl;
74
    }
75
76
    /**
77
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
78
     *
79
     * @param string   $key   The key of the item to store.
80
     * @param mixed    $value The value of the item to store, must be serializable.
81
     * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
82
     *                                     the driver supports TTL then the library may set a default value
83
     *                                     for it or let the driver take care of that.
84
     *
85
     * @return bool True on success and false on failure.
86
     */
87
    abstract public function set($key, $value, $ttl = null);
88
89
    /**
90
     * Determines whether an item is present in the cache.
91
     *
92
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
93
     * and not to be used within your live applications operations for get/set, as this method
94
     * is subject to a race condition where your has() will return true and immediately after,
95
     * another script can remove it making the state of your app out of date.
96
     *
97
     * @param string $key The cache item key.
98
     *
99
     * @return bool
100
     */
101
    abstract public function has($key);
102
103
    /**
104
     * Fetches a value from the cache.
105
     *
106
     * @param string $key     The unique key of this item in the cache.
107
     *
108
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
109
     *
110
     */
111
    abstract public function get($key);
112
113
    /**
114
     * Delete an item from the cache by its unique key.
115
     *
116
     * @param string $key The unique cache key of the item to delete.
117
     *
118
     * @return bool True if the item was successfully removed. False if there was an error.
119
     */
120
    abstract public function delete($key);
121
122
    /**
123
     * Wipes clean the entire cache's keys.
124
     *
125
     * @return bool True on success and false on failure.
126
     */
127
    abstract public function clear();
128
129
}
130