bedrock-caches
Advanced tools
Comparing version 0.0.3 to 0.0.4
/// <reference path="typings/index.d.ts" /> | ||
declare namespace Bedrock { | ||
export interface Cache<K, V> { | ||
export interface Cache<K, V> extends NodeJS.EventEmitter { | ||
fetch(key: K): V | ||
@@ -6,0 +8,0 @@ get(key: K): V |
/// <reference path="../interfaces.d.ts" /> | ||
/// <reference path="../typings/index.d.ts" /> | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var JSONFileCache = (function () { | ||
var events_1 = require("events"); | ||
var JSONFileCache = (function (_super) { | ||
__extends(JSONFileCache, _super); | ||
function JSONFileCache(options) { | ||
_super.call(this); | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error("must provide an options object"); | ||
this.fetch = options.fetch; | ||
this.fetch = options.fetch || null; | ||
this.cacheDir = options.cacheDir; | ||
@@ -23,4 +31,6 @@ } | ||
var file = path.join(this.cacheDir, key); | ||
if (fs.existsSync(file)) | ||
if (fs.existsSync(file)) { | ||
fs.unlinkSync(file); | ||
this.emit('evict', JSON.parse(fs.readFileSync(file).toString())); | ||
} | ||
}; | ||
@@ -31,4 +41,4 @@ JSONFileCache.prototype.hasCached = function (key) { | ||
return JSONFileCache; | ||
}()); | ||
}(events_1.EventEmitter)); | ||
exports.JSONFileCache = JSONFileCache; | ||
//# sourceMappingURL=json.js.map |
/// <reference path="../interfaces.d.ts" /> | ||
/// <reference path="../typings/index.d.ts" /> | ||
"use strict"; | ||
var MemoryCache = (function () { | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var events_1 = require("events"); | ||
var MemoryCache = (function (_super) { | ||
__extends(MemoryCache, _super); | ||
function MemoryCache(options) { | ||
_super.call(this); | ||
this.cached = new Map(); | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error("must provide an options object"); | ||
this.fetch = options.fetch; | ||
options = options || {}; | ||
this.fetch = options.fetch || null; | ||
} | ||
@@ -18,3 +26,7 @@ MemoryCache.prototype.get = function (key) { | ||
MemoryCache.prototype.evict = function (key) { | ||
return this.cached.remove(key); | ||
if (this.cached.has(key)) { | ||
var value = this.cached.get(key); | ||
this.cached.delete(key); | ||
this.emit('evict', value); | ||
} | ||
}; | ||
@@ -25,4 +37,4 @@ MemoryCache.prototype.hasCached = function (key) { | ||
return MemoryCache; | ||
}()); | ||
}(events_1.EventEmitter)); | ||
exports.MemoryCache = MemoryCache; | ||
//# sourceMappingURL=memory.js.map |
/// <reference path="../interfaces.d.ts" /> | ||
/// <reference path="../typings/index.d.ts" /> | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var events_1 = require("events"); | ||
/** | ||
* A cache wrapper which evicts a value if the value is changed. | ||
*/ | ||
var UpdateCache = (function () { | ||
var UpdateCache = (function (_super) { | ||
__extends(UpdateCache, _super); | ||
function UpdateCache(options) { | ||
var _this = this; | ||
_super.call(this); | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error("must provide an options object"); | ||
this.cache = options.cache; | ||
this.fetch = this.cache.fetch; | ||
this.fetch = this.cache.fetch || null; | ||
this.cache.on('evict', function (val) { return _this.emit('evict', val); }); | ||
} | ||
@@ -29,4 +39,4 @@ UpdateCache.prototype.get = function (id) { | ||
return UpdateCache; | ||
}()); | ||
}(events_1.EventEmitter)); | ||
exports.UpdateCache = UpdateCache; | ||
//# sourceMappingURL=update.js.map |
{ | ||
"name": "bedrock-caches", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A collection of cache implementations for NodeJS", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Bedrock Cache Collection | ||
======================== | ||
This is a collection of cache implementations for use in your applciation, e.g. | ||
This is a collection of cache implementations for use in your application, e.g. | ||
for caching template data. Modules are seperated into several files so only the | ||
@@ -35,15 +35,15 @@ things you need get loaded. | ||
### cache.hasCached(key) | ||
#### cache.hasCached(key) | ||
Checks if the value is added to the local cache. | ||
### cache.fetch(key) | ||
#### cache.fetch(key) | ||
Does a direct fetch for the given key. The value is not stored in the cache. | ||
### cache.evict(key) | ||
#### cache.evict(key) | ||
Removes the value stored at `key` from the cache. Will not report an error if no values were found. | ||
### new MemoryCache(options) | ||
#### new MemoryCache(options) | ||
@@ -50,0 +50,0 @@ Creates a new cache that will store values in-memory. |
@@ -7,4 +7,5 @@ | ||
import * as fs from "fs" | ||
import { EventEmitter } from "events" | ||
export class JSONFileCache<V extends Object> implements Bedrock.Cache<string, Object> { | ||
export class JSONFileCache<V extends Object> implements Bedrock.Cache<string, Object> extends EventEmitter { | ||
@@ -18,5 +19,6 @@ cacheDir: string | ||
}) { | ||
super() | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error(`must provide an options object`) | ||
this.fetch = options.fetch | ||
this.fetch = options.fetch || null | ||
this.cacheDir = options.cacheDir | ||
@@ -36,4 +38,6 @@ } | ||
const file = path.join(this.cacheDir, key) | ||
if (fs.existsSync(file)) | ||
if (fs.existsSync(file)) { | ||
fs.unlinkSync(file) | ||
this.emit('evict', JSON.parse(fs.readFileSync(file).toString())) | ||
} | ||
} | ||
@@ -40,0 +44,0 @@ |
/// <reference path="../interfaces.d.ts" /> | ||
/// <reference path="../typings/index.d.ts" /> | ||
export class MemoryCache<K, V> implements Bedrock.Cache<K, V> { | ||
import { EventEmitter } from "events" | ||
export class MemoryCache<K, V> extends EventEmitter implements Bedrock.Cache<K, V> { | ||
private cached = new Map<K, V>() | ||
@@ -11,5 +14,5 @@ | ||
constructor(options) { | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error(`must provide an options object`) | ||
this.fetch = options.fetch | ||
super() | ||
options = options || {} | ||
this.fetch = options.fetch || null | ||
} | ||
@@ -26,3 +29,7 @@ | ||
evict(key: K) { | ||
return this.cached.remove(key) | ||
if (this.cached.has(key)) { | ||
const value = this.cached.get(key) | ||
this.cached.delete(key) | ||
this.emit('evict', value) | ||
} | ||
} | ||
@@ -29,0 +36,0 @@ |
@@ -6,6 +6,8 @@ | ||
import { EventEmitter } from "events" | ||
/** | ||
* A cache wrapper which evicts a value if the value is changed. | ||
*/ | ||
export class UpdateCache<K, V extends NodeJS.EventEmitter> implements Bedrock.Cache<K, V> { | ||
export class UpdateCache<K, V extends EventEmitter> extends EventEmitter implements Bedrock.Cache<K, V> { | ||
@@ -17,8 +19,9 @@ fetch: (key: K) => V | ||
cache?: Bedrock.Cache<K, V> | ||
fetch?: (key: K) => V | ||
}) { | ||
super() | ||
if (typeof options !== 'object' && typeof options !== 'function') | ||
throw new Error(`must provide an options object`) | ||
this.cache = options.cache | ||
this.fetch = this.cache.fetch | ||
this.fetch = this.cache.fetch || null | ||
this.cache.on('evict', val => this.emit('evict', val)) | ||
} | ||
@@ -25,0 +28,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
128525
24
2752