Wolfram Research

Function Repository Resource:

ImportOnce

Source Notebook

A version of Import which uses a cache of the result unless the source file has changed

Contributed by: Jon McLoone

ResourceFunction["ImportOnce"][file,opts]

imports data from file, caching the result unless the contents of file has changed.

Details and Options

ResourceFunction["ImportOnce"] takes the following options:
"ComparisonMethod" "LastModificationDate" Method for deciding whether the file has changed, requiring a new import and cache
"ForceImport" False Override the cache, forcing the file to be re-imported.
By default, the decision whether to re-import the file or retrieve the cached value is decided by looking at the "LastModificationDate", but FileHash and any values returned by Information can be used. For example, if the file is being repeatedly re-written with the same values,"LastModificationDate" would change, causing unnecessary import work, but FileHash would only re-import if the file contents had changed. It is important to always use the same method. Alternating methods will always import the file again.

Examples

Basic Examples

ImportOnce behaves the same as Import:

In[1]:=
AbsoluteTiming[ResourceFunction["ImportOnce"]["ExampleData/rose.gif"]]
Out[1]=

But if you use it again on the same file and the file has not changed, it is faster:

In[2]:=
AbsoluteTiming[ResourceFunction["ImportOnce"]["ExampleData/rose.gif"]]
Out[2]=

Options

Change the method for deciding if a file has changed:

In[3]:=
AbsoluteTiming[
 ResourceFunction["ImportOnce"]["ExampleData/rose.gif", "ComparisonMethod" -> FileHash]]
Out[3]=
In[4]:=
AbsoluteTiming[
 ResourceFunction["ImportOnce"]["ExampleData/rose.gif", "ComparisonMethod" -> FileHash]]
Out[4]=

If you have reason to believe that the file has changed in a way that is not detected by your choice of "ComparisonMethod", you can force a refresh with "ForceImport"True:

In[5]:=
AbsoluteTiming[
 ResourceFunction["ImportOnce"]["ExampleData/rose.gif", "ComparisonMethod" -> FileHash, "ForceImport" -> True]]
Out[5]=

Possible Issues

ImportOnce can only pre-check file changes for local files. Importing over http or https will always import the file again:

In[6]:=
AbsoluteTiming[
 ResourceFunction["ImportOnce"]["http://www.wolfram.com", "Text"];]
Out[6]=
In[7]:=
AbsoluteTiming[
 ResourceFunction["ImportOnce"]["http://www.wolfram.com", "Text"];]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information