From 4e0bd16a4ec0947be6bc6d32fa89249496995d80 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 5 Mar 2021 17:01:38 +0100 Subject: [PATCH] reuse LRU Cache for session cache --- .../e2ee/megolm/decryption/SessionCache.js | 42 ++++++++----------- .../e2ee/megolm/decryption/SessionInfo.js | 4 ++ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/matrix/e2ee/megolm/decryption/SessionCache.js b/src/matrix/e2ee/megolm/decryption/SessionCache.js index d6482a01..c5b2c0fb 100644 --- a/src/matrix/e2ee/megolm/decryption/SessionCache.js +++ b/src/matrix/e2ee/megolm/decryption/SessionCache.js @@ -14,15 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ +import {BaseLRUCache} from "../../../../utils/LRUCache.js"; const DEFAULT_CACHE_SIZE = 10; /** * Cache of unpickled inbound megolm session. */ -export class SessionCache { - constructor(size) { - this._size = typeof size === "number" ? size : DEFAULT_CACHE_SIZE; - this._sessions = []; +export class SessionCache extends BaseLRUCache { + constructor(limit) { + limit = typeof limit === "number" ? limit : DEFAULT_CACHE_SIZE; + super(limit); } /** @@ -32,37 +33,28 @@ export class SessionCache { * @return {SessionInfo?} */ get(roomId, senderKey, sessionId) { - const idx = this._sessions.findIndex(s => { + return this._get(s => { return s.roomId === roomId && s.senderKey === senderKey && - sessionId === s.session.session_id(); + sessionId === s.sessionId; }); - if (idx !== -1) { - const sessionInfo = this._sessions[idx]; - // move to top - if (idx > 0) { - this._sessions.splice(idx, 1); - this._sessions.unshift(sessionInfo); - } - return sessionInfo; - } } add(sessionInfo) { sessionInfo.retain(); - // add new at top - this._sessions.unshift(sessionInfo); - if (this._sessions.length > this._size) { - // free sessions we're about to remove - for (let i = this._size; i < this._sessions.length; i += 1) { - this._sessions[i].release(); - } - this._sessions = this._sessions.slice(0, this._size); - } + this._set(sessionInfo, s => { + return s.roomId === sessionInfo.roomId && + s.senderKey === sessionInfo.senderKey && + s.sessionId === sessionInfo.sessionId; + }); + } + + _onEvictEntry(sessionInfo) { + sessionInfo.release(); } dispose() { - for (const sessionInfo of this._sessions) { + for (const sessionInfo of this._entries) { sessionInfo.release(); } } diff --git a/src/matrix/e2ee/megolm/decryption/SessionInfo.js b/src/matrix/e2ee/megolm/decryption/SessionInfo.js index e8bec3d0..098bc3de 100644 --- a/src/matrix/e2ee/megolm/decryption/SessionInfo.js +++ b/src/matrix/e2ee/megolm/decryption/SessionInfo.js @@ -27,6 +27,10 @@ export class SessionInfo { this._refCounter = 0; } + get sessionId() { + return this.session?.session_id(); + } + retain() { this._refCounter += 1; }