knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
fscache helps handle a user file system cache for an application or package.
Let us suppose we have an application called myapp
for which we want to save
text content on disk in order to reload it later.
With fscache we will create a Cache
object that will place our content in a
standard location, according to the poperating system we are running on, and
help us manage it (save, load, delete, place in sub-folders, etc).
First, start by loading the package:
library(fscache)
Then we create a Cache
instance:
my_folder_cache <- file.path(tempdir(), "my.cache") cache <- Cache$new(my_folder_cache)
For this vignette, we use a temporary folder as our cache folder.
Since the temporary folder is an absolute path, the user HOME folder will not
be used.
In practice, however, we give the name of our application as the cache folder
to create. It will then be created inside the standard use cache folder, which
is relative to the user HOME folder.
The folder will be created in the user space, inside the standard path defined
for the operating system, given by the tools
package.
If we look at the cache
object, we can see the exact location of the created
cache folder:
cache
We also see that the cache is both readable and writable by default.
However we may block either read access or write access for our cache, using
the setReadable()
and setWritable()
methods.
It is sometimes useuful for testing purposes.
The full path to the cache folder is also accessible through the getFolder()
method:
cache$getFolder()
Let us save the following text contents into the cache:
x <- c("abc", "def", "ghi")
For that we use the saveContents()
method:
cache$saveContents(x, c("a.txt", "b.txt", "c.txt"))
We have saved our three strings into three files inside the cache folder.
Since all three files have the same extension, we may have used the suffix
parameters to avoid repeating the extension in the filenames:
cache$saveContents(x, c("a", "b", "c"), suffix = ".txt")
Note that fscache does not complain that the files already exist. It overrides them silently.
We can list the existing files inside the cache folder with the following command:
cache$globPaths(suffix = ".txt")
Loading the contents from cached files is done with the loadContents()
method, which returns a character
vector with names set with the file paths:
cache$loadContents(c("a", "b", "c"), suffix = ".txt")
Note that if we try to load the content from non-existing file, a NA
value
will be returned:
cache$loadContents(c("a", "b", "c", "d"), suffix = ".txt")
Prior to loading contents, we may test the existence of files inside the cache
folder, with the pathsExist()
method:
cache$pathsExist(c("a", "b", "c", "d"), suffix = ".txt")
Instead of putting them directly inside the cache folder, files may be put inside sub-folders to organize them.
Here we put our contents into three new files inside a sub-folder named
"mysub"
that will be automatically created:
cache$saveContents(x, c("x", "y", "z"), suffix = ".txt", sub_folder = "mysub")
Let us look a the content of our sub-folder:
cache$globPaths(sub_folder = "mysub")
Files may be deleted from the cache, as in the following command:
cache$delPaths(c("a", "c"), suffix = ".txt")
The remaining files are:
cache$globPaths()
For deleting files inside a sub-folder, use the sub_folder
parameter:
cache$delPaths("y", suffix = ".txt", sub_folder = "mysub")
For deleting a sub-folder with all its content, use the delFolder()
method:
cache$delFolder("mysub")
It is also possible to move or copy files that reside outside the cache folder, into the cache folder.
Let us create a file:
my_file <- tempfile("my_file", fileext = ".txt") cat("My text content.", file = my_file) my_file
To move it into the cache, we use the importFiles()
method:
cache$importFiles(my_file, action = "move")
For copying, we would have set action
to "copy"
.
Here the new list of cached files:
cache$globPaths()
If we wish to delete the whole cache folder with all its content, we have to run the following command:
cache$erase()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.