Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
/**
5
 * SimplePie
6
 *
7
 * A PHP-Based RSS and Atom Feed Framework.
8
 * Takes the hard work out of managing a complete RSS/Atom solution.
9
 *
10
 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification, are
14
 * permitted provided that the following conditions are met:
15
 *
16
 * 	* Redistributions of source code must retain the above copyright notice, this list of
17
 * 	  conditions and the following disclaimer.
18
 *
19
 * 	* Redistributions in binary form must reproduce the above copyright notice, this list
20
 * 	  of conditions and the following disclaimer in the documentation and/or other materials
21
 * 	  provided with the distribution.
22
 *
23
 * 	* Neither the name of the SimplePie Team nor the names of its contributors may be used
24
 * 	  to endorse or promote products derived from this software without specific prior
25
 * 	  written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
28
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
30
 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @package SimplePie
38
 * @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue
39
 * @author Ryan Parman
40
 * @author Sam Sneddon
41
 * @author Ryan McCue
42
 * @link http://simplepie.org/ SimplePie
43
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44
 */
45
 
46
namespace SimplePie\Cache;
47
 
48
use InvalidArgumentException;
49
 
50
/**
51
 * Adapter for deprecated \SimplePie\Cache\Base implementations
52
 *
53
 * @package SimplePie
54
 * @subpackage Caching
55
 * @internal
56
 */
57
final class BaseDataCache implements DataCache
58
{
59
    /**
60
     * @var Base
61
     */
62
    private $cache;
63
 
64
    public function __construct(Base $cache)
65
    {
66
        $this->cache = $cache;
67
    }
68
 
69
    /**
70
     * Fetches a value from the cache.
71
     *
72
     * Equivalent to \Psr\SimpleCache\CacheInterface::get()
73
     * <code>
74
     * public function get(string $key, mixed $default = null): mixed;
75
     * </code>
76
     *
77
     * @param string $key     The unique key of this item in the cache.
78
     * @param mixed  $default Default value to return if the key does not exist.
79
     *
80
     * @return array|mixed The value of the item from the cache, or $default in case of cache miss.
81
     *
82
     * @throws InvalidArgumentException
83
     *   MUST be thrown if the $key string is not a legal value.
84
     */
85
    public function get_data(string $key, $default = null)
86
    {
87
        $data = $this->cache->load();
88
 
89
        if (! is_array($data)) {
90
            return $default;
91
        }
92
 
93
        // ignore data if internal cache expiration time is not set
94
        if (! array_key_exists('__cache_expiration_time', $data)) {
95
            return $default;
96
        }
97
 
98
        // ignore data if internal cache expiration time is expired
99
        if ($data['__cache_expiration_time'] < time()) {
100
            return $default;
101
        }
102
 
103
        // remove internal cache expiration time
104
        unset($data['__cache_expiration_time']);
105
 
106
        return $data;
107
    }
108
 
109
    /**
110
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
111
     *
112
     * Equivalent to \Psr\SimpleCache\CacheInterface::set()
113
     * <code>
114
     * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;
115
     * </code>
116
     *
117
     * @param string   $key   The key of the item to store.
118
     * @param array    $value The value of the item to store, must be serializable.
119
     * @param null|int $ttl   Optional. The TTL value of this item. If no value is sent and
120
     *                                      the driver supports TTL then the library may set a default value
121
     *                                      for it or let the driver take care of that.
122
     *
123
     * @return bool True on success and false on failure.
124
     *
125
     * @throws InvalidArgumentException
126
     *   MUST be thrown if the $key string is not a legal value.
127
     */
128
    public function set_data(string $key, array $value, ?int $ttl = null): bool
129
    {
130
        if ($ttl === null) {
131
            $ttl = 3600;
132
        }
133
 
134
        // place internal cache expiration time
135
        $value['__cache_expiration_time'] = time() + $ttl;
136
 
137
        return $this->cache->save($value);
138
    }
139
 
140
    /**
141
     * Delete an item from the cache by its unique key.
142
     *
143
     * Equivalent to \Psr\SimpleCache\CacheInterface::delete()
144
     * <code>
145
     * public function delete(string $key): bool;
146
     * </code>
147
     *
148
     * @param string $key The unique cache key of the item to delete.
149
     *
150
     * @return bool True if the item was successfully removed. False if there was an error.
151
     *
152
     * @throws InvalidArgumentException
153
     *   MUST be thrown if the $key string is not a legal value.
154
     */
155
    public function delete_data(string $key): bool
156
    {
157
        return $this->cache->unlink();
158
    }
159
}