Introduction to fscache package

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).

Initializing the cache

First, start by loading the package:


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 class
#>   The main cache folder is at /tmp/RtmpTxo928/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:

#> [1] "/tmp/RtmpTxo928/my.cache"

Saving and loading content

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"))
#> INFO  [13:50:47.931] Save 3 contents to /tmp/RtmpTxo928/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  [13:50:47.979] Save 3 contents to /tmp/RtmpTxo928/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/RtmpTxo928/my.cache/a.txt" "/tmp/RtmpTxo928/my.cache/b.txt"
#> [3] "/tmp/RtmpTxo928/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  [13:50:47.997] Load 3 contents, from /tmp/RtmpTxo928/my.cache.
#> /tmp/RtmpTxo928/my.cache/a.txt /tmp/RtmpTxo928/my.cache/b.txt 
#>                          "abc"                          "def" 
#> /tmp/RtmpTxo928/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  [13:50:48.014] Load 4 contents, from /tmp/RtmpTxo928/my.cache.
#> /tmp/RtmpTxo928/my.cache/a.txt /tmp/RtmpTxo928/my.cache/b.txt 
#>                          "abc"                          "def" 
#> /tmp/RtmpTxo928/my.cache/c.txt /tmp/RtmpTxo928/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:

cache$pathsExist(c("a", "b", "c", "d"), suffix = ".txt")

Using sub-folders

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  [13:50:48.031] Create cache folder "/tmp/RtmpTxo928/my.cache/mysub".
#> INFO  [13:50:48.032] Save 3 contents to /tmp/RtmpTxo928/my.cache/mysub.

Let us look a the content of our sub-folder:

cache$globPaths(sub_folder = "mysub")
#> [1] "/tmp/RtmpTxo928/my.cache/mysub/x.txt"
#> [2] "/tmp/RtmpTxo928/my.cache/mysub/y.txt"
#> [3] "/tmp/RtmpTxo928/my.cache/mysub/z.txt"

Deleting files

Files may be deleted from the cache, as in the following command:

cache$delPaths(c("a", "c"), suffix = ".txt")

The remaining files are:

#> [1] "/tmp/RtmpTxo928/my.cache/b.txt"

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:

#> INFO  [13:50:48.083] Erasing sub-folder "mysub".

Copying or moving files into the cache

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)
#> [1] "/tmp/RtmpTxo928/my_file1ff07d1df59877.txt"

To move it into the cache, we use the importFiles() method:

cache$importFiles(my_file, action = "move")
#> INFO  [13:50:48.098] move 1 files to /tmp/RtmpTxo928/my.cache.
#> INFO  [13:50:48.099] Done importing files.

For copying, we would have set action to "copy".

Here the new list of cached files:

#> [1] "/tmp/RtmpTxo928/my.cache/b.txt"                    
#> [2] "/tmp/RtmpTxo928/my.cache/my_file1ff07d1df59877.txt"

Erasing the cache

If we wish to delete the whole cache folder with all its content, we have to run the following command:

#> INFO  [13:50:48.112] Erasing cache "/tmp/RtmpTxo928/my.cache".