PageSourceSearch

https://confluence.ttek.fi/includes/js/object-cache.js

js ttek.fi collected 2026-10-07 19:06:16 UTC 2,472 bytes, 89 lines download raw bytes

1/**
2 * A simple cache manager that supports a
3 * LRU cache invalidation strategy.
4 *
5 * @module confluence/object-cache
6 */
7define('confluence/object-cache', [
8], function(
9) {
10    'use strict';
11
12
13    /**
14     * @constructor
15     * @param cacheSize the size of the cache before keys are invalidated
16     * @alias module:confluence/object-cache
17     */
18    var CacheManager = function(cacheSize) {
19        // protect against non-new invocation;
20        if (!(this instanceof CacheManager)) {
21            return new CacheManager(cacheSize);
22        }
23
24        this.cache = {};
25        this.cacheStack = [];
26        this.cacheSize = cacheSize || 30;
27    };
28
29    /**
30     * Return the value stored in the cache for the given key and bump weight of this cacheValue.
31     * @method
32     * @param key {String}
33     */
34    CacheManager.prototype.get = function(key) {
35        var cacheObject = this.cache[key];
36        if (!cacheObject) {
37            return;
38        }
39
40        var nextWeight = this.cacheStack[this.cacheStack.length - 1][0] + 1;
41
42        cacheObject.weight = nextWeight;
43        this.cacheStack.push([nextWeight, key]);
44
45        return cacheObject.value;
46    };
47
48    /**
49     * Put the given key, value in the cache
50     * @method
51     * @param key {String}
52     * @param value {Object}
53     */
54    CacheManager.prototype.put = function(key, value) {
55        var nextWeight = this.cacheStack.length ? (this.cacheStack[this.cacheStack.length - 1][0] + 1) : 0;
56        this.cache[key] = {
57            weight: nextWeight,
58            value: value
59        };
60        this.cacheStack.push([nextWeight, key]);
61
62        // Walk the cache stack removing low weighted cache values
63        // Will remove n cacheStack values but a maximum of one cache value
64        var current;
65        var currentWeight;
66        var currentKey;
67        while (this.cacheStack.length > this.cacheSize) {
68            current = this.cacheStack.shift();
69            currentWeight = current[0];
70            currentKey = current[1];
71            if (currentWeight === this.cache[currentKey].weight) {
72                delete this.cache[currentKey];
73            }
74        }
75    };
76
77    /**
78     * Clear the cache.
79     * @method
80     */
81    CacheManager.prototype.clear = function() {
82        this.cache = {};
83        this.cacheStack = [];
84    };
85
86    return CacheManager;
87});
88
89require('confluence/module-exporter').exportModuleAsGlobal('confluence/object-cache', 'AJS.Confluence.cacheManager');

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.