Skip to content

Commit

Permalink
FEAT: included delete-thru in the thru-cache module and storing c…
Browse files Browse the repository at this point in the history
…ontent locally only when response code is 200 (OK)

related to: Oldes/Rebol-issues#2554
  • Loading branch information
Oldes committed Jul 17, 2024
1 parent f419d30 commit d9189e9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/modules/thru-cache.reb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ Rebol [
name: thru-cache
type: module
options: [delay]
version: 0.2.0
exports: [path-thru read-thru load-thru do-thru clear-thru list-thru exists-thru?]
version: 0.2.1
exports: [path-thru read-thru load-thru do-thru clear-thru list-thru exists-thru? delete-thru]
author: @Oldes
file: %thru-cache.reb
home: https://src.rebol.tech/modules/thru-cache.reb
]

;; Initialize the directory for storing local files
so: system/options
so/log/thru-cache: 2

unless select so 'thru-cache [
put so 'thru-cache join to-real-file any [
get-env "TEMP"
so/data
] %thru-cache/
sys/log/info 'REBOL ["Using thru-cache:" mold so/thru-cache]
sys/log/info 'THRU-CACHE ["Content directory:" mold so/thru-cache]
]

path-thru: func [
Expand All @@ -28,7 +30,7 @@ path-thru: func [
/local hash file path
][
unless find so 'thru-cache [return none]
hash: checksum form url 'MD5
hash: checksum mold url 'MD5
file: head (remove back tail remove remove (form hash))
path: dirize append copy so/thru-cache copy/part file 2
unless exists? path [make-dir/deep path]
Expand All @@ -40,13 +42,14 @@ read-thru: func [
url [url!] "Remote file address"
/update "Force a cache update"
/string "Try convert result to string"
/local path data
/local path data code
][
path: path-thru url
either all [not update exists? path] [
data: read/binary path
][
data: read/binary/all url
code: any [all [block? data data/1] 200]
if all [
block? data
object? data/2
Expand All @@ -57,9 +60,14 @@ read-thru: func [
data/3
]
]
try [
write/binary path data
log-thru-file path url
either code == 200 [
try [
write/binary path data
log-thru-file path url
sys/log/more 'THRU-CACHE ["Stored:" mold url]
]
][
sys/log/debug 'THRU-CACHE ["Response" code " Not stored:" mold url]
]
]
if string [try [data: to string! data]]
Expand Down Expand Up @@ -99,7 +107,7 @@ clear-thru: func [
if all [
exists? dir/:path
find/any/match url filter
][ print [as-green skip path 3 url] ]
][ print [as-green skip path 3 mold url] ]
]
exit
]
Expand Down Expand Up @@ -128,11 +136,22 @@ exists-thru?: func[
exists? any [all [file? url url] path-thru url]
]

delete-thru: func[
"Deletes a local disk cache file"
url [url!] "Remote file address"
/local path
][
if exists? path: path-thru url [
sys/log/more 'THRU-CACHE ["Removed:" mold url]
delete path
]
]

list-thru: func[
"Prints localy stored URLs"
/only filter [string!] "List only the files that match the filter"
][
sys/log/info 'REBOL ["Used thru-cache directory:" mold so/thru-cache]
sys/log/info 'THRU-CACHE ["Content directory:" mold so/thru-cache]
clear-thru/:only/test filter
]

Expand Down
19 changes: 19 additions & 0 deletions src/tests/units/thru-cache-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ if module? try [import 'thru-cache][
equal? str1 str2
equal? str1 read/string url
]]

--test-- "read-thru status <> 200"
url: http://httpbin.org/status/400
--assert try [all [
binary? read-thru url ;; returned, but not stored!
not exists-thru? url
]]

--test-- "load-thru"
url: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/tests/units/files/print-args.r3
Expand All @@ -29,6 +36,10 @@ if module? try [import 'thru-cache][
block? blk2: load url
equal? blk1 blk2
]]

--test-- "exists-thru?"
--assert 'file = exists-thru? url
--assert none? exists-thru? http://not-exists

--test-- "do-thru"
--assert not error? try [do-thru url] ;; evaluates the previously downloaded and cached script
Expand All @@ -41,6 +52,14 @@ if module? try [import 'thru-cache][
clear-thru ;; removes everything
--assert none? exists? path-thru url

--test-- "delete-thru"
--assert all [
binary? read-thru url
'file = exists-thru? url
port? delete-thru url
not exists-thru? url
]

===end-group===
]

Expand Down

0 comments on commit d9189e9

Please sign in to comment.