Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement persistent subscription cache #5185

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
13e7870
* Implement persistent subscription cache
PikachuEXE May 28, 2024
7c544c3
! Fix method used in community tab component
PikachuEXE May 29, 2024
63949d1
! Fix community post vote count displayed differently in other window…
PikachuEXE May 29, 2024
b0ec9b0
* Implement persistence for `updateSubscriptionShortsCacheWithChannel…
PikachuEXE May 29, 2024
36a3eed
Merge branch 'development' into feature/subscription-cache
PikachuEXE Jun 3, 2024
0f2c338
Merge branch 'development' into feature/subscription-cache
PikachuEXE Jun 5, 2024
80c322b
! Rename subscriptions to subscription cache & fix outdated mutation …
PikachuEXE Jun 5, 2024
5a08fd0
Merge branch 'development' into feature/subscription-cache
PikachuEXE Jun 20, 2024
dec3b96
Merge branch 'development' into feature/subscription-cache
PikachuEXE Jul 19, 2024
ad0a93e
Merge branch 'development' into feature/subscription-cache
PikachuEXE Aug 21, 2024
1b9c1fd
Merge branch 'development' into feature/subscription-cache
PikachuEXE Sep 3, 2024
553b3fe
$- Remove useless line
PikachuEXE Sep 8, 2024
276ff38
$ Simplify code
PikachuEXE Sep 8, 2024
07d796a
$- Remove unused functions
PikachuEXE Sep 8, 2024
52a4367
Merge branch 'development' into feature/subscription-cache
PikachuEXE Sep 8, 2024
26e2328
* Save community post publish time in absolute time not relative time…
PikachuEXE Sep 9, 2024
680ff0a
Merge branch 'development' into feature/subscription-cache
PikachuEXE Sep 12, 2024
58c6b19
! Fix no timestamp shown after manual refresh
PikachuEXE Sep 16, 2024
8ef7b92
Merge branch 'development' into feature/subscription-cache
PikachuEXE Sep 16, 2024
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
22 changes: 20 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ const IpcChannels = {
DB_HISTORY: 'db-history',
DB_PROFILES: 'db-profiles',
DB_PLAYLISTS: 'db-playlists',
DB_SUBSCRIPTION_CACHE: 'db-subscription-cache',

SYNC_SETTINGS: 'sync-settings',
SYNC_HISTORY: 'sync-history',
SYNC_PROFILES: 'sync-profiles',
SYNC_PLAYLISTS: 'sync-playlists',
SYNC_SUBSCRIPTION_CACHE: 'sync-subscription-cache',

GET_REPLACE_HTTP_CACHE: 'get-replace-http-cache',
TOGGLE_REPLACE_HTTP_CACHE: 'toggle-replace-http-cache',
Expand Down Expand Up @@ -66,7 +68,15 @@ const DBActions = {
DELETE_VIDEO_ID: 'db-action-playlists-delete-video-by-playlist-name',
DELETE_VIDEO_IDS: 'db-action-playlists-delete-video-ids',
DELETE_ALL_VIDEOS: 'db-action-playlists-delete-all-videos',
}
},

SUBSCRIPTION_CACHE: {
UPDATE_VIDEOS_BY_CHANNEL: 'db-action-subscriptions-update-videos-by-channel',
UPDATE_LIVE_STREAMS_BY_CHANNEL: 'db-action-subscriptions-update-live-streams-by-channel',
UPDATE_SHORTS_BY_CHANNEL: 'db-action-subscriptions-update-shorts-by-channel',
UPDATE_SHORTS_WITH_CHANNEL_PAGE_SHORTS_BY_CHANNEL: 'db-action-subscriptions-update-shorts-with-channel-page-shorts-by-channel',
UPDATE_COMMUNITY_POSTS_BY_CHANNEL: 'db-action-subscriptions-update-community-posts-by-channel',
},
}

const SyncEvents = {
Expand All @@ -90,7 +100,15 @@ const SyncEvents = {
PLAYLISTS: {
UPSERT_VIDEO: 'sync-playlists-upsert-video',
DELETE_VIDEO: 'sync-playlists-delete-video',
}
},

SUBSCRIPTION_CACHE: {
UPDATE_VIDEOS_BY_CHANNEL: 'sync-subscriptions-update-videos-by-channel',
UPDATE_LIVE_STREAMS_BY_CHANNEL: 'sync-subscriptions-update-live-streams-by-channel',
UPDATE_SHORTS_BY_CHANNEL: 'sync-subscriptions-update-shorts-by-channel',
UPDATE_SHORTS_WITH_CHANNEL_PAGE_SHORTS_BY_CHANNEL: 'sync-subscriptions-update-shorts-with-channel-page-shorts-by-channel',
UPDATE_COMMUNITY_POSTS_BY_CHANNEL: 'sync-subscriptions-update-community-posts-by-channel',
},
}

// Utils
Expand Down
81 changes: 81 additions & 0 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,92 @@ class Playlists {
}
}

class SubscriptionCache {
static find() {
return db.subscriptionCache.findAsync({})
}

static updateVideosByChannelId({ channelId, entries, timestamp }) {
return db.subscriptionCache.updateAsync(
{ _id: channelId },
{ $set: { videos: entries, videosTimestamp: timestamp } },
{ upsert: true }
)
}

static updateLiveStreamsByChannelId({ channelId, entries, timestamp }) {
return db.subscriptionCache.updateAsync(
{ _id: channelId },
{ $set: { liveStreams: entries, liveStreamsTimestamp: timestamp } },
{ upsert: true }
)
}

static updateShortsByChannelId({ channelId, entries, timestamp }) {
return db.subscriptionCache.updateAsync(
{ _id: channelId },
{ $set: { shorts: entries, shortsTimestamp: timestamp } },
{ upsert: true }
)
}

static updateShortsWithChannelPageShortsByChannelId({ channelId, entries }) {
return db.subscriptionCache.findOneAsync({ _id: channelId }, { shorts: 1 }).then((doc) => {
if (doc == null) { return }

const shorts = doc.shorts
const cacheShorts = Array.isArray(shorts) ? shorts : []

cacheShorts.forEach(cachedVideo => {
const channelVideo = entries.find(short => cachedVideo.videoId === short.videoId)
if (!channelVideo) { return }

// authorId probably never changes, so we don't need to update that
cachedVideo.title = channelVideo.title
cachedVideo.author = channelVideo.author

// as the channel shorts page only has compact view counts for numbers above 1000 e.g. 12k
// and the RSS feeds include an exact value, we only want to overwrite it when the number is larger than the cached value
// 12345 vs 12000 => 12345
// 12345 vs 15000 => 15000

if (channelVideo.viewCount > cachedVideo.viewCount) {
cachedVideo.viewCount = channelVideo.viewCount
}
})

return db.subscriptionCache.updateAsync(
{ _id: channelId },
{ $set: { shorts: cacheShorts } },
{ upsert: true }
)
})
}

static updateCommunityPostsByChannelId({ channelId, entries, timestamp }) {
return db.subscriptionCache.updateAsync(
{ _id: channelId },
{ $set: { communityPosts: entries, communityPostsTimestamp: timestamp } },
{ upsert: true }
)
}

static deleteMultipleChannels(channelIds) {
return db.subscriptionCache.removeAsync({ _id: { $in: channelIds } }, { multi: true })
}

static deleteAll() {
return db.subscriptionCache.removeAsync({}, { multi: true })
}
}

function compactAllDatastores() {
return Promise.allSettled([
db.settings.compactDatafileAsync(),
db.history.compactDatafileAsync(),
db.profiles.compactDatafileAsync(),
db.playlists.compactDatafileAsync(),
db.subscriptionCache.compactDatafileAsync(),
])
}

Expand All @@ -211,6 +291,7 @@ export {
History as history,
Profiles as profiles,
Playlists as playlists,
SubscriptionCache as subscriptionCache,

compactAllDatastores,
}
76 changes: 75 additions & 1 deletion src/datastores/handlers/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,83 @@ class Playlists {
}
}

class SubscriptionCache {
static find() {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{ action: DBActions.GENERAL.FIND }
)
}

static updateVideosByChannelId({ channelId, entries, timestamp }) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{
action: DBActions.SUBSCRIPTION_CACHE.UPDATE_VIDEOS_BY_CHANNEL,
data: { channelId, entries, timestamp },
}
)
}

static updateLiveStreamsByChannelId({ channelId, entries, timestamp }) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{
action: DBActions.SUBSCRIPTION_CACHE.UPDATE_LIVE_STREAMS_BY_CHANNEL,
data: { channelId, entries, timestamp },
}
)
}

static updateShortsByChannelId({ channelId, entries, timestamp }) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{
action: DBActions.SUBSCRIPTION_CACHE.UPDATE_SHORTS_BY_CHANNEL,
data: { channelId, entries, timestamp },
}
)
}

static updateShortsWithChannelPageShortsByChannelId({ channelId, entries }) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{
action: DBActions.SUBSCRIPTION_CACHE.UPDATE_SHORTS_WITH_CHANNEL_PAGE_SHORTS_BY_CHANNEL,
data: { channelId, entries },
}
)
}

static updateCommunityPostsByChannelId({ channelId, entries, timestamp }) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{
action: DBActions.SUBSCRIPTION_CACHE.UPDATE_COMMUNITY_POSTS_BY_CHANNEL,
data: { channelId, entries, timestamp },
}
)
}

static deleteMultipleChannels(channelIds) {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{ action: DBActions.GENERAL.DELETE_MULTIPLE, data: channelIds }
)
}

static deleteAll() {
return ipcRenderer.invoke(
IpcChannels.DB_SUBSCRIPTION_CACHE,
{ action: DBActions.GENERAL.DELETE_ALL }
)
}
}

export {
Settings as settings,
History as history,
Profiles as profiles,
Playlists as playlists
Playlists as playlists,
SubscriptionCache as subscriptionCache,
}
3 changes: 2 additions & 1 deletion src/datastores/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export {
settings as DBSettingHandlers,
history as DBHistoryHandlers,
profiles as DBProfileHandlers,
playlists as DBPlaylistHandlers
playlists as DBPlaylistHandlers,
subscriptionCache as DBSubscriptionCacheHandlers,
} from 'DB_HANDLERS_ELECTRON_RENDERER_OR_WEB'
56 changes: 55 additions & 1 deletion src/datastores/handlers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,63 @@ class Playlists {
}
}

class SubscriptionCache {
static find() {
return baseHandlers.subscriptionCache.find()
}

static updateVideosByChannelId({ channelId, entries, timestamp }) {
return baseHandlers.subscriptionCache.updateVideosByChannelId({
channelId,
entries,
timestamp,
})
}

static updateLiveStreamsByChannelId({ channelId, entries, timestamp }) {
return baseHandlers.subscriptionCache.updateLiveStreamsByChannelId({
channelId,
entries,
timestamp,
})
}

static updateShortsByChannelId({ channelId, entries, timestamp }) {
return baseHandlers.subscriptionCache.updateShortsByChannelId({
channelId,
entries,
timestamp,
})
}

static updateShortsWithChannelPageShortsByChannelId({ channelId, entries }) {
return baseHandlers.subscriptionCache.updateShortsWithChannelPageShortsByChannelId({
channelId,
entries,
})
}

static updateCommunityPostsByChannelId({ channelId, entries, timestamp }) {
return baseHandlers.subscriptionCache.updateCommunityPostsByChannelId({
channelId,
entries,
timestamp,
})
}

static deleteMultipleChannels(channelIds) {
return baseHandlers.subscriptionCache.deleteMultipleChannels(channelIds)
}

static deleteAll() {
return baseHandlers.subscriptionCache.deleteAll()
}
}

export {
Settings as settings,
History as history,
Profiles as profiles,
Playlists as playlists
Playlists as playlists,
SubscriptionCache as subscriptionCache,
}
1 change: 1 addition & 0 deletions src/datastores/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export const settings = new Datastore({ filename: dbPath('settings'), autoload:
export const profiles = new Datastore({ filename: dbPath('profiles'), autoload: true })
export const playlists = new Datastore({ filename: dbPath('playlists'), autoload: true })
export const history = new Datastore({ filename: dbPath('history'), autoload: true })
export const subscriptionCache = new Datastore({ filename: dbPath('subscription-cache'), autoload: true })
Loading