ravendb
Version:
RavenDB client for Node.js
89 lines • 2.34 kB
JavaScript
import { safeMemoryCache } from "safe-memory-cache";
const NOT_FOUND_RESPONSE = "404 Response";
export class HttpCache {
_items;
constructor(maxKeysSize = 500) {
this._items = safeMemoryCache({
limit: maxKeysSize
});
}
dispose() {
this._items.clear();
this._items = null;
}
clear() {
this._items.clear();
}
set(url, changeVector, result) {
const httpCacheItem = new HttpCacheItem();
httpCacheItem.changeVector = changeVector;
httpCacheItem.payload = result;
httpCacheItem.cache = this;
this._items.set(url, httpCacheItem);
}
get(url, itemInfoCallback) {
const item = this._items.get(url);
if (item) {
if (itemInfoCallback) {
itemInfoCallback({
changeVector: item.changeVector,
response: item.payload
});
}
return new ReleaseCacheItem(item);
}
if (itemInfoCallback) {
itemInfoCallback({
changeVector: null,
response: null
});
}
return new ReleaseCacheItem(null);
}
setNotFound(url) {
const httpCacheItem = new HttpCacheItem();
httpCacheItem.changeVector = NOT_FOUND_RESPONSE;
httpCacheItem.cache = this;
this._items.set(url, httpCacheItem);
}
get numberOfItems() {
return this._items["_get_buckets"]().reduce((result, next) => {
return result + next.size;
}, 0);
}
getMightHaveBeenModified() {
return false; // TBD
}
}
export class ReleaseCacheItem {
item;
constructor(item) {
this.item = item;
}
notModified() {
if (this.item) {
this.item.lastServerUpdate = new Date();
}
}
// returns millis
get age() {
if (!this.item) {
return Number.MAX_VALUE;
}
return Date.now() - this.item.lastServerUpdate.valueOf();
}
get mightHaveBeenModified() {
return false; // TBD
}
}
export class HttpCacheItem {
changeVector;
payload;
lastServerUpdate;
flags;
cache;
constructor() {
this.lastServerUpdate = new Date();
}
}
//# sourceMappingURL=HttpCache.js.map