source: trip-planner-front/node_modules/stylus/lib/cache/memory.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.1 KB
Line 
1/**
2 * Module dependencies.
3 */
4
5var crypto = require('crypto')
6 , nodes = require('../nodes');
7
8var MemoryCache = module.exports = function(options) {
9 options = options || {};
10 this.limit = options['cache limit'] || 256;
11 this._cache = {};
12 this.length = 0;
13 this.head = this.tail = null;
14};
15
16/**
17 * Set cache item with given `key` to `value`.
18 *
19 * @param {String} key
20 * @param {Object} value
21 * @api private
22 */
23
24MemoryCache.prototype.set = function(key, value) {
25 var clone = value.clone()
26 , item;
27
28 clone.filename = nodes.filename;
29 clone.lineno = nodes.lineno;
30 clone.column = nodes.column;
31 item = { key: key, value: clone };
32 this._cache[key] = item;
33
34 if (this.tail) {
35 this.tail.next = item;
36 item.prev = this.tail;
37 } else {
38 this.head = item;
39 }
40
41 this.tail = item;
42 if (this.length++ == this.limit) this.purge();
43};
44
45/**
46 * Get cache item with given `key`.
47 *
48 * @param {String} key
49 * @return {Object}
50 * @api private
51 */
52
53MemoryCache.prototype.get = function(key) {
54 var item = this._cache[key]
55 , val = item.value.clone();
56
57 if (item == this.tail) return val;
58 if (item.next) {
59 if (item == this.head) this.head = item.next;
60 item.next.prev = item.prev;
61 }
62 if (item.prev) item.prev.next = item.next;
63
64 item.next = null;
65 item.prev = this.tail;
66
67 if (this.tail) this.tail.next = item;
68 this.tail = item;
69
70 return val;
71};
72
73/**
74 * Check if cache has given `key`.
75 *
76 * @param {String} key
77 * @return {Boolean}
78 * @api private
79 */
80
81MemoryCache.prototype.has = function(key) {
82 return !!this._cache[key];
83};
84
85/**
86 * Generate key for the source `str` with `options`.
87 *
88 * @param {String} str
89 * @param {Object} options
90 * @return {String}
91 * @api private
92 */
93
94MemoryCache.prototype.key = function(str, options) {
95 var hash = crypto.createHash('sha1');
96 hash.update(str + options.prefix);
97 return hash.digest('hex');
98};
99
100/**
101 * Remove the oldest item from the cache.
102 *
103 * @api private
104 */
105
106MemoryCache.prototype.purge = function() {
107 var item = this.head;
108
109 if (this.head.next) {
110 this.head = this.head.next;
111 this.head.prev = null;
112 }
113
114 this._cache[item.key] = item.prev = item.next = null;
115 this.length--;
116};
Note: See TracBrowser for help on using the repository browser.