Debian Codegraph @

man/FileCache.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/file_cache.R
3 \name{FileCache}
4 \alias{FileCache}
5 \title{Create a file cache object}
6 \description{
7 Create a file cache object
8
9 Create a file cache object
10 }
11 \details{
12 A file cache object is a key-file store that saves the values as files in a
13 directory on disk. The objects are files on disk. They are stored and
14 retrieved using the \code{get_file()}, \code{get_content()}, \code{set_file()}, and
15 \code{set_content()} methods. Objects are automatically pruned from the cache
16 according to the parameters \code{max_size}, \code{max_age}, \code{max_n}, and \code{evict}.
17 }
18 \section{Cache pruning}{
19
20
21 Cache pruning occurs when \code{set_file()} or \code{set_content()} is called, or it
22 can be invoked manually by calling \code{prune()}.
23
24 The disk cache will throttle the pruning so that it does not happen on
25 every call to \code{set_file()} or \code{set_content()}, because the filesystem
26 operations for checking the status of files can be slow. Instead, it will
27 prune once in every 20 calls to \code{set_file()} or \code{set_content()}, or if at
28 least 5 seconds have elapsed since the last prune occurred, whichever is
29 first. These parameters are currently not customizable, but may be in the
30 future.
31
32 When a pruning occurs, if there are any objects that are older than
33 \code{max_age}, they will be removed.
34
35 The \code{max_size} and \code{max_n} parameters are applied to the cache as a whole,
36 in contrast to \code{max_age}, which is applied to each object individually.
37
38 If the number of objects in the cache exceeds \code{max_n}, then objects will be
39 removed from the cache according to the eviction policy, which is set with
40 the \code{evict} parameter. Objects will be removed so that the number of items
41 is \code{max_n}.
42
43 If the size of the objects in the cache exceeds \code{max_size}, then objects
44 will be removed from the cache. Objects will be removed from the cache so
45 that the total size remains under \code{max_size}. Note that the size is
46 calculated using the size of the files, not the size of disk space used by
47 the files --- these two values can differ because of files are stored in
48 blocks on disk. For example, if the block size is 4096 bytes, then a file
49 that is one byte in size will take 4096 bytes on disk.
50
51 Another time that objects can be removed from the cache is when
52 \code{get_file()} or \code{get_content()} is called. If the target object is older
53 than \code{max_age}, it will be removed and the cache will report it as a
54 missing value.
55 }
56
57 \section{Eviction policies}{
58
59
60 If \code{max_n} or \code{max_size} are used, then objects will be removed from the
61 cache according to an eviction policy. The available eviction policies are:
62
63 \describe{ \item{\code{"lru"}}{ Least Recently Used. The least recently used
64 objects will be removed. This uses the filesystem's mtime property. When
65 "lru" is used, each time \code{get_file()} or \code{get_content()} is called, it will
66 update the file's mtime. } \item{\code{"fifo"}}{ First-in-first-out. The oldest
67 objects will be removed. } }
68
69 Both of these policies use files' mtime. Note that some filesystems
70 (notably FAT) have poor mtime resolution. (atime is not used because
71 support for atime is worse than mtime.)
72 }
73
74 \section{Sharing among multiple processes}{
75
76
77 The directory for a FileCache can be shared among multiple R processes. To
78 do this, each R process should have a FileCache object that uses the same
79 directory. Each FileCache will do pruning independently of the others, so
80 if they have different pruning parameters, then one FileCache may remove
81 cached objects before another FileCache would do so.
82
83 Even though it is possible for multiple processes to share a FileCache
84 directory, this should not be done on networked file systems, because of
85 slow performance of networked file systems can cause problems. If you need
86 a high-performance shared cache, you can use one built on a database like
87 Redis, SQLite, mySQL, or similar.
88
89 When multiple processes share a cache directory, there are some potential
90 race conditions. For example, if your code calls \code{exists(key)} to check if
91 an object is in the cache, and then call \code{get_file(key)}, the object may be
92 removed from the cache in between those two calls, and \code{get_file(key)} will
93 throw an error. Instead of calling the two functions, it is better to
94 simply call \code{get_file(key)}, and use \code{tryCatch()} to handle the error that
95 is thrown if the object is not in the cache. This effectively tests for
96 existence and gets the object in one operation.
97
98 It is also possible for one processes to prune objects at the same time
99 that another processes is trying to prune objects. If this happens, you may
100 see a warning from \code{file.remove()} failing to remove a file that has
101 already been deleted.
102 }
103
104 \keyword{internal}
105 \section{Methods}{
106 \subsection{Public methods}{
107 \itemize{
108 \item \href{#method-FileCache-new}{\code{FileCache$new()}}
109 \item \href{#method-FileCache-get_file}{\code{FileCache$get_file()}}
110 \item \href{#method-FileCache-get_content}{\code{FileCache$get_content()}}
111 \item \href{#method-FileCache-set_file}{\code{FileCache$set_file()}}
112 \item \href{#method-FileCache-set_content}{\code{FileCache$set_content()}}
113 \item \href{#method-FileCache-exists}{\code{FileCache$exists()}}
114 \item \href{#method-FileCache-keys}{\code{FileCache$keys()}}
115 \item \href{#method-FileCache-remove}{\code{FileCache$remove()}}
116 \item \href{#method-FileCache-reset}{\code{FileCache$reset()}}
117 \item \href{#method-FileCache-dir}{\code{FileCache$dir()}}
118 \item \href{#method-FileCache-prune}{\code{FileCache$prune()}}
119 \item \href{#method-FileCache-size}{\code{FileCache$size()}}
120 \item \href{#method-FileCache-destroy}{\code{FileCache$destroy()}}
121 \item \href{#method-FileCache-is_destroyed}{\code{FileCache$is_destroyed()}}
122 \item \href{#method-FileCache-clone}{\code{FileCache$clone()}}
123 }
124 }
125 \if{html}{\out{<hr>}}
126 \if{html}{\out{<a id="method-FileCache-new"></a>}}
127 \if{latex}{\out{\hypertarget{method-FileCache-new}{}}}
128 \subsection{Method \code{new()}}{
129 Create a FileCache object.
130 \subsection{Usage}{
131 \if{html}{\out{<div class="r">}}\preformatted{FileCache$new(
132 dir = NULL,
133 max_size = 40 * 1024^2,
134 max_age = Inf,
135 max_n = Inf,
136 evict = c("lru", "fifo"),
137 destroy_on_finalize = FALSE,
138 logfile = NULL
139 )}\if{html}{\out{</div>}}
140 }
141
142 \subsection{Arguments}{
143 \if{html}{\out{<div class="arguments">}}
144 \describe{
145 \item{\code{dir}}{Directory to store files for the cache. If \code{NULL} (the default) it
146 will create and use a temporary directory.}
147
148 \item{\code{max_size}}{Maximum size of the cache, in bytes. If the cache exceeds
149 this size, cached objects will be removed according to the value of the
150 \code{evict}. Use \code{Inf} for no size limit.}
151
152 \item{\code{max_age}}{Maximum age of files in cache before they are evicted, in
153 seconds. Use \code{Inf} for no age limit.}
154
155 \item{\code{max_n}}{Maximum number of objects in the cache. If the number of objects
156 exceeds this value, then cached objects will be removed according to the
157 value of \code{evict}. Use \code{Inf} for no limit of number of items.}
158
159 \item{\code{evict}}{The eviction policy to use to decide which objects are removed
160 when a cache pruning occurs. Currently, \code{"lru"} and \code{"fifo"} are supported.}
161
162 \item{\code{destroy_on_finalize}}{If \code{TRUE}, then when the FileCache object is
163 garbage collected, the cache directory and all objects inside of it will be
164 deleted from disk. If \code{FALSE} (the default), it will do nothing when
165 finalized.}
166
167 \item{\code{logfile}}{An optional filename or connection object to where logging
168 information will be written. To log to the console, use \code{stdout()}.}
169 }
170 \if{html}{\out{</div>}}
171 }
172 }
173 \if{html}{\out{<hr>}}
174 \if{html}{\out{<a id="method-FileCache-get_file"></a>}}
175 \if{latex}{\out{\hypertarget{method-FileCache-get_file}{}}}
176 \subsection{Method \code{get_file()}}{
177 Get the content associated with \code{key}, and save in a file
178 named \code{outfile}.
179 \subsection{Usage}{
180 \if{html}{\out{<div class="r">}}\preformatted{FileCache$get_file(key, outfile, overwrite = TRUE)}\if{html}{\out{</div>}}
181 }
182
183 \subsection{Arguments}{
184 \if{html}{\out{<div class="arguments">}}
185 \describe{
186 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
187
188 \item{\code{outfile}}{Name of output file. If \code{NULL}, return the content as}
189
190 \item{\code{overwrite}}{If the output file already exists, should it be
191 overwritten?}
192 }
193 \if{html}{\out{</div>}}
194 }
195 \subsection{Returns}{
196 \code{TRUE} if the object is found in the cache and copying succeeds,
197 \code{FALSE} otherwise.
198 }
199 }
200 \if{html}{\out{<hr>}}
201 \if{html}{\out{<a id="method-FileCache-get_content"></a>}}
202 \if{latex}{\out{\hypertarget{method-FileCache-get_content}{}}}
203 \subsection{Method \code{get_content()}}{
204 Get the content associated with \code{key}, and return as either
205 string or a raw vector.
206 \subsection{Usage}{
207 \if{html}{\out{<div class="r">}}\preformatted{FileCache$get_content(key, mode = c("text", "raw"))}\if{html}{\out{</div>}}
208 }
209
210 \subsection{Arguments}{
211 \if{html}{\out{<div class="arguments">}}
212 \describe{
213 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
214
215 \item{\code{mode}}{If \code{"text"}, return the content as a UTF-8-encoded text
216 string (a one element char vector). If \code{"raw"}, return the content as a
217 raw vector.}
218 }
219 \if{html}{\out{</div>}}
220 }
221 \subsection{Returns}{
222 A character or raw vector if the object is found in the cache,
223 \code{NULL} otherwise.
224 }
225 }
226 \if{html}{\out{<hr>}}
227 \if{html}{\out{<a id="method-FileCache-set_file"></a>}}
228 \if{latex}{\out{\hypertarget{method-FileCache-set_file}{}}}
229 \subsection{Method \code{set_file()}}{
230 Sets content associated with \code{key}, from a file named
231 \code{infile}.
232 \subsection{Usage}{
233 \if{html}{\out{<div class="r">}}\preformatted{FileCache$set_file(key, infile)}\if{html}{\out{</div>}}
234 }
235
236 \subsection{Arguments}{
237 \if{html}{\out{<div class="arguments">}}
238 \describe{
239 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
240
241 \item{\code{infile}}{Name of input file.}
242 }
243 \if{html}{\out{</div>}}
244 }
245 \subsection{Returns}{
246 \code{TRUE} if copying the file into the cache succeeds, \code{FALSE}
247 otherwise.
248 }
249 }
250 \if{html}{\out{<hr>}}
251 \if{html}{\out{<a id="method-FileCache-set_content"></a>}}
252 \if{latex}{\out{\hypertarget{method-FileCache-set_content}{}}}
253 \subsection{Method \code{set_content()}}{
254 Sets content associated with \code{key}, from a single-element
255 vector.
256 \subsection{Usage}{
257 \if{html}{\out{<div class="r">}}\preformatted{FileCache$set_content(key, content)}\if{html}{\out{</div>}}
258 }
259
260 \subsection{Arguments}{
261 \if{html}{\out{<div class="arguments">}}
262 \describe{
263 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
264
265 \item{\code{content}}{A character or raw vector. If it is a character vector,
266 it will be written with UTF-8 encoding, with with elements collapsed
267 with \verb{\\\\n} (consistent across platforms).}
268 }
269 \if{html}{\out{</div>}}
270 }
271 \subsection{Returns}{
272 \code{TRUE} if setting the content in the cache succeeds, \code{FALSE}
273 otherwise.
274 }
275 }
276 \if{html}{\out{<hr>}}
277 \if{html}{\out{<a id="method-FileCache-exists"></a>}}
278 \if{latex}{\out{\hypertarget{method-FileCache-exists}{}}}
279 \subsection{Method \code{exists()}}{
280 Check if content associated with \code{key} exists in cache
281 \subsection{Usage}{
282 \if{html}{\out{<div class="r">}}\preformatted{FileCache$exists(key)}\if{html}{\out{</div>}}
283 }
284
285 \subsection{Arguments}{
286 \if{html}{\out{<div class="arguments">}}
287 \describe{
288 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
289 }
290 \if{html}{\out{</div>}}
291 }
292 \subsection{Returns}{
293 \code{TRUE} if the object is in the cache, \code{FALSE} otherwise.
294 }
295 }
296 \if{html}{\out{<hr>}}
297 \if{html}{\out{<a id="method-FileCache-keys"></a>}}
298 \if{latex}{\out{\hypertarget{method-FileCache-keys}{}}}
299 \subsection{Method \code{keys()}}{
300 Get all keys
301 \subsection{Usage}{
302 \if{html}{\out{<div class="r">}}\preformatted{FileCache$keys()}\if{html}{\out{</div>}}
303 }
304
305 \subsection{Returns}{
306 A character vector of all keys currently in the cache.
307 }
308 }
309 \if{html}{\out{<hr>}}
310 \if{html}{\out{<a id="method-FileCache-remove"></a>}}
311 \if{latex}{\out{\hypertarget{method-FileCache-remove}{}}}
312 \subsection{Method \code{remove()}}{
313 Remove an object
314 \subsection{Usage}{
315 \if{html}{\out{<div class="r">}}\preformatted{FileCache$remove(key)}\if{html}{\out{</div>}}
316 }
317
318 \subsection{Arguments}{
319 \if{html}{\out{<div class="arguments">}}
320 \describe{
321 \item{\code{key}}{Key. Must be lowercase numbers and letters.}
322 }
323 \if{html}{\out{</div>}}
324 }
325 \subsection{Returns}{
326 \code{TRUE} if the object was found and successfully removed, \code{FALSE}
327 otherwise.
328 }
329 }
330 \if{html}{\out{<hr>}}
331 \if{html}{\out{<a id="method-FileCache-reset"></a>}}
332 \if{latex}{\out{\hypertarget{method-FileCache-reset}{}}}
333 \subsection{Method \code{reset()}}{
334 Clear all objects from the cache.
335 \subsection{Usage}{
336 \if{html}{\out{<div class="r">}}\preformatted{FileCache$reset()}\if{html}{\out{</div>}}
337 }
338
339 }
340 \if{html}{\out{<hr>}}
341 \if{html}{\out{<a id="method-FileCache-dir"></a>}}
342 \if{latex}{\out{\hypertarget{method-FileCache-dir}{}}}
343 \subsection{Method \code{dir()}}{
344 Returns the directory used for the cache.
345 \subsection{Usage}{
346 \if{html}{\out{<div class="r">}}\preformatted{FileCache$dir()}\if{html}{\out{</div>}}
347 }
348
349 }
350 \if{html}{\out{<hr>}}
351 \if{html}{\out{<a id="method-FileCache-prune"></a>}}
352 \if{latex}{\out{\hypertarget{method-FileCache-prune}{}}}
353 \subsection{Method \code{prune()}}{
354 Prune the cache, using the parameters specified by
355 \code{max_size}, \code{max_age}, \code{max_n}, and \code{evict}.
356 \subsection{Usage}{
357 \if{html}{\out{<div class="r">}}\preformatted{FileCache$prune()}\if{html}{\out{</div>}}
358 }
359
360 }
361 \if{html}{\out{<hr>}}
362 \if{html}{\out{<a id="method-FileCache-size"></a>}}
363 \if{latex}{\out{\hypertarget{method-FileCache-size}{}}}
364 \subsection{Method \code{size()}}{
365 Return the number of items currently in the cache.
366 \subsection{Usage}{
367 \if{html}{\out{<div class="r">}}\preformatted{FileCache$size()}\if{html}{\out{</div>}}
368 }
369
370 }
371 \if{html}{\out{<hr>}}
372 \if{html}{\out{<a id="method-FileCache-destroy"></a>}}
373 \if{latex}{\out{\hypertarget{method-FileCache-destroy}{}}}
374 \subsection{Method \code{destroy()}}{
375 Clears all objects in the cache, and removes the cache
376 directory from disk.
377 \subsection{Usage}{
378 \if{html}{\out{<div class="r">}}\preformatted{FileCache$destroy()}\if{html}{\out{</div>}}
379 }
380
381 }
382 \if{html}{\out{<hr>}}
383 \if{html}{\out{<a id="method-FileCache-is_destroyed"></a>}}
384 \if{latex}{\out{\hypertarget{method-FileCache-is_destroyed}{}}}
385 \subsection{Method \code{is_destroyed()}}{
386 Reports whether the cache has been destroyed.
387 \subsection{Usage}{
388 \if{html}{\out{<div class="r">}}\preformatted{FileCache$is_destroyed(throw = FALSE)}\if{html}{\out{</div>}}
389 }
390
391 \subsection{Arguments}{
392 \if{html}{\out{<div class="arguments">}}
393 \describe{
394 \item{\code{throw}}{Should this function throw an error if the cache has been
395 destroyed?}
396 }
397 \if{html}{\out{</div>}}
398 }
399 }
400 \if{html}{\out{<hr>}}
401 \if{html}{\out{<a id="method-FileCache-clone"></a>}}
402 \if{latex}{\out{\hypertarget{method-FileCache-clone}{}}}
403 \subsection{Method \code{clone()}}{
404 The objects of this class are cloneable with this method.
405 \subsection{Usage}{
406 \if{html}{\out{<div class="r">}}\preformatted{FileCache$clone(deep = FALSE)}\if{html}{\out{</div>}}
407 }
408
409 \subsection{Arguments}{
410 \if{html}{\out{<div class="arguments">}}
411 \describe{
412 \item{\code{deep}}{Whether to make a deep clone.}
413 }
414 \if{html}{\out{</div>}}
415 }
416 }
417 }