1 |
efrain |
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.
|
|
|
12 |
//
|
|
|
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/>.
|
|
|
15 |
|
|
|
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) {
|
|
|
26 |
|
|
|
27 |
// Private functions and variables.
|
|
|
28 |
/** @var {Object} StorageWrapper - Wraps browsers localStorage object */
|
|
|
29 |
var storage = new StorageWrapper(window.localStorage);
|
|
|
30 |
|
|
|
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 |
},
|
|
|
42 |
|
|
|
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 |
}
|
|
|
54 |
|
|
|
55 |
};
|
|
|
56 |
});
|