-
Notifications
You must be signed in to change notification settings - Fork 9
feat(cdn): adding in cdn caching #41
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
Open
bassrock
wants to merge
14
commits into
rive-app:main
Choose a base branch
from
bassrock:db/feat/cdn-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9465d85
feat(cdn/viewDispoal): adding in cdn caching and view disposal checking
bassrock e52a135
fix(dispose): removing the disposed for ios
bassrock 1320a60
fix(android): removing android disposal
bassrock 95b62b4
Merge branch 'main' of github.com:rive-app/rive-nitro-react-native in…
bassrock c513f3d
feat(cache): updating to a basic url cache
bassrock 3b94ed6
fix(android): updating android and ios cdn rive
bassrock b9d9c8e
fix(docs): removing pr description
bassrock cc72242
Merge branch 'main' of github.com:rive-app/rive-nitro-react-native in…
bassrock 58f3d92
Merge commit '4b9433d0e48383c88818235d9dedd59d30f034bb' into db/feat/…
bassrock 24a872a
Merge branch 'rive-app:main' into db/feat/cdn-cache
bassrock cc5c68d
Merge branch 'main' into db/feat/cdn-cache
bassrock 29a5372
Merge branch 'main' into db/feat/cdn-cache
bassrock 37660fb
Merge branch 'main' into db/feat/cdn-cache
bassrock 68944ef
Merge branch 'main' into db/feat/cdn-cache
bassrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
android/src/main/java/com/margelo/nitro/rive/URLAssetCache.kt
This file contains hidden or 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,65 @@ | ||
| package com.margelo.nitro.rive | ||
|
|
||
| import android.content.Context | ||
| import android.util.Log | ||
| import com.margelo.nitro.NitroModules | ||
| import java.io.File | ||
| import java.security.MessageDigest | ||
|
|
||
| object URLAssetCache { | ||
| private const val CACHE_DIR_NAME = "rive_url_assets" | ||
| private const val TAG = "URLAssetCache" | ||
|
|
||
| private fun getCacheDir(): File? { | ||
| val context = NitroModules.applicationContext ?: return null | ||
| val cacheDir = File(context.cacheDir, CACHE_DIR_NAME) | ||
| if (!cacheDir.exists()) { | ||
| cacheDir.mkdirs() | ||
| } | ||
| return cacheDir | ||
| } | ||
|
|
||
| private fun urlToCacheKey(url: String): String { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| val hashBytes = digest.digest(url.toByteArray()) | ||
| return hashBytes.joinToString("") { "%02x".format(it) } | ||
| } | ||
|
|
||
| private fun getCacheFile(url: String): File? { | ||
| val cacheDir = getCacheDir() ?: return null | ||
| val cacheKey = urlToCacheKey(url) | ||
| return File(cacheDir, cacheKey) | ||
| } | ||
|
|
||
| fun getCachedData(url: String): ByteArray? { | ||
| return try { | ||
| val cacheFile = getCacheFile(url) ?: return null | ||
| if (cacheFile.exists() && cacheFile.length() > 0) { | ||
| cacheFile.readBytes() | ||
| } else { | ||
| null | ||
| } | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to read from cache: ${e.message}") | ||
| null | ||
| } | ||
| } | ||
|
|
||
| fun saveToCache(url: String, data: ByteArray) { | ||
| try { | ||
| val cacheFile = getCacheFile(url) ?: return | ||
| cacheFile.writeBytes(data) | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to save to cache: ${e.message}") | ||
| } | ||
| } | ||
|
|
||
| fun clearCache() { | ||
| try { | ||
| val cacheDir = getCacheDir() ?: return | ||
| cacheDir.listFiles()?.forEach { it.delete() } | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Failed to clear cache: ${e.message}") | ||
| } | ||
| } | ||
| } |
Empty file.
This file contains hidden or 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 hidden or 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,71 @@ | ||
| import Foundation | ||
| import CryptoKit | ||
|
|
||
| enum URLAssetCache { | ||
| private static let cacheDirName = "rive_url_assets" | ||
|
|
||
| private static func getCacheDirectory() -> URL? { | ||
| guard let cacheDir = FileManager.default.urls( | ||
| for: .cachesDirectory, | ||
| in: .userDomainMask | ||
| ).first else { | ||
| return nil | ||
| } | ||
| let riveCacheDir = cacheDir.appendingPathComponent(cacheDirName) | ||
|
|
||
| // Create directory if it doesn't exist | ||
| try? FileManager.default.createDirectory( | ||
| at: riveCacheDir, | ||
| withIntermediateDirectories: true, | ||
| attributes: nil | ||
| ) | ||
|
|
||
| return riveCacheDir | ||
| } | ||
|
|
||
| private static func urlToCacheKey(_ url: String) -> String { | ||
| let data = Data(url.utf8) | ||
| let hash = SHA256.hash(data: data) | ||
| return hash.compactMap { String(format: "%02x", $0) }.joined() | ||
| } | ||
|
|
||
| private static func getCacheFileURL(for url: String) -> URL? { | ||
| guard let cacheDir = getCacheDirectory() else { return nil } | ||
| let cacheKey = urlToCacheKey(url) | ||
| return cacheDir.appendingPathComponent(cacheKey) | ||
| } | ||
|
|
||
| static func getCachedData(for url: String) -> Data? { | ||
| guard let cacheFileURL = getCacheFileURL(for: url) else { return nil } | ||
|
|
||
| do { | ||
| let data = try Data(contentsOf: cacheFileURL) | ||
| return data.isEmpty ? nil : data | ||
| } catch { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| static func saveToCache(_ data: Data, for url: String) { | ||
| guard let cacheFileURL = getCacheFileURL(for: url) else { return } | ||
|
|
||
| do { | ||
| try data.write(to: cacheFileURL) | ||
| } catch { | ||
| // Silently fail - caching is best effort | ||
| } | ||
| } | ||
|
|
||
| static func clearCache() { | ||
| guard let cacheDir = getCacheDirectory() else { return } | ||
|
|
||
| do { | ||
| let files = try FileManager.default.contentsOfDirectory(at: cacheDir, includingPropertiesForKeys: nil) | ||
| for file in files { | ||
| try? FileManager.default.removeItem(at: file) | ||
| } | ||
| } catch { | ||
| // Silently fail | ||
| } | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
nitrogen/generated/android/c++/JHybridViewModelImagePropertySpec.cpp
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.