-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hoang Pham <hoangmaths96@gmail.com>
- Loading branch information
Showing
22 changed files
with
3,684 additions
and
1,961 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import StorageStrategy from './StorageStrategy.js' | ||
import { LRUCache } from 'lru-cache' | ||
|
||
export default class LRUCacheStrategy extends StorageStrategy { | ||
|
||
constructor(apiService) { | ||
super() | ||
this.apiService = apiService | ||
this.cache = new LRUCache({ | ||
max: 1000, | ||
ttl: 30 * 60 * 1000, | ||
ttlAutopurge: true, | ||
dispose: async (value, key) => { | ||
console.log('Disposing room', key) | ||
if (value?.data && value?.lastEditedUser) { | ||
try { | ||
await this.apiService.saveRoomDataToServer( | ||
key, | ||
value.data, | ||
value.lastEditedUser, | ||
) | ||
} catch (error) { | ||
console.error(`Failed to save room ${key} data:`, error) | ||
} | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
async get(key) { | ||
return this.cache.get(key) | ||
} | ||
|
||
async set(key, value) { | ||
this.cache.set(key, value) | ||
} | ||
|
||
async delete(key) { | ||
this.cache.delete(key) | ||
} | ||
|
||
async clear() { | ||
this.cache.clear() | ||
} | ||
|
||
getRooms() { | ||
return this.cache | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import StorageStrategy from './StorageStrategy.js' | ||
import { createClient } from 'redis' | ||
import Room from './Room.js' | ||
|
||
export default class RedisStrategy extends StorageStrategy { | ||
|
||
constructor(apiService) { | ||
super() | ||
this.apiService = apiService | ||
this.client = createClient({ | ||
url: process.env.REDIS_URL || 'redis://localhost:6379', | ||
retry_strategy: (options) => { | ||
if (options.error && options.error.code === 'ECONNREFUSED') { | ||
return new Error('The server refused the connection') | ||
} | ||
if (options.total_retry_time > 1000 * 60 * 60) { | ||
return new Error('Retry time exhausted') | ||
} | ||
if (options.attempt > 10) { | ||
return undefined | ||
} | ||
return Math.min(options.attempt * 100, 3000) | ||
}, | ||
}) | ||
this.client.on('error', (err) => | ||
console.error('Redis Client Error', err), | ||
) | ||
this.connect() | ||
} | ||
|
||
async connect() { | ||
try { | ||
await this.client.connect() | ||
} catch (error) { | ||
console.error('Failed to connect to Redis:', error) | ||
throw error | ||
} | ||
} | ||
|
||
async get(key) { | ||
try { | ||
const data = await this.client.get(key) | ||
if (!data) return null | ||
return this.deserialize(data) | ||
} catch (error) { | ||
console.error(`Error getting data for key ${key}:`, error) | ||
return null | ||
} | ||
} | ||
|
||
async set(key, value) { | ||
try { | ||
const serializedData = this.serialize(value) | ||
await this.client.set(key, serializedData, { EX: 30 * 60 }) | ||
} catch (error) { | ||
console.error(`Error setting data for key ${key}:`, error) | ||
} | ||
} | ||
|
||
async delete(key) { | ||
try { | ||
const room = await this.get(key) | ||
if (room?.data && room?.lastEditedUser) { | ||
await this.apiService.saveRoomDataToServer( | ||
key, | ||
room.data, | ||
room.lastEditedUser, | ||
) | ||
} | ||
await this.client.del(key) | ||
} catch (error) { | ||
console.error(`Error deleting key ${key}:`, error) | ||
} | ||
} | ||
|
||
async clear() { | ||
try { | ||
await this.client.flushDb() | ||
} catch (error) { | ||
console.error('Error clearing Redis database:', error) | ||
} | ||
} | ||
|
||
async getRooms() { | ||
try { | ||
const keys = await this.client.keys('*') | ||
const rooms = new Map() | ||
for (const key of keys) { | ||
const room = await this.get(key) | ||
if (room) rooms.set(key, room) | ||
} | ||
return rooms | ||
} catch (error) { | ||
console.error('Error getting rooms:', error) | ||
return new Map() | ||
} | ||
} | ||
|
||
serialize(value) { | ||
return value instanceof Room | ||
? JSON.stringify(value.toJSON()) | ||
: JSON.stringify(value) | ||
} | ||
|
||
deserialize(data) { | ||
const parsedData = JSON.parse(data) | ||
return parsedData.id && parsedData.users | ||
? Room.fromJSON(parsedData) | ||
: parsedData | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
export default class Room { | ||
|
||
constructor(id, data = null, users = new Set(), lastEditedUser = null) { | ||
this.id = id | ||
this.data = data | ||
this.users = new Set(users) | ||
this.lastEditedUser = lastEditedUser | ||
} | ||
|
||
setUsers(users) { | ||
this.users = new Set(users) | ||
} | ||
|
||
updateLastEditedUser(userId) { | ||
this.lastEditedUser = userId | ||
} | ||
|
||
setData(data) { | ||
this.data = data | ||
} | ||
|
||
isEmpty() { | ||
return this.users.size === 0 | ||
} | ||
|
||
toJSON() { | ||
return { | ||
id: this.id, | ||
data: this.data, | ||
users: Array.from(this.users), | ||
lastEditedUser: this.lastEditedUser, | ||
} | ||
} | ||
|
||
static fromJSON(json) { | ||
return new Room( | ||
json.id, | ||
json.data, | ||
new Set(json.users), | ||
json.lastEditedUser, | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.