PageSourceSearch

https://earsc-portal.eu/includes/js/localstorage-cache.js

js earsc-portal.eu collected 2026-09-28 23:58:00 UTC 2,289 bytes, 65 lines download raw bytes

1/**
2 * A extension of the cacheManager (cache-manager.js) that supports
3 * localstorage as a persistence mechanism.
4 *
5 * @module confluence/localstorage-cache
6 */
7define('confluence/localstorage-cache', [
8    'confluence/object-cache',
9    'jquery',
10    'window'
11], function(CacheManager,
12    $,
13    window) {
14    'use strict';
15
16    /**
17     * @constructor
18     * @param storageKey the localstorage key to store data against.
19     * @param cacheSize size of the cache.
20     *
21     * @extends module:confluence/object-cache
22     * @alias module:confluence/localstorage-cache
23     */
24    var LocalStorageCacheManager = function(storageKey, cacheSize) {
25        this.cache = {};
26        this.cacheStack = [];
27        this.cacheSize = cacheSize || 30;
28
29        // If localStorage doesn't exist or if no storageKey is provided this object behaves as a cacheManager.
30        if (!window.localStorage || (typeof storageKey !== 'string' && storageKey.length > 0)) {
31            return;
32        }
33
34        var localStoreKey = 'Confluence.' + storageKey;
35        var localCacheKey = localStoreKey + '.cache';
36        var localCacheStackKey = localStoreKey + '.cacheStack';
37
38        var cache = window.localStorage.getItem(localCacheKey);
39        var cacheStack = window.localStorage.getItem(localCacheStackKey);
40        var self = this;
41
42        // Must check both of these together. A browser could GC one before the other.
43        if (cache && cacheStack) {
44            try {
45                // On misuse of the API, localstorage keys can get polluted this checks for that and resets values.
46                this.cache = JSON.parse(cache);
47                this.cacheStack = JSON.parse(cacheStack);
48            } catch (e) {
49                this.cache = {};
50                this.cacheStack = {};
51            }
52        }
53
54        $(window).unload(function() {
55            window.localStorage.setItem(localCacheKey, JSON.stringify(self.cache));
56            window.localStorage.setItem(localCacheStackKey, JSON.stringify(self.cacheStack));
57        });
58    };
59
60    LocalStorageCacheManager.prototype = new CacheManager();
61
62    return LocalStorageCacheManager;
63});
64
65require('confluence/module-exporter').exportModuleAsGlobal('confluence/localstorage-cache', 'AJS.Confluence.localStorageCacheManager');

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.