Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions 1.js

This file was deleted.

48 changes: 42 additions & 6 deletions Models/CacheCommands/CacheConnection/SqliteCacheConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import SqliteCacheSetting from "./SqliteCacheSetting.js";
import CacheConnectionBase from "./CacheConnectionBase.js";
import CacheResult from "../../options/CacheResult.js";
import sqlite3 from "sqlite3";
import fs from "fs/promises";
import path from "path";
import crypto from "crypto";

export default class SqliteCacheConnection extends CacheConnectionBase {
/** @type {SqliteCacheSetting} */
Expand All @@ -11,7 +14,7 @@ export default class SqliteCacheConnection extends CacheConnectionBase {
* @param {SqliteCacheSetting} settings
*/
constructor(settings) {
super()
super();
this.settings = settings;
}
/**
Expand All @@ -27,7 +30,18 @@ export default class SqliteCacheConnection extends CacheConnectionBase {
database = new sqlite3.Database(this.settings.dbPath);
const query = `SELECT * FROM ${this.settings.tableName} WHERE key = ?`;
const result = await this.#executeSqliteQuery(database, query, [key]);
return result[0];
let retVal;
try {
retVal =
result[0]?.content && this.settings.isFileBase
? await fs.readFile(
path.join(this.settings.filesPath, result[0]?.content)
)
: result[0];
} catch (error) {
console.log("cache file was deleted for" + key);
}
return retVal;
} finally {
if (database) {
database.close();
Expand All @@ -44,23 +58,45 @@ export default class SqliteCacheConnection extends CacheConnectionBase {
async addCacheContentAsync(key, content, properties) {
let database = new sqlite3.Database(this.settings.dbPath);
try {
const savedContent = await this.loadContentAsync(key);
const selectQuery = `SELECT * FROM ${this.settings.tableName} WHERE key = ?`;
const [savedContent] = await this.#executeSqliteQuery(
database,
selectQuery,
[key]
);
if (savedContent) {
await this.#executeSqliteQuery(
database,
`DELETE FROM ${this.settings.tableName} WHERE key = ?`,
[key]
);
try {
this.settings.isFileBase
? fs.unlink(
path.join(this.settings.filesPath, savedContent?.content)
)
: undefined;
} catch (err) {
}
}
const query = `INSERT INTO ${this.settings.tableName} (key, content, properties) VALUES (?, ?, ?)`;
let filename;
if (this.settings.isFileBase) {
filename =
crypto.createHash("sha256").update(key).digest("hex") + ".bin";
await fs.writeFile(
path.join(this.settings.filesPath, filename),
content
);
}
const result = this.#executeSqliteQuery(database, query, [
key,
content,
filename ? filename : content,
JSON.stringify(properties),
]);
return result;
// } catch (err) {
// throw new Error("error in add cache : " + err);
} catch (err) {
throw new Error("error in add cache : " + err);
} finally {
if (database) {
database.close();
Expand Down
4 changes: 4 additions & 0 deletions Models/CacheCommands/CacheConnection/SqliteCacheSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ export default class SqliteCacheSetting {
dbPath;
/** @type {string} */
tableName
/** @type {boolean} */
isFileBase
/** @type {string} */
filesPath
}
2 changes: 2 additions & 0 deletions endPoint/h2HttpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ export default class H2HttpHostEndPoint extends SecureHttpHostEndPoint {
}

async _checkCacheAsync(stream, headers, next) {
const { query: queryObj } = url.parse(headers[":path"], true);
if (
!queryObj.refresh &&
this._cacheOptions.isEnabled &&
this._cacheOptions.requestMethods.includes(headers[":method"]) &&
this._cacheConnection
Expand Down
2 changes: 2 additions & 0 deletions endPoint/httpHostEndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ class HttpHostEndPoint extends HostEndPoint {
* @param {ServerResponse} res
*/
async _checkCacheAsync(req, res, next) {
const urlObject = url.parse(req.url, true);
if (
!urlObject?.query.refresh &&
this._cacheOptions.isEnabled &&
this._cacheOptions.requestMethods.includes(req.method) &&
this._cacheConnection
Expand Down
2 changes: 1 addition & 1 deletion help.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
https://depth-first.com/articles/2021/10/20/types-without-typescript/
class :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
must have the imageMagick library : https://imagemagick.org/script/download.php (recommended : ImageMagick-7.1.1-31-Q16-HDRI-x64)
the imagemagick path should ber added to enviorment variables to use fileStreamer and index 4
the imagemagick path should ber added to enviorment variables to use fileStreamer and index 4
Loading