-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Add caching module for YouTube video info #96
Conversation
- Introduced `VInfoCache` class for creating, checking, and retrieving cached video information. - Added `CacheBase64` class for encoding and decoding cache objects using Base64. - Added `CacheZLib` class for compressing and decompressing cache objects using zlib. - Defined `CACHE_PATH` and `VINFO_CACHE_PATH` constants for cache directory paths. - Implemented utility functions for cache path generation and ID validation. - Included comprehensive JSDoc comments for better understanding and usage examples.
- Enhanced `VInfoCache.getCache` to support `humanReadable` option, allowing cache data to be returned as a formatted string. - Updated `VInfoCache.getAllCaches` to include `humanReadable` option for retrieving all caches in a readable format. - Improved JSDoc comments to reflect the new functionality and provide detailed descriptions of the new options.
- Added unit tests for `getCachePath` to verify correct cache path generation. - Added unit tests for `CacheBase64` to test encoding and decoding of objects. - Added unit tests for `CacheZLib` to test compression and decompression of objects. - Added unit tests for `VInfoCache` to test cache creation, validation, retrieval, and error handling. - Included tests for the new `humanReadable` option in cache retrieval functions.
- Optimized the `VInfoCache.createCache` function to return the cache path early if the cache file already exists. - Introduced a new error class, called `CacheValidationError` to handle invalid cache objects with detailed contextual information (e.g., cache ID, type, and path). - Removed the `VInfoCache.checkCache` function to consolidated cache validation logic into relevant methods, due to unreliable method to check the cache object while it also unnecessary retrieves and parses the cache object. - Added the `cacheOptions.validate` option to validate the cache object exact before returning it, implemented to `VInfoCache.getCache` and `VInfoCache.getAllCaches` functions. - Enhanced the `VInfoCache.getCache` and `VInfoCache.getAllCaches` by throwing the `CacheValidationError` when the cache object does not meet the expected format. - Refactored the `VInfoCache.getAllCaches` parameters (such as `cachePath` and `humanReadable`) into a single parameter that accepts an object called `cacheOptions`, will be passed to `VInfoCache.getCache` function.
This commit introduces tests for handling invalid cache objects, focusing on scenarios where validation is enabled. - Added setup in `before` hook to create an invalid cache scenario for testing. - Added `CacheValidationError` handling in validation-related tests. - Removed the test cases for `VInfoCache.checkCache` function, which has been removed (1362df6). - Added new test cases to validate cache objects and handle errors during retrieval and listing when `cacheOptions.validate` is enabled. - Updated `testMessages.VInfoCache` with a new message for validation-specific testing.
This cause the `getAllCaches` function give a null of video info from the cache object. Now has been resolved by removing unnecessary check of video info keys in decrypted cache object.
This to indicate that cache object has been created at specific date. The value itself is a milliseconds returned from `Date.now()`. - Added the `createdDate` property to cache object when creating cache. - Exposed the `createdDate` value to human-readable string format in `getCache` function for adding detail information of cache.
- Added `VInfoCache.deleteCache` function to delete the cache with a given ID - Added support for `deleteCache` to use the specified cache directory if provided
- Added a test case for testing the `VInfoCache.deleteCache` function - A minor refactor to other test cases
- Declared a new typedef, `ExtractedVideoInfoCacheObject`, to represents the parsed and extracted cache of video information object - Changed the returned type in `getCache` and `getAllCaches` functions
- Renamed `cacheOptions.cachePath` to `cacheOptions.cacheDir` for consistency and to avoid confusion. - Updated all references and documentation to use `cacheDir` instead of `cachePath`. - Removed unused import of `@distube/ytdl-core`.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #96 +/- ##
==========================================
+ Coverage 80.36% 83.16% +2.80%
==========================================
Files 11 12 +1
Lines 611 713 +102
==========================================
+ Hits 491 593 +102
Misses 120 120
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
These changes are only the cache module implementation. Integrating to the core module ( |
Cache module recently updated and comes with a new feature to force the cache creation. Refer to #98 for more details. cacheOptions: {
cacheDir?: string,
force?: boolean
} |
This pull request introduces the ability to force the creation of a cache even when an entry with the same video ID already exists. This feature ensures that developers or later implementations can overwrite outdated or incorrect cache objects with new video information, keeping the cache accurate and up to date. - b72300e - test(unit): Add test case to verify the cache overwrite ability - a3a11bb - feat(cache): Add new option to force the cache creation Related changes: #96 --- Signed-off-by: Ryuu Mitsuki <dhefam31@gmail.com>
The return logic of the `getAllCaches` function has been improved to better align with its intended behavior, offering more predictable and meaningful responses based on the `humanReadable` option. This change enhances usability and prepares the API for more diverse use cases. - 1246a8a - test(unit): Add test case to verify the adjusted return value - 9992268 - fix(cache): Change the return value if no caches found Related changes: #96, #98 --- Signed-off-by: Ryuu Mitsuki <dhefam31@gmail.com>
Overview
This pull request introduces a new caching module to optimize the retrieval of YouTube video information. By implementing a local caching mechanism, the system avoids repetitive fetch requests to YouTube servers, resulting in significant performance improvements.
Changes Made
Features
VInfoCache
class to handle caching of video information, including creation, validation, and retrieval.CacheBase64
andCacheZLib
for encoding, decoding, compressing, and decompressing cache objects.CACHE_PATH
,VINFO_CACHE_PATH
).humanReadable
option toVInfoCache.getCache
andVInfoCache.getAllCaches
for better presentation of cache data.createdDate
property to cache objects to indicate the creation timestamp.VInfoCache.deleteCache
for cache deletion.CacheValidationError
to handle invalid cache objects with detailed contextual information (e.g., cache ID, type, and path).Implementation
VInfoCache
:CacheBase64
:CacheZLib
:Testing
CacheBase64
,CacheZLib
,VInfoCache
, and utility functions.CacheValidationError
.humanReadable
option.Documentation
VideoInfoCacheObject
, to represent the cache data structure.ExtractedVideoInfoCacheObject
, to represent parsed and extracted cache data.Cache Structure
The cache structure is as follows:
TypeScript:
Details
id
encoding
"binary"
.createdDate
Date.now()
).title
"<unknown>"
if not available.authorName
"<unknown>"
if not available.videoUrl
authorUrl
videoInfo
videoInfo.type
"zlib/bin"
.videoInfo.data
Usage Examples
Please take note first, that the cache utilities are intended for internal use.
Impact
Summary
This PR establishes the foundation for a robust caching mechanism that enhances both performance and usability for video information handling. The changes ensure better scalability and reliability while adhering to high standards of code quality and maintainability.