# Create a new cache instance inside a custom folder
cache <- Cache$new(tempdir())
# Create some contents for the example
contents <- c("a", "b", "c")
# Save contents
cache$saveContents(contents, c("a.txt", "b.txt", "c.txt"),
sub_folder = "sub1")
# Get list of files inside folder
files <- cache$listFolder("sub1")
# Delete files
cache$delPaths(c("a.txt", "c.txt"), sub_folder = "sub1")
# Delete whole sub-folder
cache$delFolder("sub1")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$new`
## ------------------------------------------------
# Create a new cache instance.
# Note for the sake of the example we use a temporary directory specified
# as an absolute path, however the usual way to use the cache system is
# to provide a relative path, that will be placed inside the standard
# user cache folder defined by the OS.
cache <- Cache$new(tempdir())
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$isReadable`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Tests if readable (TRUE by default)
if (cache$isReadable()) {
print("Cache is readable")
}
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$isWritable`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Tests if writeable (TRUE by default)
if (cache$isWritable()) {
print("Cache is writeable")
}
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$setReadable`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Disallow reading
cache$setReadable(FALSE)
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$setWritable`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Disallow writing
cache$setWritable(FALSE)
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$getFolder`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get the absolute path to the cache folder
folder <- cache$getFolder()
# Get the absolute path to a cache sub-folder
sub_folder <- cache$getFolder('my_sub_folder')
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$hasFolder`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Test if a sub-folder exists
if (cache$hasFolder("my_sub_folder")) {
print("Sub-folder exists.")
}
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$getPaths`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get the paths a list of filenames should have in the cache folder
paths <- cache$getPaths(c("a.csv", "b.txt"))
# Get paths using a common extension for filenames
paths <- cache$getPaths(c("a", "b"), suffix = ".csv")
# Get paths of files inside a sub-folder
paths <- cache$getPaths(c("a.csv", "b.txt"), sub_folder = "foo")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$globPaths`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get all existing files inside sub-folder foo
paths <- cache$globPaths(sub_folder = "foo")
# Get all existing files with extension ".txt" inside main folder
paths <- cache$globPaths(suffix = ".txt")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$getNbItems`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get the number of files inside sub-folder "foo"
n <- cache$getNbItems("foo")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$pathsExist`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Test if some files exist in the cache
exits <- cache$pathsExist(c("a", "b"), suffix = ".txt")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$tagExists`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Test if tag file "downloaded" exists in sub-folder "hmdb"
if (cache$tagExists("downloaded", sub_folder = "hmdb")) {
print("Tag exists")
}
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$writeTag`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Create tag file "downloaded" in sub-folder "hmdb"
cache$writeTag("downloaded", sub_folder = "hmdb")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$getTmp`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get the cache temporary folder
tmp <- cache$getTmp()
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$getSubFolders`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Get the list of sub-folders
sub.folders <- cache$getSubFolders()
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$importFiles`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Create some files for the example
files <- c("k.txt", "u.csv")
file.create(files)
# Move those files into the cache
cache$importFiles(files, sub_folder = "foo", action = "copy")
# Remove original files
unlink(files)
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$saveContents`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Create some contents for the example
contents <- c("a", "b", "c")
# Save contents
cache$saveContents(contents, c("a.txt", "b.txt", "c.txt"))
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$loadContents`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Create some contents for the example
contents <- c("1", "2", "3")
# Save contents
cache$saveContents(contents, c("a", "b", "c"), suffix = ".txt",
sub_folder = "ex2")
# Load contents
contents <- cache$loadContents(c("a", "b", "c"), suffix = ".txt",
sub_folder = "ex2")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$delPaths`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Delete some cache files
cache$delPaths(c("a.txt", "b.txt"))
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$delFolder`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Delete sub-folder
cache$delFolder("my_sub_folder")
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$listFolder`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new("my_cache_folder")
# List files in sub-folder
files <- cache$listFolder("my_sub_folder")
# Remove cache folder
cache$erase()
## ------------------------------------------------
## Method `Cache$print`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Print information
print(cache)
# Erase cache
cache$erase()
## ------------------------------------------------
## Method `Cache$erase`
## ------------------------------------------------
# Create a new cache instance
cache <- Cache$new(tempdir())
# Deletes the whole cache content
cache$erase()
# Erase cache
cache$erase()
Run the code above in your browser using DataLab