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-2016 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 Redis as NativeRedis;
49
 
50
/**
51
 * Caches data to redis
52
 *
53
 * Registered for URLs with the "redis" protocol
54
 *
55
 * For example, `redis://localhost:6379/?timeout=3600&prefix=sp_&dbIndex=0` will
56
 * connect to redis on `localhost` on port 6379. All tables will be
57
 * prefixed with `simple_primary-` and data will expire after 3600 seconds
58
 *
59
 * @package SimplePie
60
 * @subpackage Caching
61
 * @uses Redis
62
 * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
63
 */
64
class Redis implements Base
65
{
66
    /**
67
     * Redis instance
68
     *
69
     * @var NativeRedis
70
     */
71
    protected $cache;
72
 
73
    /**
74
     * Options
75
     *
76
     * @var array
77
     */
78
    protected $options;
79
 
80
    /**
81
     * Cache name
82
     *
83
     * @var string
84
     */
85
    protected $name;
86
 
87
    /**
88
     * Create a new cache object
89
     *
90
     * @param string $location Location string (from SimplePie::$cache_location)
91
     * @param string $name Unique ID for the cache
92
     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
93
     */
94
    public function __construct($location, $name, $options = null)
95
    {
96
        //$this->cache = \flow\simple\cache\Redis::getRedisClientInstance();
97
        $parsed = \SimplePie\Cache::parse_URL($location);
98
        $redis = new NativeRedis();
99
        $redis->connect($parsed['host'], $parsed['port']);
100
        if (isset($parsed['pass'])) {
101
            $redis->auth($parsed['pass']);
102
        }
103
        if (isset($parsed['path'])) {
104
            $redis->select((int)substr($parsed['path'], 1));
105
        }
106
        $this->cache = $redis;
107
 
108
        if (!is_null($options) && is_array($options)) {
109
            $this->options = $options;
110
        } else {
111
            $this->options = [
112
                'prefix' => 'rss:simple_primary:',
113
                'expire' => 0,
114
            ];
115
        }
116
 
117
        $this->name = $this->options['prefix'] . $name;
118
    }
119
 
120
    /**
121
     * @param NativeRedis $cache
122
     */
123
    public function setRedisClient(NativeRedis $cache)
124
    {
125
        $this->cache = $cache;
126
    }
127
 
128
    /**
129
     * Save data to the cache
130
     *
131
     * @param array|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
132
     * @return bool Successfulness
133
     */
134
    public function save($data)
135
    {
136
        if ($data instanceof \SimplePie\SimplePie) {
137
            $data = $data->data;
138
        }
139
        $response = $this->cache->set($this->name, serialize($data));
140
        if ($this->options['expire']) {
141
            $this->cache->expire($this->name, $this->options['expire']);
142
        }
143
 
144
        return $response;
145
    }
146
 
147
    /**
148
     * Retrieve the data saved to the cache
149
     *
150
     * @return array Data for SimplePie::$data
151
     */
152
    public function load()
153
    {
154
        $data = $this->cache->get($this->name);
155
 
156
        if ($data !== false) {
157
            return unserialize($data);
158
        }
159
        return false;
160
    }
161
 
162
    /**
163
     * Retrieve the last modified time for the cache
164
     *
165
     * @return int Timestamp
166
     */
167
    public function mtime()
168
    {
169
        $data = $this->cache->get($this->name);
170
 
171
        if ($data !== false) {
172
            return time();
173
        }
174
 
175
        return false;
176
    }
177
 
178
    /**
179
     * Set the last modified time to the current time
180
     *
181
     * @return bool Success status
182
     */
183
    public function touch()
184
    {
185
        $data = $this->cache->get($this->name);
186
 
187
        if ($data !== false) {
188
            $return = $this->cache->set($this->name, $data);
189
            if ($this->options['expire']) {
190
                return $this->cache->expire($this->name, $this->options['expire']);
191
            }
192
            return $return;
193
        }
194
 
195
        return false;
196
    }
197
 
198
    /**
199
     * Remove the cache
200
     *
201
     * @return bool Success status
202
     */
203
    public function unlink()
204
    {
205
        return $this->cache->set($this->name, null);
206
    }
207
}
208
 
209
class_alias('SimplePie\Cache\Redis', 'SimplePie_Cache_Redis');