Blame view

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