Cache format (#168)
This commit is contained in:
1 parent
4efbdda167
commit
fc3629de09
2 files changed
+62
-13
No files matched your search
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- Layout in channel view so that the upload time and view count of videos is visible
|
- Layout in channel view so that the upload time and view count of videos is visible
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Cache format so it does less parsing work
|
||||||
|
|
||||||
## [0.13.1] - 2023-10-08
|
## [0.13.1] - 2023-10-08
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -555,7 +555,7 @@ namespace HttpClientCache
|
|||||||
end if
|
end if
|
||||||
|
|
||||||
hash = CryptoUtils.GetMd5(cacheKey)
|
hash = CryptoUtils.GetMd5(cacheKey)
|
||||||
request._cacheLocation = `cachefs:/request_v1_${hash}.json`
|
request._cacheLocation = `cachefs:/request_v2_${hash}.json`
|
||||||
return request._cacheLocation
|
return request._cacheLocation
|
||||||
end function
|
end function
|
||||||
|
|
||||||
@@ -590,16 +590,37 @@ namespace HttpClientCache
|
|||||||
|
|
||||||
cacheLocation = GetLocation(request)
|
cacheLocation = GetLocation(request)
|
||||||
cacheText = ReadAsciiFile(cacheLocation)
|
cacheText = ReadAsciiFile(cacheLocation)
|
||||||
cacheObject = ParseJson(cacheText)
|
if cacheText = invalid
|
||||||
if cacheObject = invalid
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
firstLineIndex = cacheText.InStr(`\n`)
|
||||||
|
if firstLineIndex = -1
|
||||||
|
LogWarn("Failed to parse cache.")
|
||||||
|
Delete(request)
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
metadataText = cacheText.Left(firstLineIndex)
|
||||||
|
metadata = ParseJson(metadataText)
|
||||||
|
if metadata = invalid
|
||||||
|
LogWarn("Failed to parse metadata from cache.")
|
||||||
|
Delete(request)
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
headersText = cacheText.Mid(firstLineIndex + 1, metadata.headers)
|
||||||
|
headers = ParseJson(headersText)
|
||||||
|
if headers = invalid
|
||||||
|
LogWarn("Failed to parse headers from cache.")
|
||||||
Delete(request)
|
Delete(request)
|
||||||
return invalid
|
return invalid
|
||||||
end if
|
end if
|
||||||
|
|
||||||
expireSeconds = request._expireSeconds
|
expireSeconds = request._expireSeconds
|
||||||
|
|
||||||
if expireSeconds = invalid and cacheObject.headers <> invalid
|
if expireSeconds = invalid and headers <> invalid
|
||||||
expireSeconds = ParseHeadersForCacheControl(cacheObject.headers)
|
expireSeconds = ParseHeadersForCacheControl(headers)
|
||||||
end if
|
end if
|
||||||
|
|
||||||
if expireSeconds = invalid
|
if expireSeconds = invalid
|
||||||
@@ -608,12 +629,21 @@ namespace HttpClientCache
|
|||||||
|
|
||||||
date = CreateObject("roDateTime")
|
date = CreateObject("roDateTime")
|
||||||
nowTimestamp = date.AsSeconds()
|
nowTimestamp = date.AsSeconds()
|
||||||
if cacheObject.timestamp + expireSeconds < nowTimestamp
|
if metadata.timestamp + expireSeconds < nowTimestamp
|
||||||
Delete(request)
|
Delete(request)
|
||||||
return invalid
|
return invalid
|
||||||
end if
|
end if
|
||||||
|
|
||||||
return cacheObject
|
body = ""
|
||||||
|
if metadata.body > 0
|
||||||
|
body = cacheText.Mid(firstLineIndex + 1 + metadata.headers, metadata.body)
|
||||||
|
end if
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: metadata.statusCode,
|
||||||
|
headers: headers,
|
||||||
|
body: body
|
||||||
|
}
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Set(response as HttpClient.HttpResponse) as void
|
function Set(response as HttpClient.HttpResponse) as void
|
||||||
@@ -643,17 +673,32 @@ namespace HttpClientCache
|
|||||||
date = CreateObject("roDateTime")
|
date = CreateObject("roDateTime")
|
||||||
timestamp = date.AsSeconds()
|
timestamp = date.AsSeconds()
|
||||||
|
|
||||||
cacheObject = {
|
' Cache file format:
|
||||||
|
' First line is a json payload of metadata:
|
||||||
|
' {
|
||||||
|
' timestamp: number,
|
||||||
|
' statusCode: number,
|
||||||
|
' headers: number, ' this is the size of the headers json array in chars
|
||||||
|
' body: number ' this is the size of the body in chars
|
||||||
|
' }
|
||||||
|
' {headers} ' a json objects with headers, string size matching metadata.headers
|
||||||
|
' {body} ' the body as is, string size matching metadata.body
|
||||||
|
|
||||||
|
jsonHeaders = FormatJson(response.Headers())
|
||||||
|
body = ValidString(response.Text())
|
||||||
|
|
||||||
|
cacheMetadata = {
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
statusCode: response.StatusCode(),
|
statusCode: response.StatusCode(),
|
||||||
headers: response.Headers(),
|
headers: jsonHeaders.Len(),
|
||||||
body: response.Text()
|
body: body.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jsonMetadata = FormatJson(cacheMetadata)
|
||||||
|
|
||||||
cacheLocation = GetLocation(response.request)
|
cacheLocation = GetLocation(response.request)
|
||||||
' TODO:P2 use a diferent format other than json for better performance
|
|
||||||
cacheText = FormatJson(cacheObject)
|
WriteAsciiFile(cacheLocation, jsonMetadata + `\n` + jsonHeaders + body)
|
||||||
WriteAsciiFile(cacheLocation, cacheText)
|
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Delete(request as HttpClient.HttpRequest) as boolean
|
function Delete(request as HttpClient.HttpRequest) as boolean
|
||||||
|
|||||||
Reference in new issue
Block a user