The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
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:
Then we create a Cache
instance:
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
#> Cache class
#> The main cache folder is at /tmp/RtmpjQ3jyd/my.cache.
#> The cache is readable.
#> The cache is writable.
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:
Let us save the following text contents into the cache:
For that we use the saveContents()
method:
cache$saveContents(x, c("a.txt", "b.txt", "c.txt"))
#> INFO [08:25:08.115] Save 3 contents to /tmp/RtmpjQ3jyd/my.cache.
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")
#> INFO [08:25:08.175] Save 3 contents to /tmp/RtmpjQ3jyd/my.cache.
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")
#> [1] "/tmp/RtmpjQ3jyd/my.cache/a.txt" "/tmp/RtmpjQ3jyd/my.cache/b.txt"
#> [3] "/tmp/RtmpjQ3jyd/my.cache/c.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")
#> INFO [08:25:08.217] Load 3 contents, from /tmp/RtmpjQ3jyd/my.cache.
#> /tmp/RtmpjQ3jyd/my.cache/a.txt /tmp/RtmpjQ3jyd/my.cache/b.txt
#> "abc" "def"
#> /tmp/RtmpjQ3jyd/my.cache/c.txt
#> "ghi"
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")
#> INFO [08:25:08.245] Load 4 contents, from /tmp/RtmpjQ3jyd/my.cache.
#> /tmp/RtmpjQ3jyd/my.cache/a.txt /tmp/RtmpjQ3jyd/my.cache/b.txt
#> "abc" "def"
#> /tmp/RtmpjQ3jyd/my.cache/c.txt /tmp/RtmpjQ3jyd/my.cache/d.txt
#> "ghi" NA
Prior to loading contents, we may test the existence of files inside
the cache folder, with the pathsExist()
method:
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")
#> INFO [08:25:08.284] Create cache folder "/tmp/RtmpjQ3jyd/my.cache/mysub".
#> INFO [08:25:08.285] Save 3 contents to /tmp/RtmpjQ3jyd/my.cache/mysub.
Let us look a the content of our sub-folder:
Files may be deleted from the cache, as in the following command:
The remaining files are:
For deleting files inside a sub-folder, use the
sub_folder
parameter:
For deleting a sub-folder with all its content, use the
delFolder()
method:
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
#> [1] "/tmp/RtmpjQ3jyd/my_file52eb42ecdcc07.txt"
To move it into the cache, we use the importFiles()
method:
cache$importFiles(my_file, action = "move")
#> INFO [08:25:08.455] move 1 files to /tmp/RtmpjQ3jyd/my.cache.
#> INFO [08:25:08.456] Done importing files.
For copying, we would have set action
to
"copy"
.
Here the new list of cached files:
If we wish to delete the whole cache folder with all its content, we have to run the following command:
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.