Merge branch 'MDL-81713-main' of https://github.com/junpataleta/moodle
[moodle.git] / lib / amd / src / localstorage.js
blobb335b98f7bb4acbd55c8880fe3a8932caf86b802
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Simple API for set/get to localstorage, with cacherev expiration.
18  *
19  * @module     core/localstorage
20  * @class      localstorage
21  * @copyright  2015 Damyon Wiese <damyon@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  * @since      2.9
24  */
25 define(['core/config', 'core/storagewrapper'], function(config, StorageWrapper) {
27     // Private functions and variables.
28     /** @var {Object} StorageWrapper - Wraps browsers localStorage object */
29     var storage = new StorageWrapper(window.localStorage);
31     return /** @alias module:core/localstorage */ {
32         /**
33          * Get a value from local storage. Remember - all values must be strings.
34          *
35          * @method get
36          * @param {string} key The cache key to check.
37          * @return {boolean|string} False if the value is not in the cache, or some other error - a string otherwise.
38          */
39         get: function(key) {
40             return storage.get(key);
41         },
43         /**
44          * Set a value to local storage. Remember - all values must be strings.
45          *
46          * @method set
47          * @param {string} key The cache key to set.
48          * @param {string} value The value to set.
49          * @return {boolean} False if the value can't be saved in the cache, or some other error - true otherwise.
50          */
51         set: function(key, value) {
52             return storage.set(key, value);
53         }
55     };
56 });