Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
16
 
18
 * The library file for the file cache store.
-
 
19
 *
17
use core_cache\configurable_cache_interface;
20
 * This file is part of the file cache store, it contains the API for interacting with an instance of the store.
18
use core_cache\definition;
21
 * This is used as a default cache store within the Cache API. It should never be deleted.
-
 
22
 *
19
use core_cache\key_aware_cache_interface;
23
 * @package    cachestore_file
20
use core_cache\lockable_cache_interface;
24
 * @category   cache
21
use core_cache\searchable_cache_interface;
25
 * @copyright  2012 Sam Hemelryk
-
 
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
Línea 27... Línea 22...
27
 */
22
use core_cache\store;
28
 
23
 
29
/**
24
/**
30
 * The file store class.
25
 * The file store class.
31
 *
26
 *
32
 * Configuration options
27
 * Configuration options
33
 *      path:           string: path to the cache directory, if left empty one will be created in the cache directory
28
 *      path:           string: path to the cache directory, if left empty one will be created in the cache directory
34
 *      autocreate:     true, false
29
 *      autocreate:     true, false
-
 
30
 *      prescan:        true, false
35
 *      prescan:        true, false
31
 *
36
 *
32
 * @package    cachestore_file
37
 * @copyright  2012 Sam Hemelryk
33
 * @copyright  2012 Sam Hemelryk
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
35
 */
-
 
36
class cachestore_file extends store implements
-
 
37
    key_aware_cache_interface,
39
 */
38
    configurable_cache_interface,
-
 
39
    searchable_cache_interface,
-
 
40
    lockable_cache_interface
-
 
41
{
-
 
42
    /**
-
 
43
     * Value to represent use of the PHP serializer.
-
 
44
     */
-
 
45
    public const SERIALIZER_PHP = 'php';
-
 
46
 
-
 
47
    /**
-
 
48
     * Value to represent use of the Igbinary serializer.
Línea 40... Línea 49...
40
class cachestore_file extends cache_store implements cache_is_key_aware, cache_is_configurable, cache_is_searchable,
49
     */
41
        cache_is_lockable {
50
    public const SERIALIZER_IGBINARY = 'igbinary';
42
 
51
 
43
    /**
52
    /**
Línea 103... Línea 112...
103
     */
112
     */
104
    protected $isready = false;
113
    protected $isready = false;
Línea 105... Línea 114...
105
 
114
 
106
    /**
115
    /**
107
     * The cache definition this instance has been initialised with.
116
     * The cache definition this instance has been initialised with.
108
     * @var cache_definition
117
     * @var definition
109
     */
118
     */
Línea 110... Línea 119...
110
    protected $definition;
119
    protected $definition;
111
 
120
 
Línea 145... Línea 154...
145
     * @var array $locks
154
     * @var array $locks
146
     */
155
     */
147
    protected $locks = [];
156
    protected $locks = [];
Línea 148... Línea 157...
148
 
157
 
-
 
158
    /**
-
 
159
     * Serializer for this store.
-
 
160
     *
-
 
161
     * @var string
-
 
162
     */
-
 
163
    protected $serializer = self::SERIALIZER_PHP;
-
 
164
 
-
 
165
    /**
-
 
166
     * Determine if igbinary functions are available for use.
-
 
167
     *
-
 
168
     * @return boolean
-
 
169
     */
-
 
170
    public static function igbinary_available(): bool {
-
 
171
        return function_exists('igbinary_serialize');
-
 
172
    }
-
 
173
 
-
 
174
    /**
-
 
175
     * Gets an array of options to use as the serialiser.
-
 
176
     *
-
 
177
     * @return array
-
 
178
     */
-
 
179
    public static function config_get_serializer_options(): array {
-
 
180
        $options = [
-
 
181
            self::SERIALIZER_PHP => get_string('serializer_php', 'cachestore_file'),
-
 
182
        ];
-
 
183
        if (self::igbinary_available()) {
-
 
184
            $options[self::SERIALIZER_IGBINARY] = get_string('serializer_igbinary', 'cachestore_file');
-
 
185
        }
-
 
186
        return $options;
-
 
187
    }
-
 
188
 
149
    /**
189
    /**
150
     * Constructs the store instance.
190
     * Constructs the store instance.
151
     *
191
     *
152
     * Noting that this function is not an initialisation. It is used to prepare the store for use.
192
     * Noting that this function is not an initialisation. It is used to prepare the store for use.
153
     * The store will be initialised when required and will be provided with a cache_definition at that time.
193
     * The store will be initialised when required and will be provided with a definition at that time.
154
     *
194
     *
155
     * @param string $name
195
     * @param string $name
156
     * @param array $configuration
196
     * @param array $configuration
157
     */
197
     */
Línea 224... Línea 264...
224
        $this->lockfactory = new \core\lock\file_lock_factory('cachestore_file', $lockdir);
264
        $this->lockfactory = new \core\lock\file_lock_factory('cachestore_file', $lockdir);
225
        if (!$this->lockfactory->is_available()) {
265
        if (!$this->lockfactory->is_available()) {
226
            // File locking is disabled in config, fall back to default lock factory.
266
            // File locking is disabled in config, fall back to default lock factory.
227
            $this->lockfactory = \core\lock\lock_config::get_lock_factory('cachestore_file');
267
            $this->lockfactory = \core\lock\lock_config::get_lock_factory('cachestore_file');
228
        }
268
        }
-
 
269
 
-
 
270
        // Set the serializer to use based on configuration.
-
 
271
        if (array_key_exists('serializer', $configuration)) {
-
 
272
            $this->serializer = (string)$configuration['serializer'];
-
 
273
        }
229
    }
274
    }
Línea 230... Línea 275...
230
 
275
 
231
    /**
276
    /**
232
     * Performs any necessary operation when the file store instance has been created.
277
     * Performs any necessary operation when the file store instance has been created.
Línea 300... Línea 345...
300
    }
345
    }
Línea 301... Línea 346...
301
 
346
 
302
    /**
347
    /**
303
     * Returns true if the given mode is supported by this store.
348
     * Returns true if the given mode is supported by this store.
304
     *
349
     *
305
     * @param int $mode One of cache_store::MODE_*
350
     * @param int $mode One of store::MODE_*
306
     * @return bool
351
     * @return bool
307
     */
352
     */
308
    public static function is_supported_mode($mode) {
353
    public static function is_supported_mode($mode) {
309
        return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION);
354
        return ($mode === static::MODE_APPLICATION || $mode === static::MODE_SESSION);
Línea 310... Línea 355...
310
    }
355
    }
311
 
356
 
312
    /**
357
    /**
313
     * Initialises the cache.
358
     * Initialises the cache.
314
     *
359
     *
315
     * Once this has been done the cache is all set to be used.
360
     * Once this has been done the cache is all set to be used.
316
     *
361
     *
317
     * @param cache_definition $definition
362
     * @param definition $definition
318
     */
363
     */
Línea 319... Línea 364...
319
    public function initialise(cache_definition $definition) {
364
    public function initialise(definition $definition) {
320
        global $CFG;
365
        global $CFG;
321
 
366
 
Línea 548... Línea 593...
548
     *
593
     *
549
     * @param mixed $data
594
     * @param mixed $data
550
     * @return string
595
     * @return string
551
     */
596
     */
552
    protected function prep_data_before_save($data) {
597
    protected function prep_data_before_save($data) {
553
        return serialize($data);
598
        return $this->serialize($data);
554
    }
599
    }
Línea 555... Línea 600...
555
 
600
 
556
    /**
601
    /**
557
     * Prepares the data it has been read from the cache. Undoing what was done in prep_data_before_save.
602
     * Prepares the data it has been read from the cache. Undoing what was done in prep_data_before_save.
558
     *
603
     *
559
     * @param string $data
604
     * @param string $data
560
     * @param string $path
605
     * @param string $path
561
     * @return mixed
606
     * @return mixed
562
     */
607
     */
563
    protected function prep_data_after_read($data, $path) {
608
    protected function prep_data_after_read($data, $path) {
564
        $result = @unserialize($data);
609
        $result = @$this->unserialize($data);
565
        if ($result === false && $data != serialize(false)) {
610
        if ($result === false && $data != @$this->serialize(false)) {
566
            debugging('Failed to unserialise data from cache file: ' . $path . '. Data: ' . $data, DEBUG_DEVELOPER);
611
            debugging('Failed to unserialise data from cache file: ' . $path . '. Data: ' . $data, DEBUG_DEVELOPER);
567
            return false;
612
            return false;
568
        }
613
        }
569
        return $result;
614
        return $result;
Línea 719... Línea 764...
719
            $config['asyncpurge'] = $data->asyncpurge;
764
            $config['asyncpurge'] = $data->asyncpurge;
720
        }
765
        }
721
        if (isset($data->lockwait)) {
766
        if (isset($data->lockwait)) {
722
            $config['lockwait'] = $data->lockwait;
767
            $config['lockwait'] = $data->lockwait;
723
        }
768
        }
-
 
769
        if (isset($data->serializer)) {
-
 
770
            $config['serializer'] = $data->serializer;
-
 
771
        }
Línea 724... Línea 772...
724
 
772
 
725
        return $config;
773
        return $config;
Línea 726... Línea 774...
726
    }
774
    }
Línea 749... Línea 797...
749
            $data['asyncpurge'] = (bool)$config['asyncpurge'];
797
            $data['asyncpurge'] = (bool)$config['asyncpurge'];
750
        }
798
        }
751
        if (isset($config['lockwait'])) {
799
        if (isset($config['lockwait'])) {
752
            $data['lockwait'] = (int)$config['lockwait'];
800
            $data['lockwait'] = (int)$config['lockwait'];
753
        }
801
        }
-
 
802
        if (isset($config['serializer'])) {
-
 
803
            $data['serializer'] = (string)$config['serializer'];
-
 
804
        }
754
        $editform->set_data($data);
805
        $editform->set_data($data);
755
    }
806
    }
Línea 756... Línea 807...
756
 
807
 
757
    /**
808
    /**
Línea 800... Línea 851...
800
    /**
851
    /**
801
     * Generates an instance of the cache store that can be used for testing.
852
     * Generates an instance of the cache store that can be used for testing.
802
     *
853
     *
803
     * Returns an instance of the cache store, or false if one cannot be created.
854
     * Returns an instance of the cache store, or false if one cannot be created.
804
     *
855
     *
805
     * @param cache_definition $definition
856
     * @param definition $definition
806
     * @return cachestore_file
857
     * @return cachestore_file
807
     */
858
     */
808
    public static function initialise_test_instance(cache_definition $definition) {
859
    public static function initialise_test_instance(definition $definition) {
809
        $name = 'File test';
860
        $name = 'File test';
810
        $path = make_cache_directory('cachestore_file_test');
861
        $path = make_cache_directory('cachestore_file_test');
811
        $cache = new cachestore_file($name, array('path' => $path));
862
        $cache = new cachestore_file($name, array('path' => $path));
812
        if ($cache->is_ready()) {
863
        if ($cache->is_ready()) {
813
            $cache->initialise($definition);
864
            $cache->initialise($definition);
Línea 1007... Línea 1058...
1007
     * Use lock factory to acquire a lock.
1058
     * Use lock factory to acquire a lock.
1008
     *
1059
     *
1009
     * @param string $key Lock identifier
1060
     * @param string $key Lock identifier
1010
     * @param string $ownerid Cache identifier
1061
     * @param string $ownerid Cache identifier
1011
     * @return bool
1062
     * @return bool
1012
     * @throws cache_exception
-
 
1013
     */
1063
     */
1014
    public function acquire_lock($key, $ownerid): bool {
1064
    public function acquire_lock($key, $ownerid): bool {
1015
        $lock = $this->lockfactory->get_lock($key, $this->lockwait);
1065
        $lock = $this->lockfactory->get_lock($key, $this->lockwait);
1016
        if ($lock) {
1066
        if ($lock) {
1017
            $this->locks[$key][$ownerid] = $lock;
1067
            $this->locks[$key][$ownerid] = $lock;
Línea 1037... Línea 1087...
1037
        if ($unlocked) {
1087
        if ($unlocked) {
1038
            unset($this->locks[$key]);
1088
            unset($this->locks[$key]);
1039
        }
1089
        }
1040
        return $unlocked;
1090
        return $unlocked;
1041
    }
1091
    }
-
 
1092
 
-
 
1093
    /**
-
 
1094
     * Serializes the data according to the configured serializer.
-
 
1095
     *
-
 
1096
     * @param mixed $value
-
 
1097
     * @return string
-
 
1098
     */
-
 
1099
    protected function serialize($value): string {
-
 
1100
        switch ($this->serializer) {
-
 
1101
            case self::SERIALIZER_PHP:
-
 
1102
                return serialize($value);
-
 
1103
            case self::SERIALIZER_IGBINARY:
-
 
1104
                if (self::igbinary_available()) {
-
 
1105
                    return igbinary_serialize($value);
-
 
1106
                }
-
 
1107
        }
-
 
1108
        debugging("Unknown or unavailable serializer {$this->serializer}");
-
 
1109
        return serialize($value);
-
 
1110
    }
-
 
1111
 
-
 
1112
    /**
-
 
1113
     * Unserializes the data according to the configured serializer.
-
 
1114
     *
-
 
1115
     * @param mixed $value
-
 
1116
     * @return mixed
-
 
1117
     */
-
 
1118
    protected function unserialize($value) {
-
 
1119
        switch ($this->serializer) {
-
 
1120
            case self::SERIALIZER_PHP:
-
 
1121
                return unserialize($value);
-
 
1122
            case self::SERIALIZER_IGBINARY:
-
 
1123
                if (self::igbinary_available()) {
-
 
1124
                    return igbinary_unserialize($value);
-
 
1125
                }
-
 
1126
        }
-
 
1127
        debugging("Unknown or unavailable serializer: {$this->serializer}");
-
 
1128
        return unserialize($value);
-
 
1129
    }
1042
}
1130
}