Skip to content

Commit d9189e9

Browse files
committed
FEAT: included delete-thru in the thru-cache module and storing content locally only when response code is 200 (OK)
related to: Oldes/Rebol-issues#2554
1 parent f419d30 commit d9189e9

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/modules/thru-cache.reb

+29-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ Rebol [
44
name: thru-cache
55
type: module
66
options: [delay]
7-
version: 0.2.0
8-
exports: [path-thru read-thru load-thru do-thru clear-thru list-thru exists-thru?]
7+
version: 0.2.1
8+
exports: [path-thru read-thru load-thru do-thru clear-thru list-thru exists-thru? delete-thru]
99
author: @Oldes
1010
file: %thru-cache.reb
1111
home: https://src.rebol.tech/modules/thru-cache.reb
1212
]
1313

1414
;; Initialize the directory for storing local files
1515
so: system/options
16+
so/log/thru-cache: 2
17+
1618
unless select so 'thru-cache [
1719
put so 'thru-cache join to-real-file any [
1820
get-env "TEMP"
1921
so/data
2022
] %thru-cache/
21-
sys/log/info 'REBOL ["Using thru-cache:" mold so/thru-cache]
23+
sys/log/info 'THRU-CACHE ["Content directory:" mold so/thru-cache]
2224
]
2325

2426
path-thru: func [
@@ -28,7 +30,7 @@ path-thru: func [
2830
/local hash file path
2931
][
3032
unless find so 'thru-cache [return none]
31-
hash: checksum form url 'MD5
33+
hash: checksum mold url 'MD5
3234
file: head (remove back tail remove remove (form hash))
3335
path: dirize append copy so/thru-cache copy/part file 2
3436
unless exists? path [make-dir/deep path]
@@ -40,13 +42,14 @@ read-thru: func [
4042
url [url!] "Remote file address"
4143
/update "Force a cache update"
4244
/string "Try convert result to string"
43-
/local path data
45+
/local path data code
4446
][
4547
path: path-thru url
4648
either all [not update exists? path] [
4749
data: read/binary path
4850
][
4951
data: read/binary/all url
52+
code: any [all [block? data data/1] 200]
5053
if all [
5154
block? data
5255
object? data/2
@@ -57,9 +60,14 @@ read-thru: func [
5760
data/3
5861
]
5962
]
60-
try [
61-
write/binary path data
62-
log-thru-file path url
63+
either code == 200 [
64+
try [
65+
write/binary path data
66+
log-thru-file path url
67+
sys/log/more 'THRU-CACHE ["Stored:" mold url]
68+
]
69+
][
70+
sys/log/debug 'THRU-CACHE ["Response" code " Not stored:" mold url]
6371
]
6472
]
6573
if string [try [data: to string! data]]
@@ -99,7 +107,7 @@ clear-thru: func [
99107
if all [
100108
exists? dir/:path
101109
find/any/match url filter
102-
][ print [as-green skip path 3 url] ]
110+
][ print [as-green skip path 3 mold url] ]
103111
]
104112
exit
105113
]
@@ -128,11 +136,22 @@ exists-thru?: func[
128136
exists? any [all [file? url url] path-thru url]
129137
]
130138

139+
delete-thru: func[
140+
"Deletes a local disk cache file"
141+
url [url!] "Remote file address"
142+
/local path
143+
][
144+
if exists? path: path-thru url [
145+
sys/log/more 'THRU-CACHE ["Removed:" mold url]
146+
delete path
147+
]
148+
]
149+
131150
list-thru: func[
132151
"Prints localy stored URLs"
133152
/only filter [string!] "List only the files that match the filter"
134153
][
135-
sys/log/info 'REBOL ["Used thru-cache directory:" mold so/thru-cache]
154+
sys/log/info 'THRU-CACHE ["Content directory:" mold so/thru-cache]
136155
clear-thru/:only/test filter
137156
]
138157

src/tests/units/thru-cache-test.r3

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ if module? try [import 'thru-cache][
2121
equal? str1 str2
2222
equal? str1 read/string url
2323
]]
24+
25+
--test-- "read-thru status <> 200"
26+
url: http://httpbin.org/status/400
27+
--assert try [all [
28+
binary? read-thru url ;; returned, but not stored!
29+
not exists-thru? url
30+
]]
2431

2532
--test-- "load-thru"
2633
url: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/tests/units/files/print-args.r3
@@ -29,6 +36,10 @@ if module? try [import 'thru-cache][
2936
block? blk2: load url
3037
equal? blk1 blk2
3138
]]
39+
40+
--test-- "exists-thru?"
41+
--assert 'file = exists-thru? url
42+
--assert none? exists-thru? http://not-exists
3243

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

55+
--test-- "delete-thru"
56+
--assert all [
57+
binary? read-thru url
58+
'file = exists-thru? url
59+
port? delete-thru url
60+
not exists-thru? url
61+
]
62+
4463
===end-group===
4564
]
4665

0 commit comments

Comments
 (0)