This commit is contained in:
Alan K 2015-07-16 17:56:12 -04:00
parent 047536c013
commit 4de8bc4b81
4 changed files with 38 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "filer",
"version": "0.0.42",
"version": "0.0.43",
"main": "dist/filer.js",
"ignore": [
"build",

46
dist/filer.js vendored
View File

@ -3716,11 +3716,13 @@ Object.defineProperty(LRUCache.prototype, "itemCount",
LRUCache.prototype.forEach = function (fn, thisp) {
thisp = thisp || this
var i = 0;
for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
var i = 0
var itemCount = this._itemCount
for (var k = this._mru - 1; k >= 0 && i < itemCount; k--) if (this._lruList[k]) {
i++
var hit = this._lruList[k]
if (this._maxAge && (Date.now() - hit.now > this._maxAge)) {
if (isStale(this, hit)) {
del(this, hit)
if (!this._allowStale) hit = undefined
}
@ -3775,19 +3777,24 @@ LRUCache.prototype.dumpLru = function () {
return this._lruList
}
LRUCache.prototype.set = function (key, value) {
LRUCache.prototype.set = function (key, value, maxAge) {
maxAge = maxAge || this._maxAge
var now = maxAge ? Date.now() : 0
if (hOP(this._cache, key)) {
// dispose of the old one before overwriting
if (this._dispose) this._dispose(key, this._cache[key].value)
if (this._maxAge) this._cache[key].now = Date.now()
if (this._dispose)
this._dispose(key, this._cache[key].value)
this._cache[key].now = now
this._cache[key].maxAge = maxAge
this._cache[key].value = value
this.get(key)
return true
}
var len = this._lengthCalculator(value)
var age = this._maxAge ? Date.now() : 0
var hit = new Entry(key, value, this._mru++, len, age)
var hit = new Entry(key, value, this._mru++, len, now, maxAge)
// oversized objects fall out of cache automatically.
if (hit.length > this._max) {
@ -3799,14 +3806,16 @@ LRUCache.prototype.set = function (key, value) {
this._lruList[hit.lu] = this._cache[key] = hit
this._itemCount ++
if (this._length > this._max) trim(this)
if (this._length > this._max)
trim(this)
return true
}
LRUCache.prototype.has = function (key) {
if (!hOP(this._cache, key)) return false
var hit = this._cache[key]
if (this._maxAge && (Date.now() - hit.now > this._maxAge)) {
if (isStale(this, hit)) {
return false
}
return true
@ -3833,7 +3842,7 @@ LRUCache.prototype.del = function (key) {
function get (self, key, doUse) {
var hit = self._cache[key]
if (hit) {
if (self._maxAge && (Date.now() - hit.now > self._maxAge)) {
if (isStale(self, hit)) {
del(self, hit)
if (!self._allowStale) hit = undefined
} else {
@ -3844,6 +3853,18 @@ function get (self, key, doUse) {
return hit
}
function isStale(self, hit) {
if (!hit || (!hit.maxAge && !self._maxAge)) return false
var stale = false;
var diff = Date.now() - hit.now
if (hit.maxAge) {
stale = diff > hit.maxAge
} else {
stale = self._maxAge && (diff > self._maxAge)
}
return stale;
}
function use (self, hit) {
shiftLU(self, hit)
hit.lu = self._mru ++
@ -3871,12 +3892,13 @@ function del (self, hit) {
}
// classy, since V8 prefers predictable objects.
function Entry (key, value, lu, length, now) {
function Entry (key, value, lu, length, now, maxAge) {
this.key = key
this.value = value
this.lu = lu
this.length = length
this.now = now
if (maxAge) this.maxAge = maxAge
}
})()

4
dist/filer.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -11,7 +11,7 @@
"idb",
"websql"
],
"version": "0.0.42",
"version": "0.0.43",
"author": "Alan K <ack@modeswitch.org> (http://blog.modeswitch.org)",
"homepage": "http://filerjs.github.io/filer",
"bugs": "https://github.com/filerjs/filer/issues",