@koopjs/cache-memory
Advanced tools
Comparing version
@@ -5,2 +5,6 @@ # Change Log | ||
## [1.2.0] - 2022-06-23 | ||
### Changed | ||
* Fixed an issue where some geojson properties (like `crs`) were lost in the caching procedure | ||
## [1.1.2] - 2022-04-15 | ||
@@ -30,2 +34,3 @@ ### Changed | ||
[1.2.0]: https://github.com/koopjs/koop-cache-memory/releases/tag/v1.2.0 | ||
[1.1.2]: https://github.com/koopjs/koop-cache-memory/releases/tag/v1.1.2 | ||
@@ -32,0 +37,0 @@ [1.1.1]: https://github.com/koopjs/koop-cache-memory/releases/tag/v1.1.1 |
{ | ||
"name": "@koopjs/cache-memory", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "An in-memory cache for KOop", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const Util = require('util') | ||
const EventEmitter = require('events') | ||
const _ = require('lodash') | ||
const { asCachableGeojson } = require('./helper') | ||
const Readable = require('stream').Readable | ||
// Convenience to make callbacks optional in most functions | ||
function noop () {} | ||
function noop() { } | ||
function Cache (options = {}) { | ||
function Cache(options = {}) { | ||
this.store = new Map() | ||
@@ -22,9 +23,8 @@ this.catalog.store = new Map() | ||
Cache.prototype.insert = function (key, geojson, options = {}, callback = noop) { | ||
// support a feature collection or an array of features | ||
if (this.store.has(key)) return callback(new Error('Cache key is already in use')) | ||
const features = geojson.features ? geojson.features : geojson | ||
this.store.set(key, features) | ||
const metadata = geojson.metadata || {} | ||
geojson = asCachableGeojson(geojson); | ||
this.store.set(key, geojson) | ||
const metadata = geojson.metadata; | ||
if (options.ttl) metadata.expires = Date.now() + (options.ttl * 1000) | ||
this.catalog.insert(key, metadata, callback) | ||
this.catalog.insert(key, geojson.metadata, callback) | ||
} | ||
@@ -41,6 +41,5 @@ | ||
Cache.prototype.update = function (key, geojson, options = {}, callback = noop) { | ||
// support a feature collection or an array of features | ||
if (!this.store.has(key)) return callback(new Error('Resource not found')) | ||
const features = geojson.features ? geojson.features : geojson | ||
this.store.set(key, features) | ||
geojson = asCachableGeojson(geojson); | ||
this.store.set(key, geojson) | ||
const existingMetadata = this.catalog.store.get(key) | ||
@@ -53,5 +52,5 @@ const metadata = geojson.metadata || existingMetadata | ||
Cache.prototype.append = function (key, geojson, options = {}, callback = noop) { | ||
const features = geojson.features ? geojson.features : geojson | ||
geojson = asCachableGeojson(geojson); | ||
const existing = this.store.get(key) | ||
this.store.set(key, features.concat(existing)) | ||
existing.features = existing.features.concat(geojson.features) | ||
this.catalog.update(key, { updated: Date.now() }) | ||
@@ -63,5 +62,4 @@ callback() | ||
if (!this.store.has(key)) return callback(new Error('Resource not found')) | ||
const features = this.store.get(key) | ||
const metadata = this.catalog.store.get(key) | ||
const geojson = { type: 'FeatureCollection', metadata, features } | ||
const geojson = this.store.get(key) | ||
geojson.metadata = this.catalog.store.get(key) | ||
callback(null, geojson) | ||
@@ -72,4 +70,4 @@ return geojson | ||
Cache.prototype.createStream = function (key, options = {}) { | ||
const features = this.store.get(key) | ||
return Readable.from(features) | ||
const geojson = this.store.get(key) | ||
return Readable.from(geojson.features) | ||
} | ||
@@ -76,0 +74,0 @@ |
12242
3.75%8
14.29%126
11.5%