@@ -4,21 +4,23 @@ Rebol [
4
4
name: thru-cache
5
5
type: module
6
6
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 ]
9
9
author: @Oldes
10
10
file: %thru-cache.reb
11
11
home: https://src.rebol.tech/modules/thru-cache.reb
12
12
]
13
13
14
14
;; Initialize the directory for storing local files
15
15
so: system/options
16
+ so/log/thru-cache: 2
17
+
16
18
unless select so 'thru-cache [
17
19
put so 'thru-cache join to-real-file any [
18
20
get-env "TEMP"
19
21
so/data
20
22
] %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 ]
22
24
]
23
25
24
26
path-thru : func [
@@ -28,7 +30,7 @@ path-thru: func [
28
30
/local hash file path
29
31
] [
30
32
unless find so 'thru-cache [return none]
31
- hash: checksum form url 'MD5
33
+ hash: checksum mold url 'MD5
32
34
file: head (remove back tail remove remove (form hash))
33
35
path: dirize append copy so/thru-cache copy/part file 2
34
36
unless exists? path [make-dir/deep path]
@@ -40,13 +42,14 @@ read-thru: func [
40
42
url [url! ] "Remote file address"
41
43
/update "Force a cache update"
42
44
/string "Try convert result to string"
43
- /local path data
45
+ /local path data code
44
46
] [
45
47
path: path-thru url
46
48
either all [not update exists? path] [
47
49
data: read /binary path
48
50
][
49
51
data: read /binary/all url
52
+ code: any [all [block? data data/1 ] 200 ]
50
53
if all [
51
54
block? data
52
55
object? data/2
@@ -57,9 +60,14 @@ read-thru: func [
57
60
data/3
58
61
]
59
62
]
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]
63
71
]
64
72
]
65
73
if string [try [data: to string! data]]
@@ -99,7 +107,7 @@ clear-thru: func [
99
107
if all [
100
108
exists? dir/: path
101
109
find/any/match url filter
102
- ][ print [as-green skip path 3 url] ]
110
+ ][ print [as-green skip path 3 mold url] ]
103
111
]
104
112
exit
105
113
]
@@ -128,11 +136,22 @@ exists-thru?: func[
128
136
exists? any [all [file? url url] path-thru url]
129
137
]
130
138
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
+
131
150
list-thru : func [
132
151
"Prints localy stored URLs"
133
152
/only filter [string! ] "List only the files that match the filter"
134
153
] [
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 ]
136
155
clear-thru/: only/test filter
137
156
]
138
157
0 commit comments