Skip to content

Commit

Permalink
Add a setting to clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed May 21, 2024
1 parent 0bbb876 commit 55c811a
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- A dev setting to clear the cache

### Fixed

- A bug where bookmarked channels can be duplicated
- A bug where video cast from web app would not play if there's an error dialog on screen

### Changed

- Font weight to be less bold

## [0.23.5] - 2024-05-19

### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/source/utils/CacheUtils.bs"
import "pkg:/source/utils/FocusManagement.bs"
import "pkg:/source/utils/Locale.bs"

function Init()
m.top.focusable = true
m.top.itemSpacings = [8]
m.button = m.top.findNode("Button")
m.button.observeField("buttonSelected", FuncName(OnButtonSelected))
end function

function OnFocusChange() as void
if not m.top.focus
return
end if
NodeSetFocus(m.button, true)
end function

function BindPreference(_preferences as object, _key as string)
' Do nothing
end function

function OnButtonSelected()
CacheUtils.Clear()
DialogUtils.ShowDialog(Tr(Locale.Settings.CacheCleared), Tr(Locale.Generic.Success))
end function
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ClearCacheControl" extends="LayoutGroup" includes="Focus">
<interface>
<field id="displayText" type="string" alias="Button.text" />
<field id="description" type="string" alias="DescriptionLabel.text" />
<function name="BindPreference" />
</interface>
<children>
<Button
id="Button"
minWidth="340"
showFocusFootprint="true" />
<Label id="DescriptionLabel"
width="450"
color="0xb4b4b4ff"
wrap="true">
<Font role="font" uri="font:SystemFontFile" size="18" />
</Label>
</children>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "pkg:/source/utils/CacheUtils.bs"

namespace Http

class CacheRouter extends HttpRouter

function new()
super()
end function

@delete("/api/cache")
function ClearCache(context as object) as boolean
response = context.response

CacheUtils.Clear()
response.Default(204, "OK")
return true
end function

end class

end namespace
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/BookmarksRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/CacheRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/DashRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/DialRouter.bs"
import "pkg:/components/Web/PlayletWebServer/Middleware/HomeLayoutRouter.bs"
Expand Down Expand Up @@ -50,6 +51,7 @@ function SetupRoutes(server as object)
server.UseRouter(new Http.SearchHistoryRouter(server))
server.UseRouter(new Http.PlayletLibUrlsRouter(server))
server.UseRouter(new Http.DialRouter(server))
server.UseRouter(new Http.CacheRouter())

etags = new Http.EtagUtils()
server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true }))
Expand Down
14 changes: 14 additions & 0 deletions playlet-lib/src/config/preferences.json5
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@
visibility: "web",
svelteComponent: "PlayletLibVersionControl",
},
{
displayText: "Clear cache",
key: "dev.clear_cache",
description: "Clear cache from cachefs: and tmp:",
visibility: "web",
svelteComponent: "ClearCacheControl",
},
{
displayText: "Clear cache",
key: "dev.clear_cache",
description: "Clear cache from cachefs: and tmp:",
visibility: "tv",
rokuComponent: "ClearCacheControl",
},
],
},
]
12 changes: 12 additions & 0 deletions playlet-lib/src/locale/en_US/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,5 +1256,17 @@
<source>Rating</source>
<translation>Rating</translation>
</message>
<message>
<source>Clear cache</source>
<translation>Clear cache</translation>
</message>
<message>
<source>Clear cache from cachefs: and tmp:</source>
<translation>Clear cache from cachefs: and tmp:</translation>
</message>
<message>
<source>Cache cleared.</source>
<translation>Cache cleared.</translation>
</message>
</context>
</TS>
10 changes: 10 additions & 0 deletions playlet-lib/src/source/utils/CacheUtils.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"

namespace CacheUtils

function Clear()
AsyncTask.Start(Tasks.ClearCacheTask, invalid)
end function

end namespace
33 changes: 33 additions & 0 deletions playlet-lib/src/source/utils/ClearCacheTask.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "pkg:/source/utils/Logging.bs"

@asynctask
function ClearCacheTask() as object
fs = CreateObject("roFileSystem")
if fs = invalid
LogError("Failed to create roFileSystem object")
return invalid
end if

ClearVolume(fs, "tmp:/")
ClearVolume(fs, "cachefs:/")

return invalid
end function

function ClearVolume(fs as object, volume as string)
items = fs.GetDirectoryListing(volume)
for each item in items
fullpath = volume + item

' Skip logs directory
if fullpath = "cachefs:/logs"
continue for
end if

if not fs.Delete(fullpath)
LogError("Failed to delete:", fullpath)
end if
end for

LogInfo("Cleared:", volume)
end function
3 changes: 3 additions & 0 deletions playlet-lib/src/source/utils/Locale.bs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ namespace Locale
UseDevVideoPlayerDescription = "Enable the dev video player. This is an unfinished video player for testing purposes."
PlayletLibraryVersion = "Playlet Library version"
PlayletLibraryVersionDescription = "The version of Playlet Lib to use. These correspond to tagged releases on Github. Releases that do not have a playlet-lib.zip file are disabled."
ClearCache = "Clear cache"
ClearCacheDescription = "Clear cache from cachefs: and tmp:"
CacheCleared = "Cache cleared."
end enum

@locale
Expand Down
9 changes: 9 additions & 0 deletions playlet-web/src/lib/Api/InvidiousApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ export class InvidiousApi {
}
}

public static clearCache() {
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
if (key && key.startsWith("v1:")) {
localStorage.removeItem(key);
}
}
}

// TODO:P2 use more appropriate cache storage
private getCache(url: string, cacheSeconds: number) {
const cacheKey = this.getCacheKey(url);
Expand Down
4 changes: 4 additions & 0 deletions playlet-web/src/lib/Api/PlayletApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export class PlayletApi {
return await fetch(`${PlayletApi.host()}/api/search-history`, { method: "DELETE" });
}

static async clearCache() {
return await fetch(`${PlayletApi.host()}/api/cache`, { method: "DELETE" });
}

static async getBookmarkFeeds() {
const response = await fetch(`${PlayletApi.host()}/api/bookmarks/feeds`);
return await response.json();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import { InvidiousApi } from "lib/Api/InvidiousApi";
import { PlayletApi } from "lib/Api/PlayletApi";
import { tr } from "lib/Stores";
import { get } from "svelte/store";
const textSizes = ["text-2xl", "text-lg", "text-base", "text-sm", "text-xs"];
export let displayText: string = "";
// svelte-ignore unused-export-let
export let key: string = "";
export let description: string = "";
export let level: number = 0;
async function clearCache() {
await PlayletApi.clearCache();
InvidiousApi.clearCache();
alert(get(tr)("Cache cleared."));
}
</script>

<div class="m-5">
<div class={textSizes[level]}>{$tr(displayText)}</div>
<div class="text-xs text-gray-500">{@html $tr(description)}</div>
<button class="btn m-1" on:click={clearCache}>{$tr("Clear cache")}</button>
</div>
2 changes: 2 additions & 0 deletions playlet-web/src/lib/Screens/Settings/SettingsNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import StringControl from "lib/Screens/Settings/SettingControls/StringControl.svelte";
import NumberControl from "./SettingControls/NumberControl.svelte";
import { tr } from "lib/Stores";
import ClearCacheControl from "./SettingControls/ClearCacheControl.svelte";
const textSizes = ["text-2xl", "text-lg", "text-base", "text-sm", "text-xs"];
Expand All @@ -30,6 +31,7 @@
const customComponents = {
ClearSearchHistoryControl,
ClearCacheControl,
EditHomeScreenControl,
PlayletLibVersionControl,
};
Expand Down

0 comments on commit 55c811a

Please sign in to comment.