File: Cache.js

package info (click to toggle)
movabletype-opensource 4.2.3-1%2Blenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 21,268 kB
  • ctags: 15,862
  • sloc: perl: 178,892; php: 26,178; sh: 161; makefile: 82
file content (293 lines) | stat: -rw-r--r-- 8,194 bytes parent folder | download | duplicates (2)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
LRU Cache Library
$Id: Cache.js 159 2007-04-24 01:05:37Z ydnar $

Copyright (c) 2005, Six Apart, Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

    * Neither the name of "Six Apart" nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

Cache = new Class( Object, {
    maxLength: 100,
    hits: 0,
    misses: 0,
    
    
    init: function( maxLength ) {
        if ( maxLength > 0 )
            this.maxLength = maxLength;
        this.flush();
    },


    /* public interface */

    /* flush the cache */
    flush: function() {
        /* xxx should this clear the hits and miss? */
        
        this.length = 0;
        
        /* least recently used */
        this.LRU = 0;
        
        /* most recently used */
        this.MRU = 0;
        
        /* idx to position in value,prev,next arrays */
        this.IDX = {};
        
        /* each node has the same offset in: */
        this.KEY = [];
        this.VALUE = [];
        this.PREV = [];
        this.NEXT = [];
        this.DELETE = [];
    },


    getItemsOrdered: function( offset, count ) {
        offset = offset || 0;
        count = count || this.length;

        var keys = [];
        var values = [];
        
        var idx = this.MRU;
        var c = 0;
        for( var i = 0; i < this.length; i++ ) {
            /* walk until it reaches the needed offset */
            if ( i < offset ) {
                idx = this.NEXT[ idx ];
                continue;
            }
            
            keys.push( this.KEY[ idx ] );
            values.push( this.VALUE[ idx ] );
            
            c++;
            /* keep pulling keys/values until count is reached, or end of array */
            if ( c >= count )
                break;
            
            idx = this.NEXT[ idx ];
        }

        return [ keys, values ];
    },


    getItems_: function() {
        /* xxx does not account for order AND undef values due to deletion */
        /* use getItemsOrdered if that behavior is needed */
        return [ this.KEY, this.VALUE ];
    },


    deleteItem: function( key ) {
        if ( !this.IDX.hasOwnProperty( key ) )
            return undefined;
        var value = this.VALUE[ this.IDX[ key ] ];
        this.deleteNode( key );
        return value;
    },


    setItem: function( key, value) {
        if ( !defined( key ) || !defined( value ) )
            return undefined;
       
        var idx;
        if ( this.IDX.hasOwnProperty( key ) )
            idx = this.deleteNode( key );
        else if ( this.length >= this.maxLength && defined( this.KEY[ this.LRU ] ) )
            idx = this.deleteNode( this.KEY[ this.LRU ] );
        else if ( this.KEY.length && this.KEY[ this.LRU ] == undefined )
            idx = this.LRU;
            
        this.insertNode( key, value, idx );
        return value;
    }, 
 
 
    getItem: function( key ) {
        /* index of node requested */
        var idx = this.IDX[ key ];
            
        /* does this node exist */
        if ( !this.IDX.hasOwnProperty( key ) || idx < 0 || idx >= this.length || !defined( this.VALUE[ idx ] ) ) {
            /* cache miss stat */
            this.misses++;
            return undefined;
        }
        
        /* cache hit stat */
        this.hits++;

        /* move it to the front */
        this.setMRU( idx );
        
        return this.VALUE[ idx ];
    },


    getItems: function( ids ) {
        
        var items = [];
        for ( var i = 0; i < ids.length; i++ ) {
            var item = this.getItem( ids[ i ] );
            if ( item )
                items.push( item );
        }
        
        return items;
    },


    touchItem: function( key ) {
        /* index of node requested */
        var idx = this.IDX[ key ];
            
        /* does this node exist */
        if ( !this.IDX.hasOwnProperty(key) || idx < 0 || idx >= this.length || !defined( this.VALUE[ idx ] ) )
            return undefined;
        
        /* move it to the front */
        this.setMRU( idx );
    },


    /* private functions */

    setMRU: function( idx ) {
        var prevnode = this.PREV[ idx ];
        var nextnode = this.NEXT[ idx ];
        
        if (prevnode == -1) {
            /* this can happen if you select the mru */
            if (this.MRU != idx)
                log.error("LRUCache::setMRU idx:" + idx + " has an inconsistent PREV key (PREV:-1 but MRU != idx)");
            return;
        }
        
        this.connectNodes( prevnode, nextnode );
            
        /* make this node the mru by making the current mru a peer of this node */
        this.PREV[ this.MRU ] = idx;
        this.NEXT[ idx ] = this.MRU;
        
        this.PREV[ idx ] = -1;
        this.MRU = idx;
    },


    setLRU: function( idx ) {
        var nextnode = this.NEXT[ idx ];
        var prevnode = this.PREV[ idx ];
        
        if (nextnode == -1) {
            if (this.LRU != idx)
                log.error("LRUCache::setLRU  idx:" + idx + " has an inconsistent NEXT key (NEXT:-1 but LRU != idx)");
            return;
        }
        
        this.connectNodes( prevnode, nextnode );
        
        /* move it to the end */
        this.NEXT[ this.LRU ] = idx;
        this.PREV[ idx ] = this.LRU;
        
        this.NEXT[ idx ] = -1;
        this.LRU = idx;
    },


    connectNodes: function( prevnode, nextnode ) {
        /* match peers to each other */
        
        if (prevnode == -1)
            this.MRU = nextnode;
        else
            this.NEXT[ prevnode ] = nextnode;
        
        
        if (nextnode == -1)
            this.LRU = prevnode;
        else
            this.PREV[ nextnode ] = prevnode;

    },


    /* reposition a nodes peers and return the index */  
    deleteNode: function( key ) {
        var idx = this.IDX[ key ];
        
        /* move it to the end */
        this.setLRU( idx );
        
        delete this.IDX[ key ];
        
        this.KEY[ idx ] = undefined;
        this.VALUE[ idx ] = undefined;
        
        /* the node isnt actually deleted, it is reused */
        return idx;
    },

 
    insertNode: function( key, value, idx ) {
        /* insert new node */
        if ( !defined( idx ) )
            idx = this.length++;
        
        /* move it to the front */
        this.setMRU( idx );
        
        this.VALUE[ idx ] = value;
        this.KEY[ idx ] = key;
        this.IDX[ key ] = idx;

        return idx;
    },


    /* for debugging */
    visualize: function() {
        var c = this.LRU;
        log.warn( "LRUCache::visualize MRU: " + c );
        for( var i = 0; i < this.length; i++ ) {
            log.warn( "LRUCache::visualize [ " + this.KEY[ c ] + " ] " + c +
                ((this.LRU == c) ? " - LRU" : "") + ((this.MRU == c) ? " - MRU" : ""));
            c = this.PREV[ c ];
        }
    }
});